0

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?

kukis
  • 4,489
  • 6
  • 27
  • 50

3 Answers3

1

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'

paone
  • 11
  • 1
0

try to put sudo=True at the end of run_command

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

like in docs

Druta Ruslan
  • 7,171
  • 2
  • 28
  • 38
  • There are some key differences between su and sudo. I want to execute su command. – kukis May 30 '18 at 05:39
  • @kukis to enter in `su` `superuser` you need to do `sudo su` and password – Druta Ruslan May 30 '18 at 06:19
  • sudo su doesn't work because for myUser I receive "Sorry, user myUser is not allowed to execute '/bin/su' as root on myHost." Simply, myUser is not in the group that can execute sudo. I need to log in as root (using root password, not myUser's password) – kukis May 30 '18 at 06:26
0

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.

kukis
  • 4,489
  • 6
  • 27
  • 50