Background: I am using python and paramiko to automate the process I go through everytime I have to hand in a program for a class. We use a command called "handin" to submit source code, but this must be done from a school computer. So when I submit code from home, I have to: sftp into school servers, put the files in a dir, ssh into school computer, use 'handin' command
I can successfully put files onto the school machines. The problem occurs when I try to use exec_command('handin my files') and then read the output to determine the next action.
so I have
try:
(stdin, stdout, stderr) = client.exec_command(s)
except:
print 'whoops'
sys.exit()
print stdout.readlines()
But this causes a deadlock for some reason, the script appears to be doing nothing and I have to eventually kill the entire process (ctrl+c doesnt work). Im not sure if exec_command is not completing correctly (even though it is getting out of the try/catch block) or if im having network problems or what.
Any ideas?
update:
The problem is with interacting with the handin command during execution. After executing the command, handin may or may not still be running. If its the first time submitting it says success, blah blah, and finishes executing. All is well. But if I am re-submitting I have to authorize an overwrite (stdin.write('y')) for each file.
TL/DR:
How do I check if a exec_command() is still running, waiting for input, and readline() from stdout accordingly?
Using exec_command() in Paramiko returns a tuple (stdin, stdout, stderr) . Most of the time, you just want to read stdout and ignore stdin and stderr . You can get the output the command by using stdout. read() (returns a string) or stdout.
Paramiko does not itself leverage OpenSSH-style config file directives, but it does implement a parser for the format, which users can honor themselves (and is used by higher-level libraries, such as Fabric).
A Paramiko SSH Example: Connect to Your Server Using a Password. This section shows you how to authenticate to a remote server with a username and password. To begin, create a new file named first_experiment.py and add the contents of the example file. Ensure that you update the file with your own Linode's details.
class paramiko.agent. AgentKey (agent, blob) Private key held in a local SSH agent. This type of key can be used for authenticating to a remote server (signing). Most other key operations work as expected.
I don't see anything wrong with the code snippit above. The program below is a complete script which logs into a host and runs the ls command to view files. I just tried it and it works for me. Perhaps try this and see if it works for you. If it does not work, I suspect some issue specific to either your ssh server, command you are running, or paramiko installation. If it does work for you, its just a matter of making changes to this to move towards your existing functionality and see where it breaks.
import paramiko
ssh=paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('<ip address here>',username='<username here>',password='<password here>')
stdin,stdout,stderr = ssh.exec_command("ls /")
print stdout.readlines()
If that works for you my next suggestion would be to try replacing the 'ls /' with the actual handin command you are trying to run. Its possible that command is hanging waiting for user input, etc.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With