简体   繁体   中英

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 ?

Try this:

**def exec_command(hosts):
    strr = ""
    client = ParallelSSHClient(hosts, user='admin', password='admin_password')
    cmd = 'echo root_password |su -c "commmand" root'
    output = client.run_command(cmd)
    client.join()
    for host_out in output:
    for line in host_out.stdout:
        strr+=line+" "
    return strr
    **

'echo root_password |su -c "command" root'

try to put sudo=True at the end of run_command

output = client.run_command(<..>, sudo=True)

like in docs

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

The first problem

I found inthis 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM