检查没有telnet的端口打开

Kar*_*arl 26 debug port

当未安装 Telnet 时,人们使用什么来检查端口是否打开且可访问?例如,我曾经使用 的技术telnet <destination>并且知道它在那里,即使 telnet 无法与另一端的系统交互。

没有安装 Windows 2008 telnet 所以我有点迷茫。那我可以用什么代替。如果它在 Linux 或 Solaris 中不存在,也请提供。

我是一名在不同网站上工作的顾问。出于多种原因(访问权限、更改控制时间、如果我安装它明年有人使用它我们有一些责任等)我无法安装在其他人的服务器上。但是 USB 或其他自包含的、未安装的工具会很棒......

sko*_*hrs 50

这里有几种不同的方法可以在没有 telnet 的情况下测试 TCP 端口。

BASH手册页

# cat < /dev/tcp/127.0.0.1/22
SSH-2.0-OpenSSH_5.3
^C

# cat < /dev/tcp/127.0.0.1/23
bash: connect: Connection refused
bash: /dev/tcp/127.0.0.1/23: Connection refused
Run Code Online (Sandbox Code Playgroud)


卷曲

# curl -v telnet://127.0.0.1:22
* About to connect() to 127.0.0.1 port 22 (#0)
*   Trying 127.0.0.1... connected
* Connected to 127.0.0.1 (127.0.0.1) port 22 (#0)
SSH-2.0-OpenSSH_5.3
^C

# curl -v telnet://127.0.0.1:23
* About to connect() to 127.0.0.1 port 23 (#0)
*   Trying 127.0.0.1... Connection refused
* couldn't connect to host
* Closing connection #0
curl: (7) couldn't connect to host
Run Code Online (Sandbox Code Playgroud)


Python

# python
Python 2.6.6 (r266:84292, Oct 12 2012, 14:23:48)
[GCC 4.4.6 20120305 (Red Hat 4.4.6-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import socket
>>> clientsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>>> clientsocket.connect(('127.0.0.1', 22))
>>> clientsocket.send('\n')
1
>>> clientsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>>> clientsocket.connect(('127.0.0.1', 23))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1, in connect
socket.error: [Errno 111] Connection refused
Run Code Online (Sandbox Code Playgroud)


珀尔

# perl
use IO::Socket::INET;
$| = 1;
my $socket = new IO::Socket::INET(
  PeerHost => '127.0.0.1',
  PeerPort => '22',
  Proto => 'tcp',
);
die "cannot connect to the server $!\n" unless $socket;
print "connected to the server\n";
^D
connected to the server
Run Code Online (Sandbox Code Playgroud)


Vas*_*kis 34

像老板一样使用 Powershell


基本代码

$ipaddress = "4.2.2.1"
$port = 53
$connection = New-Object System.Net.Sockets.TcpClient($ipaddress, $port)

if ($connection.Connected) {
    Write-Host "Success"
}
else {
    Write-Host "Failed"
}
Run Code Online (Sandbox Code Playgroud)

一个班轮

PS C:\> test-netconnection -ComputerName 4.2.2.1 -Port 53
Run Code Online (Sandbox Code Playgroud)

将其转换为 cmdlet

[CmdletBinding()]
Param(
  [Parameter(Mandatory=$True,Position=1)]
   [string]$ip,
    
   [Parameter(Mandatory=$True,Position=2)]
   [int]$port
)

$connection = New-Object System.Net.Sockets.TcpClient($ip, $port)
if ($connection.Connected) {
    Return "Connection Success"
}
else {
    Return "Connection Failed"
}
Run Code Online (Sandbox Code Playgroud)

另存为脚本,一直使用

然后你在你的 powershell 或 cmd 提示符中使用命令,如下所示:

PS C:\> telnet.ps1 -ip 8.8.8.8 -port 53

或者

PS C:\> telnet.ps1 8.8.8.8 53

  • 您也可以只执行: New-Object System.Net.Sockets.TcpClient("IP or DomainName", 80) 如果无法连接,您将收到错误消息,如果连接成功,您将收到有关创建对象的信息。 (6认同)