使用Powershell打开端口 [英] Opening up a port with powershell

查看:839
本文介绍了使用Powershell打开端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Machine A上,我正在运行端口扫描程序.我想在Machine B上以有组织的方式打开和关闭端口.我希望通过powershell来完成所有这些工作.

On Machine A I am running a port scanner. On Machine B I would like to open and close ports in an organized fashion. I am hoping to do all of this via powershell.

我找到了脚本可在Machine B上运行,但是从Machine A扫描同一端口时,它仍然表示已关闭.

I found THIS script to run on Machine B however when scanning the same port from Machine A it still says that it is closed.

您是否知道我如何成功打开Machine B

Do any of you know how I can successfully open a port on Machine B

推荐答案

如果可能,请避免使用COM.您可以使用 TcpListener 打开端口:

Avoid COM if possible. You can use TcpListener to open a port:

$Listener = [System.Net.Sockets.TcpListener]9999;
$Listener.Start();
#wait, try connect from another PC etc.
$Listener.Stop();

如果在调试过程中碰巧错过了Stop命令-只需关闭并从打开套接字的位置重新打开应用程序-应该清除挂起的端口.就我而言,是PowerGUI script editor.

If you happen to miss a Stop command during debugging - just close and re-open the application from where you opened the socket - hanging port should be cleared. In my case it was PowerGUI script editor.

然后使用 TcpClient 进行检查

(new-object Net.Sockets.TcpClient).Connect($host, $port)

如果您无法连接,则表示防火墙正在阻止它.

If you cannot connect, means the firewall is blocking it.

编辑:要在收到连接时打印消息,您应该能够使用此代码(基于

To print a message when connection is received, you should be able to use this code (based on this article from MSDN):

#put this code in between Start and Stop calls.
while($true) 
{
    $client = $Listener.AcceptTcpClient();
    Write-Host "Connected!";
    $client.Close();
}

这篇关于使用Powershell打开端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆