@@ -72,7 +72,8 @@ class SSHClient(object):
7272
7373 def __init__ (self , host ,
7474 user = None , password = None , port = None ,
75- pkey = None ):
75+ pkey = None , forward_ssh_agent = True ,
76+ _agent = None ):
7677 """Connect to host honouring any user set configuration in ~/.ssh/config \
7778 or /etc/ssh/ssh_config
7879
@@ -89,6 +90,15 @@ def __init__(self, host,
8990 :type port: int
9091 :param pkey: (Optional) Client's private key to be used to connect with
9192 :type pkey: :mod:`paramiko.PKey`
93+ :param forward_ssh_agent: (Optional) Turn on SSH agent forwarding - \
94+ equivalent to `ssh -A` from the `ssh` command line utility.
95+ Defaults to True if not set.
96+ :type forward_ssh_agent: bool
97+ :param _agent: (Optional) Override SSH agent object with the provided. \
98+ This allows for overriding of the default paramiko behaviour of \
99+ connecting to local SSH agent to lookup keys with our own SSH agent.
100+ Only really useful for testing, hence the internal variable prefix.
101+ :type _agent: :mod:`paramiko.agent.Agent`
92102 :raises: :mod:`pssh.AuthenticationException` on authentication error
93103 :raises: :mod:`pssh.UnknownHostException` on DNS resolution error
94104 :raises: :mod:`pssh.ConnectionErrorException` on error connecting
@@ -113,6 +123,7 @@ def __init__(self, host,
113123 user = _user
114124 client = paramiko .SSHClient ()
115125 client .set_missing_host_key_policy (paramiko .MissingHostKeyPolicy ())
126+ self .forward_ssh_agent = forward_ssh_agent
116127 self .client = client
117128 self .channel = None
118129 self .user = user
@@ -125,6 +136,8 @@ def __init__(self, host,
125136 if self .proxy_command :
126137 logger .debug ("Proxy configured for destination host %s - ProxyCommand: '%s'" % (
127138 self .host , " " .join (self .proxy_command .cmd ),))
139+ if _agent :
140+ self .client ._agent = _agent
128141 self ._connect ()
129142
130143 def _connect (self , retries = 1 ):
@@ -177,6 +190,8 @@ def exec_command(self, command, sudo=False, **kwargs):
177190 stderr are buffers containing command output.
178191 """
179192 channel = self .client .get_transport ().open_session ()
193+ if self .forward_ssh_agent :
194+ agent_handler = paramiko .agent .AgentRequestHandler (channel )
180195 channel .get_pty ()
181196 # stdin (unused), stdout, stderr
182197 (_ , stdout , stderr ) = (channel .makefile ('wb' ), channel .makefile ('rb' ),
0 commit comments