paramiko channel AttributeError: 'NoneType' object has no attribute 'recv'

paramiko channel AttributeError: 'NoneType' object has no attribute 'recv'

我正在创建 SSH 服务器:

#make import
import sys, threading, socket, paramiko

#information about user
usern = "dacoder"
paswd = "pikachups"

#server builder
class Server(paramiko.ServerInterface):
    #initialize build
    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)
    #check password
    def check_auth_password(self, username, password):
        if (username == usern) and (password == passwd):
            return(paramiko.AUTH_SUCCESSFUL)
        return(paramiko.AUTH_FAILED)

#main loop
def looper(ip, port, key, keypassword):
    #create sockets
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    #set ssh options for socket
    sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    #bind socket
    try:
        sock.bind((ip, port))
    except:
        print("error : UNABLE TO BIND SOCKET AT " + str(ip) + ":" + str(port))
        sys.exit()
    #begin listening 
    sock.listen(20)
print("[*] listening . . . \n")
while True:
    #connect to client and get address
    try:
        (client, addr) = sock.accept()
        print("[+] connected to " + addr[0])
    except:
        print("error : CANNOT CONNECT TO CLIENT")
        sys.exit()
    #transport client to paramiko and add server key
    try:
        session = paramiko.Transport(client)
        session.add_server_key(paramiko.RSAKey(filename=key, password=keypassword))
    except:
        print("error : UNABLE TO TRANPORT CLIENT AND//OR ADD SERVER KEY")
        sys.exit()
    #create server instance
    server = Server()
    #start server that we have just created
    try:
        session.start_server(server=server)
    except:
        print("error : CANNOT START SERVER")
        sys.exit()
    #create channel
    channel = session.accept()
    print("[*] authenticated")
    #get data from client
    print("[==>] receiving data from client : ")
    data = channel.recv(1024)
    print(data.decode("utf-8"))
    #send echo to client
    print("[<==] sending echo to client")
    channel.send(data)
    #close session
    session.close()

#cmd line parser
def main():
    #fancy cmd line parsing
    if (len(sys.argv[1:]) != 4) and (len(sys.argv[1:]) != 3):
        print("SSH SERVER\nCREATED BY BENDACODER")
        print("USAGE : \n./SSHserver.py [serverIP] [serverPort] [sshKey] [sshKeyPswd]=optional")
        sys.exit()
    ip = sys.argv[1]
    port = int(sys.argv[2])
    key = sys.argv[3]
    try:
        pswd = sys.argv[4]
    except:
        pswd = None
    looper(ip, port, key, pswd)

#execute in cmd
main()

一切似乎都还好。但是,当我像这样通过 CMD 行启动服务器时:

./SSHserver.py 127.0.16.16 5005 /root/.ssh/id_rsa

并通过此客户端代码连接到服务器:

import paramiko
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect("127.0.16.16", port=5005, username="dacoder", password="pikachups")

然后我收到这个错误:

Traceback (most recent call last):
  File "./SSHserver.py", line 99, in <module>
    main()
  File "./SSHserver.py", line 96, in main
    looper(ip, port, key, pswd)
  File "./SSHserver.py", line 74, in looper
    data = channel.recv(1024)
AttributeError: 'NoneType' object has no attribute 'recv'

我知道当我进入频道 = session.accept() 时,我收到 None,但为什么我收到 none?请帮助。

你打错字了

paswd = "pikachups"(密码中的一个's')

if (username == usern) and (password == passwd)(密码中有两个's')

如果启用了日志记录,您也可以自己发现它:

paramiko.util.log_to_file("filename.log")