mwilliamson / spur.py

Run commands and manipulate files locally or over SSH using the same interface
BSD 2-Clause "Simplified" License
267 stars 37 forks source link

Connection Refused - VirtualBox #3

Closed akloster closed 11 years ago

akloster commented 11 years ago

I am trying to connect via ssh to a virtualbox. I have set up the vm to accept password and key authentication. The access is via port forwarding, e.g. localhost port 2222 to port 22 inside the guest machine.

Logging in via port 2222 works from bash, both with password and key file.

Using spur does not work. I am getting a "111: Connection refused".

I began using the original simple examples from the readme, then proceeded to try various things as mentioned in #2 .

Any further ideas?

Thank you for your time.

mwilliamson commented 11 years ago

Could you paste in the code you're using to invoke spur, and the manual command from bash that's successful?

akloster commented 11 years ago

Python code:

shell = spur.SshShell(hostname="localhost", port=2222, username="MASKED", private_key_file="MASKED")
with shell:
    result = shell.run(["echo", "-n", "hello"])
    print result.output

and ssh user@localhost -p 2222

mwilliamson commented 11 years ago

Have you tried using "127.0.0.1" instead of "localhost" for the hostname?

Can you grab the traceback from the "Connection refused" error? Something similar to the following should do the trick:

import spur
import spur.ssh

try:
    shell = spur.SshShell(
        hostname="localhost",
        port=2222,
        username="MASKED",
        private_key_file="MASKED"
    )
    with shell:
        result = shell.run(["echo", "-n", "hello"])
        print result.output
except spur.ssh.ConnectionError as error:
    print error.original_traceback
    raise
akloster commented 11 years ago

Actually, with 127.0.0.1 it works. But it's strange that this would work, while the other works only with 22, not with 2222.

Here are the tracebacks:

Traceback (most recent call last):
  File "/home/andi/.virtualenvs/plan/local/lib/python2.7/site-packages/spur/ssh.py", line 178, in _get_ssh_transport
    return self._connect_ssh().get_transport()
  File "/home/andi/.virtualenvs/plan/local/lib/python2.7/site-packages/spur/ssh.py", line 195, in _connect_ssh
    timeout=self._connect_timeout
  File "/home/andi/.virtualenvs/plan/local/lib/python2.7/site-packages/paramiko/client.py", line 301, in connect
    retry_on_signal(lambda: sock.connect(addr))
  File "/home/andi/.virtualenvs/plan/local/lib/python2.7/site-packages/paramiko/util.py", line 278, in retry_on_signal
    return function()
  File "/home/andi/.virtualenvs/plan/local/lib/python2.7/site-packages/paramiko/client.py", line 301, in <lambda>
    retry_on_signal(lambda: sock.connect(addr))
  File "/usr/lib/python2.7/socket.py", line 224, in meth
    return getattr(self._sock,name)(*args)
error: [Errno 111] Connection refused

Traceback (most recent call last):
  File "deploy.py", line 20, in <module>
    result = shell.run(["echo", "-n", "hello"])
  File "/home/andi/.virtualenvs/plan/local/lib/python2.7/site-packages/spur/ssh.py", line 72, in run
    return self.spawn(*args, **kwargs).wait_for_result()
  File "/home/andi/.virtualenvs/plan/local/lib/python2.7/site-packages/spur/ssh.py", line 82, in spawn
    channel = self._get_ssh_transport().open_session()
  File "/home/andi/.virtualenvs/plan/local/lib/python2.7/site-packages/spur/ssh.py", line 180, in _get_ssh_transport
    raise self._connection_error(error)
spur.ssh.ConnectionError: Error creating SSH connection
Original error: [Errno 111] Connection refused´´´
mwilliamson commented 11 years ago

Actually, with 127.0.0.1 it works.

I've hit the same issue, but I'm not entirely sure what causes it. Regardless, it sounds like things are working, so I'll close the issue.