我正在尝试将设置ssh连接与主机连接。这是我的代码:
def make_connection_paramiko(Username, Password):
ssh = paramiko.SSHClient()
hostname = "username@hobbes.cs.ucsb.edu"
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.load_host_keys(os.path.expanduser(os.path.join("~", ".ssh", "known_hosts")))
try:
ssh.connect(hostname, port = 22, username = 'username', password = 'password')
except paramiko.AuthenticationException:
print "Login failed! %s\t%s" %(username, password)
except socket.timeout:
print "Socket connection failed"
#print str(value) +"\t"+ message
else:
print "Login Successful! %s\t%s" %(username, password)
ssh.close()
但是由于某种原因,我继续遇到以下错误:
Traceback (most recent call last):
File "pass_crack.py", line 56, in <module>
begin_cracking(wordlist, username)
File "pass_crack.py", line 45, in begin_cracking
make_connection_paramiko(username, "hello")
File "pass_crack.py", line 29, in make_connection_paramiko
ssh.connect(hostname, port = 3600, username = 'xxxxxxx', password = 'xxxxxx')
File "/usr/lib/python2.7/dist-packages/paramiko/client.py", line 282, in connect
for (family, socktype, proto, canonname, sockaddr) in socket.getaddrinfo(hostname, port, socket.AF_UNSPEC, socket.SOCK_STREAM):
socket.error: [Errno 2] No such file or directory
我正在尝试使用paramiko和python进行连接,而我正在使用Ubuntu 13.04。我不确定是什么问题,当我尝试使用
pxssh
为主机名,用户名和密码使用相同的值进行连接时,连接是否有效,那么为什么对paramiko不起作用?提前致谢
最佳答案
回答
这不是主机名:
hostname = "username@hobbes.cs.ucsb.edu"
而是一个连接字符串。删除
username@
部分,它应该再次连接。更多的信息
请记住,您始终可以look at the source code。在这里,您可以看到主机名已传递给directly into the raw socket call:
socket.getaddrinfo(hostname, port, socket.AF_UNSPEC, socket.SOCK_STREAM)
查看
socket.getaddrinfo
的帮助,我们可以看到它正在尝试解析实际的主机名,类似于nslookup
所需的语法:>>> print socket.getaddrinfo.__doc__
getaddrinfo(host, port [, family, socktype, proto, flags])
-> list of (family, socktype, proto, canonname, sockaddr)
Resolve host and port into addrinfo struct.
最后,我建议您在paramiko和其他基础库中启用
debugging
:>>> import logging
>>> logger = paramiko.util.logging.getLogger()
>>> logger.setLevel(logging.DEBUG)
关于python - paramiko和python ssh,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20083132/