Search code examples
pythonlinuxstdinrootparallel-ssh

How to execute 'su' command using parallel-ssh


I want to log in to two hosts using parallel-ssh and execute su command. Then I want to confirm that I am the root user by printing out whoami

Code:

hosts = ['myHost1', 'myHost2']
client = ParallelSSHClient(hosts, user='myUser', password='myPassword')

output = client.run_command('su')

for host in output:
    stdin = output[host].stdin
    stdin.write('rootPassword\n')
    stdin.flush()

client.join(output)

output = client.run_command('whoami')

for host, host_output in output.items():
    for line in host_output.stdout:
        print("Host [%s] - %s" % (host, line))

Result:

Host [myHost1] - myUser
Host [myHost2] - myUser

Obviously, I expect root in the output. I am following the documentation.

I've tried using all different line endings instead of \n and nothing has changed. How can I execute su command using parallel-ssh?


Solution

  • It turns out that what I am trying to do is not achievable.

    The first problem

    I found in this post that all commands are in their own channel. That means that even if su would be successful it wouldn't affect the second command. The author of the post recommends running

    su -c whoami - root
    

    The second problem

    I managed to debug the problem even further by changing host_output.stdout to host_output.stderr It turned out that I receive an error which previously was not being shown on the terminal:

    standard in must be a tty
    

    Possible solutions to this problem are here . They didn't work for me but might work for you.

    For me workaround was to allow on all my hosts root login. And then in parallel-ssh I log in as a root already with all the rights in place.