20

When I run the command:

fab -H localhost host_type

I receive the following error:

[localhost] Executing task 'host_type'
[localhost] run: uname -s

Fatal error: Low level socket error connecting to host localhost: Connection refused

Aborting.

Any thoughts as to why? Thanks.

Fabfile.py

from fabric.api import run
def host_type():
    run('uname -s')

Configuration

  • Fabric 1.0a0 (installed from the most recent Github commit---b8e1b6a)
  • Paramiko 1.7.4
  • PyCrypto 2.0.1
  • Virtualenv ver 1.3.3
  • Python 2.6.2+ (release26-maint:74924, Sep 18 2009, 16:03:18)
  • Mac OS X 10.6.1
Matthew Rankin
  • 457,139
  • 39
  • 126
  • 163

4 Answers4

27

The important part isn't the "low level error" part of the message - the important part is the "Connection refused" part. You'll get a "connection refused" message when trying to connect to a closed port.

The most likely scenario is that you are not running an ssh server on your machine at the time that Fabric is running. If you do

ssh localhost

you'll probably get a message similar to

ssh: connect to host localhost: Connection refused

So you'll have to go out and set up an SSH server on your computer before you can proceed with Fabric from there.

Mark Rushakoff
  • 249,864
  • 45
  • 407
  • 398
  • You're welcome. Thanks for introducing me to Fabric - that might come in handy for me in the near future, actually. – Mark Rushakoff Sep 24 '09 at 02:25
  • Shouldn't Fabric be smart enough not to try to connect to localhost and just use the current session? – Marco Nov 02 '09 at 22:01
  • @Marco No it should not. It should do exactly what it is told and not try to second-guess the programmer. `run()` is explicitly a remote (connected) operation, with all the failure modes that entails. Use `local()` instead if you want to run something locally. – Chris Vest Nov 23 '09 at 00:03
  • 2
    I find it irksome that I can't polymorphically install my dev environment locally using the same fab:install command that I use to deploy to our servers. I'd to use the same script for both so that my dev env is as similar to our servers as I can make it. This seems to me like it should be a common use-case. Am I trying to do something unusual? – Jonathan Hartley Dec 12 '11 at 17:38
  • @JonathanHartley - I totally agree. Seems nutty to have to choose between writing two versions of every function or running an SSH server locally. – Alex Dean Apr 18 '12 at 17:36
  • 4
    @JonathanHartley There is (now) a [GitHub issue on this topic](https://github.com/fabric/fabric/issues/98). In the meantime, running the SSH server is actually very simple: first do `sudo apt-get install ssh` to make sure you have it installed (even if you think you do). Then do `sudo service ssh start`|`stop`|`restart` as needed. Cheers. – floer32 Jan 31 '13 at 21:58
  • 2
    @JonathanHartley as an alternative you could also set up your Fabfile to utilize a function like this, wherever you would have done `execute` or `run`: `def do(*args, **kwargs): _do = local if env.host_string == 'localhost' else run; _do(*args, **kwargs)`. It's not exactly the same thing, surely, but in some contexts it'll do. Not sure if it's a preferred way to do it, but the fun thing about fabfiles is that they're usually just yours to play with :) – floer32 Feb 03 '13 at 09:57
  • Also a good point. Thanks again! I'm pretty sure I'll be using both of your ideas within the week. Nice one. – Jonathan Hartley Feb 04 '13 at 10:24
  • To enable an ssh daemon on the Mac, go into System Preferences | Sharing and enable "Remote Login." – shacker Jul 19 '13 at 19:30
2

I had the same problem, but the reason was different: While I could easily log in to the server via SSH (default port 22), fabric tried to connect on a closed port 9090.

Finally I recognized that I had defined "env.port=9090" in my old fabfile for some WSGI server setup; while that was never a problem, I updated my Python installation some weeks before, and fabric now uses env.port for its SSH connection. I just renamed that config, and all is well again.

Hraban
  • 545
  • 5
  • 10
2

This can also happen in OS X 10.11.4 and Fabric 1.10.1, in the case where you are ssh'ing to a VM using Vagrant, which does port forwarding from localhost. In this case, localhost was resolving to the IPv6 ::1 address (not due to /etc/hosts file), and giving this error.

The fix was to force use of IPv4 by using the 127.0.0.1 address in the fabfile instead of the hostname. Using a hostname in /etc/hosts with this address didn't work.

You might also want to try these useful tips for debugging connection issues in Fabric.

RichVel
  • 7,030
  • 6
  • 32
  • 48
1
env.roledefs = {
'role1': env.hosts[0:5],
'role2':[env.hosts[5],]
}

I encountered the same error if "role" value IS NOT A LIST. for example, the above code works but the following doesn't.

 env.roledefs = {
'role1': env.hosts[0:5],
'role2':env.hosts[5],
}
ForceBru
  • 43,482
  • 10
  • 63
  • 98
weiwang
  • 393
  • 3
  • 10