paramiko channel AttributeError:' NoneType'对象没有属性' recv'

时间:2015-03-21 20:47:52

标签: python ssh paramiko

我正在创建一个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'

据我所知,当我输入channel = session.accept()时,我收到无,但为什么我没有?请帮助。

1 个答案:

答案 0 :(得分:3)

你有一个错字

paswd = "pikachups"(一个人在paswd中)

if (username == usern) and (password == passwd)(密码中的两个人)

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

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