sad*_*ave 3 remote-access 11.10 localhost telnet
我写了一个python脚本来监听2626端口并执行脚本。
本地主机正在监听的证明
tcp 0 0 127.0.0.1:2626 0.0.0.0:* LISTEN
Run Code Online (Sandbox Code Playgroud)
我可以使用以下命令通过 telnet 连接到本地主机上的这个端口:
:~$ telnet localhost 2626
Trying ::1...
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试从远程机器连接时,连接被拒绝。
:~$ telnet 128.251.xxx.xxx 2626
Trying 128.251.xxx.xxx...
telnet: Unable to connect to remote host: Connection refused
Run Code Online (Sandbox Code Playgroud)
我什至发出以下命令来接受端口 2626 上的流量:
sudo iptables -A INPUT -p tcp -i eth0 --dport 2626 -j ACCEPT
Run Code Online (Sandbox Code Playgroud)
端口 2626 上的 telnet 拒绝来自远程机器的连接的原因可能是什么,我该如何解决?
该输出表明您的程序127.0.0.1仅在侦听。侦听套接字(开放端口)要么绑定到特定接口,如在这种情况下,要么绑定到所有接口(在这种情况下,您将看到0.0.0.0“任何地址”的表示法)。绑定到127.0.0.1only的含义是只有通过该接口的连接才会进入侦听套接字,从而进入您的程序。如果您使用的是 pythonsocket库,请在bind()调用中绑定到您的外部 IP ,或者绑定到所有 IP,如下所示:
>>> import socket
>>> s = socket.socket()
>>> s.bind(('', 6666))
>>> s.listen(1)
Run Code Online (Sandbox Code Playgroud)
这给出了你想要的结果 netstat
tcp 0 0 0.0.0.0:6666 0.0.0.0:* LISTEN
Run Code Online (Sandbox Code Playgroud)
并且确实可以从另一台机器连接到
ial@roach> telnet polihale 6666
Trying 128.243.20.139...
Connected to polihale.
Escape character is '^]'.
Run Code Online (Sandbox Code Playgroud)
在127.0.0.1:2626从netstat的输出表明您的Python脚本只在接受连接127.0.0.1。如果您希望能够接受来自任何地址的连接,请让它接受以下地址的连接0.0.0.0