Domanda

I am trying to connect setup a ssh connection with a host machine. Here is my code:

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()

But for some reason I keep on getting the following error:

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

I am trying to connect using paramiko with python, and I am using Ubuntu 13.04. I am not sure what is wrong, when I have tried to connect using the same values for the hostname, username, and password using pxssh the connection works, so why doesn't it work with paramiko?

Thanks in advance

È stato utile?

Soluzione

Answer

That's not a hostname:

hostname = "username@hobbes.cs.ucsb.edu"

Instead, that's a connection string. Remove the username@ section, and it should connect again.

Further Information

Remember, you can always look at the source code. Here, you can see that hostname is passed directly into the raw socket call:

socket.getaddrinfo(hostname, port, socket.AF_UNSPEC, socket.SOCK_STREAM)

Looking at the help for socket.getaddrinfo, we can see it's trying to resolve an actual hostname, similar to the syntax required for 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.

Lastly, I would recommend looking at enabling debugging in paramiko, and other underlying libraries:

>>> import logging
>>> logger = paramiko.util.logging.getLogger()
>>> logger.setLevel(logging.DEBUG)
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top