2
votes

I'm using Paramiko (newbie) to connect to a linux server. I'm able to connect to the server an execute different commands (such as 'ls', 'uptime') and to read the results. However, when trying to execute the 'sudo -iu' command (doesn't require a password), the Paramiko hangs. Here is the relevant code lines:

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy (paramiko.AutoAddPolicy())
sh.connect (server, username='aaaa', password= 'bbbbb', key_filename='C:\\Users\\aaaa\\Documents\\paramiko_dsa_key')
        stdin, stdout, stderr = ssh.exec_command("sudo -iu aaauser")
        #stdin, stdout, stderr = ssh.exec_command("uptime")
        print stdout.read()

Note, that there are no issues running these commands using known SSH tools such as Xshell.

Thanks in advance, Tal

1

1 Answers

3
votes

Paramiko hanging is exactly what would be expected. When you do sudo -i, a new shell is spawned and the current shell goes into I/O wait until the newly spawned shell exits, at which time control would be turned back to Paramiko.

Running sudo -i would thus never result in output to stdout, and so your loop would be stuck in I/O wait, and thus hang, as you are currently experiencing. sudo -i should never be used in scripts, it should only be used for interactive shells.

You have two options:

  1. Configure the SSH daemon to allow logins as the user aaauser and avoid this problem in the first place.
  2. Execute each command using sudo in the normal, non-interactive fashion.