fab-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

how to run commands after ssh to remote host and then sudo -iu <another


From: Yongqin Xu
Subject: how to run commands after ssh to remote host and then sudo -iu <another user>
Date: Sun, 13 Feb 2022 11:30:01 -0800

Hello everyone,

Very new to use fabric, i am writing a python cli that need to ssh into remote host with pkey
of current login user1, then with that opened connection, it will need to sudo -iu <user2> so that
it can issue commands like sqlplus / as sysdba, the cli need to interact with some sql commands
output. So far, my cli can:
1)  ssh to remote host with user1 pkey and open a connection
2)  issue sudo -iu <user2> with provided password, and see the remote host console output as
    login user2

but from there, any commands issued from the current connection will not interact with remote
user2 session anymore until i manually exit from the user2 session.  So my question is: how do
i use the current connection to issue command after sudo login into user2 session? Sample code:

try:
ssh_conn.open()
except SSHException as ex:
raise click.ClickException(f'Unable to open SSH connection: {ex}')

oracle_sudo_pass = getpass(prompt='Oracle sudo password: ')
sudopass = Responder(pattern=r'Password:', response=oracle_sudo_pass + '\n',)
response = ssh_conn.run('sudo -iu oracle', pty=True, watchers=[sudopass])
ssh_conn.run('whoami\n')
after running ssh_conn.run('sudo -iu oracle') call, i can not issue any command from ssh_conn
until i manually exit from user oracle session, then command `whoami` will print out user1, not
oracle user.

So any idea how to achive this? any help will be greatly appreciated!

Phil





reply via email to

[Prev in Thread] Current Thread [Next in Thread]