[英]Python SSH server (socket + paramiko) “Address already in use”
So I decided to read more about networks in Python, and in the book I'm reading, there's this piece of code that creates a SSH server using paramiko (a third party SSH module) and socket. 因此,我决定阅读有关Python网络的更多信息,在我正在阅读的书中,有一段代码使用paramiko(第三方SSH模块)和套接字创建了SSH服务器。
The problem I'm having is that whenever I input a server address, it says "Address already in use". 我遇到的问题是,每当我输入服务器地址时,都会显示“地址已在使用中”。 In addition, I'm already using sock.setsockopt(sock.SOL_SOCKET, sock.SO_REUSEADDR, 1)
so the address can be reused, but the problem still persists. 另外,我已经在使用sock.setsockopt(sock.SOL_SOCKET, sock.SO_REUSEADDR, 1)
因此可以重复使用该地址,但是问题仍然存在。
Here's the full code: 这是完整的代码:
import socket
import paramiko
import threading
import sys
import traceback
# using the key from the Paramiko demo files
host_key = paramiko.RSAKey(filename='test_rsa.key')
class Server (paramiko.ServerInterface):
def __init__(self):
self.event = threading.Event()
def check_channel_request(self, kind, chanid):
if kind=='session':
return paramiko.OPEN_SUCCEEDED
return paramiko.OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED
def check_auth_password(self,user, password):
if (usernae == 'matheus') and (password == 'password'):
return paramiko.AUTH_SUCCESSFUL
return paramiko.AUTH_FAILED
server = sys.argv[1]
ssh_port = int(sys.argv[2])
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind((server, ssh_port))
sock.listen(100) # Wow so many connections
print ("[+] Listening for connection ...")
client, addr = sock.accept()
except Exception, e:
print("[-] Listen failed: " + str(e))
traceback.print_stack()
sys.exit(1)
print("[+] Got a connection!") # runs as except exits
try:
bhSession = paramiko.Transport(client)
bhSession.add_server_key(host_key)
server = Server()
try:
bhSession.start_server(server=server)
except paramiko.SSHException, x:
print("[-] SSH negotiation failed.")
chan = bhSession.accept(20)
print("[+] Authenticated!")
print(chan.recv(1024))
chan.send("Welcome to bh_ssh")
while True:
try:
command = raw_input("Enter command: ").strip('\n')
if command != 'exit':
chan.send(command)
print(chan.recv(1024)+'\n')
else:
chan.send('exit')
print("exiting")
bhSession.close()
raise Exception("exit")
except KeyboardInterrupt:
bhSession.close()
except Exception, e:
print("[-] Caught exception: " + str(e))
try:
bhSession.close()
except:
pass
sys.exit(1)
The addresses I've tried are: 我尝试过的地址是:
192.168.1.107 (current device address)
0.0.0.0
0.0.0.1
127.0.0.1 (localhost)
The port was always 22. 该端口始终为22。
Peace! 和平!
SO_REUSEPORT
is what most people would expect SO_REUSEADDR
to be. SO_REUSEPORT
是大多数人期望的SO_REUSEADDR
。
Basically, SO_REUSEPORT
allows you to bind an arbitrary number of sockets to exactly the same source address and port as long as all prior bound sockets also had SO_REUSEPORT
set before they were bound. 基本上, SO_REUSEPORT
允许您将任意数量的套接字绑定到完全相同的源地址和端口,只要所有先前绑定的套接字在绑定之前也都设置了SO_REUSEPORT
。
绑定套接字之前,请尝试使用SO_REUSEADDR
套接字选项。
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.