Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Factored connect_forward function our of monkeypatch.py and into netw…

…ork.py
  • Loading branch information...
commit ee3c9f51555c7b6de4f1152b0c35d9873955451f 1 parent 4708ba1
Erwin Bolwidt authored
Showing with 51 additions and 58 deletions.
  1. +1 −58 fabric/monkeypatch.py
  2. +50 −0 fabric/network.py
View
59 fabric/monkeypatch.py
@@ -1,64 +1,7 @@
-import sys
-import socket
-
-from forward_ssh import ForwardSSHClient
-import getpass
-import paramiko as ssh
-from paramiko.resource import ResourceManager
from fabric import network
from fabric import state as s
-def connect_forward(gw, host, port, user):
- """
- Create a different connect that works with a gateway. We really need to
- create the socket and destroy it when the connection fails and then retry
- the connect.
- """
- client = ForwardSSHClient()
- while True:
- # Load known host keys (e.g. ~/.ssh/known_hosts) unless user says not to.
- if not s.env.disable_known_hosts:
- client.load_system_host_keys()
- # Unless user specified not to, accept/add new, unknown host keys
- if not s.env.reject_unknown_hosts:
- client.set_missing_host_key_policy(ssh.AutoAddPolicy())
-
- sock = gw.get_transport().open_channel('direct-tcpip', (host, int(port)), ('', 0))
- try:
- client.connect(host, sock, int(port), user, s.env.password,
- key_filename=s.env.key_filename, timeout=10)
- client._sock_ = sock
- return client
- except (
- ssh.AuthenticationException,
- ssh.PasswordRequiredException,
- ssh.SSHException
- ), e:
- if e.__class__ is ssh.SSHException and s.env.password:
- network.abort(str(e))
-
- s.env.password = network.prompt_for_password(s.env.password)
- sock.close()
-
- except (EOFError, TypeError):
- # Print a newline (in case user was sitting at prompt)
- print('')
- sys.exit(0)
- # Handle timeouts
- except socket.timeout:
- network.abort('Timed out trying to connect to %s' % host)
- # Handle DNS error / name lookup failure
- except socket.gaierror:
- network.abort('Name lookup failed for %s' % host)
- # Handle generic network-related errors
- # NOTE: In 2.6, socket.error subclasses IOError
- except socket.error, e:
- network.abort('Low level socket error connecting to host %s: %s' % (
- host, e[1])
- )
-
-
class GatewayConnectionCache(network.HostConnectionCache):
_gw = None
def __getitem__(self, key):
@@ -78,7 +21,7 @@ def __getitem__(self, key):
# If not found, create new connection and store it
if real_key not in self:
- self[real_key] = connect_forward(self._gw, host, port, user)
+ self[real_key] = network.connect_forward(self._gw, host, port, user)
# Return the value either way
return dict.__getitem__(self, real_key)
View
50 fabric/network.py
@@ -265,6 +265,56 @@ def connect(user, host, port):
host, e[1])
)
+def connect_forward(gw, host, port, user):
+ """
+ Create a different connect that works with a gateway. We really need to
+ create the socket and destroy it when the connection fails and then retry
+ the connect.
+ """
+ from state import env
+ from forward_ssh import ForwardSSHClient
+ client = ForwardSSHClient()
+ while True:
+ # Load known host keys (e.g. ~/.ssh/known_hosts) unless user says not to.
+ if not env.disable_known_hosts:
+ client.load_system_host_keys()
+ # Unless user specified not to, accept/add new, unknown host keys
+ if not env.reject_unknown_hosts:
+ client.set_missing_host_key_policy(ssh.AutoAddPolicy())
+
+ sock = gw.get_transport().open_channel('direct-tcpip', (host, int(port)), ('', 0))
+ try:
+ client.connect(host, sock, int(port), user, env.password,
+ key_filename=env.key_filename, timeout=10)
+ client._sock_ = sock
+ return client
+ except (
+ ssh.AuthenticationException,
+ ssh.PasswordRequiredException,
+ ssh.SSHException
+ ), e:
+ if e.__class__ is ssh.SSHException and env.password:
+ abort(str(e))
+
+ env.password = prompt_for_password(env.password)
+ sock.close()
+
+ except (EOFError, TypeError):
+ # Print a newline (in case user was sitting at prompt)
+ print('')
+ sys.exit(0)
+ # Handle timeouts
+ except socket.timeout:
+ abort('Timed out trying to connect to %s' % host)
+ # Handle DNS error / name lookup failure
+ except socket.gaierror:
+ abort('Name lookup failed for %s' % host)
+ # Handle generic network-related errors
+ # NOTE: In 2.6, socket.error subclasses IOError
+ except socket.error, e:
+ abort('Low level socket error connecting to host %s: %s' % (
+ host, e[1])
+ )
def prompt_for_password(prompt=None, no_colon=False, stream=None):
"""
Please sign in to comment.
Something went wrong with that request. Please try again.