Skip to content

Commit

Permalink
Merge 42e14b8 into 50205e3
Browse files Browse the repository at this point in the history
  • Loading branch information
Red-M committed May 29, 2018
2 parents 50205e3 + 42e14b8 commit 1ab8475
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
22 changes: 15 additions & 7 deletions pexpect/pxssh.py
Expand Up @@ -259,7 +259,7 @@ def login (self, server, username, password='', terminal_type='ansi',
sync_multiplier=1, check_local_ip=True,
password_regex=r'(?i)(?:password:)|(?:passphrase for key)',
ssh_tunnels={}, spawn_local_ssh=True,
sync_original_prompt=True):
sync_original_prompt=True, ssh_config=None):
'''This logs the user into the given server.
It uses
Expand Down Expand Up @@ -294,8 +294,15 @@ def login (self, server, username, password='', terminal_type='ansi',
session to do so. Setting this option to `False` and not having an active session
will trigger an error.
Set ``ssh_key`` to `True` to force passing the current SSH authentication socket to the
Set ``ssh_key`` to a file path to an SSH private key to use that SSH key
for the session authentication.
Set ``ssh_key`` to `True` to force passing the current SSH authentication socket
to the desired ``hostname``.
Set ``ssh_config`` to a file path string of an SSH client config file to pass that
file to the client to handle itself. You may set any options you wish in here, however
doing so will require you to post extra information that you may not want to if you
run into issues.
'''

session_regex_array = ["(?i)are you sure you want to continue connecting", original_prompt, password_regex, "(?i)permission denied", "(?i)terminal type", TIMEOUT]
Expand All @@ -310,18 +317,19 @@ def login (self, server, username, password='', terminal_type='ansi',
ssh_options = ssh_options + " -o'NoHostAuthenticationForLocalhost=yes'"
if self.force_password:
ssh_options = ssh_options + ' ' + self.SSH_OPTS
if ssh_config is not None:
if spawn_local_ssh and not os.path.isfile(ssh_config):
raise ExceptionPxssh('SSH config does not exist or is not a file.')
ssh_options = ssh_options + '-F ' + ssh_config
if port is not None:
ssh_options = ssh_options + ' -p %s'%(str(port))
if ssh_key is not None:
# Allow forwarding our SSH key to the current session
if ssh_key==True:
ssh_options = ssh_options + ' -A'
else:
try:
if spawn_local_ssh:
os.path.isfile(ssh_key)
except:
raise ExceptionPxssh('private ssh key does not exist')
if spawn_local_ssh and not os.path.isfile(ssh_key):
raise ExceptionPxssh('private ssh key does not exist or is not a file.')
ssh_options = ssh_options + ' -i %s' % (ssh_key)

# SSH tunnels, make sure you know what you're putting into the lists
Expand Down
14 changes: 11 additions & 3 deletions tests/test_pxssh.py
@@ -1,6 +1,6 @@
#!/usr/bin/env python
import os

import tempfile
import unittest

from pexpect import pxssh
Expand Down Expand Up @@ -85,6 +85,13 @@ def test_remote_ssh_tunnel_string(self):
if confirmation_strings!=len(confirmation_array):
assert False, 'String generated from remote tunneling is incorrect.'

def test_ssh_config_passing_string(self):
ssh = pxssh.pxssh(debug_command_string=True)
(temp_file,config_path) = tempfile.mkstemp()
string = ssh.login('server', 'me', password='s3cret', spawn_local_ssh=False, ssh_config=config_path)
if not '-F '+config_path in string:
assert False, 'String generated from SSH config passing is incorrect.'

def test_ssh_key_string(self):
ssh = pxssh.pxssh(debug_command_string=True)
confirmation_strings = 0
Expand All @@ -98,8 +105,9 @@ def test_ssh_key_string(self):
assert False, 'String generated from forcing the SSH agent sock is incorrect.'

confirmation_strings = 0
confirmation_array = [' -i True']
string = ssh.login('server', 'me', password='s3cret', ssh_key='True')
(temp_file,ssh_key) = tempfile.mkstemp()
confirmation_array = [' -i '+ssh_key]
string = ssh.login('server', 'me', password='s3cret', ssh_key=ssh_key)
for confirmation in confirmation_array:
if confirmation in string:
confirmation_strings+=1
Expand Down

0 comments on commit 1ab8475

Please sign in to comment.