1

Could you help me with following. I need to login as user then sudo su - then run command ex. parted -l using Paramiko exec_command when I run following code it hangs. Please let me know how I can check before gets to hanged.

with SSHClient() as client:
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    client.connect('172.20.xxx.xx', port=22, username='xxx', password='xxxxxxx',timeout=15.0)
    transport = client.get_transport()
    session = transport.open_session()
    session.set_combine_stderr(True)
    session.get_pty()
    session.exec_command('sudo su -')
    time.sleep(5)
    stdin = session.makefile('wb', -1)
    stdout = session.makefile('rb', -1)
    stderr = session.makefile_stderr('rb', -1)
    stdin.write('xxxxxxx' + '\n')
    stdin.flush()
    session.exec_command('parted -l')
    time.sleep(5)
    stdin = session.makefile('wb', -1)
    stdout = session.makefile('rb', -1)
    stderr = session.makefile_stderr('rb', -1)
    stdin.flush()
    for line in stdout.read().splitlines():
        print 'LINE: %s' % (line)
0

1 Answer 1

1

Do you want to check result of the individual parted command? If you want to check results of individual commands, do not run shell (su in this case). Run the command only:

sudo su -c "parted -l"

See also Executing command using "su -l" in SSH using Python

Then you can use recv_exit_status():
How can you get the SSH return code using Paramiko?


If you want to keep your current approach for whatever reason, see:

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.