Get command prompt via "sudo su -" then run command using Paramiko exec_command

77
April 09, 2022, at 3:20 PM

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)
Answer 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:

  • Execute multiple dependent commands individually with Paramiko and find out when each command finishes
  • Combining interactive shell and recv_exit_status method using Paramiko
Rent Charter Buses Company
READ ALSO
How to iterate mutli-dimensional arrays in Python?

How to iterate mutli-dimensional arrays in Python?

CommunityI'm working with a Pandas Dataframe and I've reached a point where I need to sample two columns from the DataFrame

91
How do you connect to an existing database table in peewee?

How do you connect to an existing database table in peewee?

Much of the tutorials I've read show how to connect to the database and create a new tableMaybe I am wrong or misunderstanding , Ive reading the documents here: peewee offical

106
Run hive queries on empty dataframe using pyspark

Run hive queries on empty dataframe using pyspark

I want to test my complex hive queries beforehand by executing on empty dataframes using pyspark or pandasHow can I do this

98
Trying to create filters for images, getting integer argument expected errors, screenshots and code included

Trying to create filters for images, getting integer argument expected errors, screenshots and code included

so I am taking a Python course on Codio and the current topic is image manipulation, with the assignment being to create certain filters for images

186