0

I need to perform some operation in a remote machine in which first I login as userA then change to userB

ssh userA@remoteHost   
userA$ 'sudo su userB'

Type password of userA to change to userB. Do other operations as userB

I want to achieve this by running a script from my local machine to the remote machine.

localUser$ ssh userA@remoteHost 'bash -s' < script

Inside the script, some thing like this :

#!/bin/bash
sudo su userB

....
perform other operations as userB

At this moment when I run this script it asks for userA password to run the script first. Then asks for password for userA to switch to userB. But I am not able to input anything as the prompts do not persist. How to achieve this ?

saurav
  • 359
  • 1
  • 6
  • 18

2 Answers2

0

But I am not able to input anything as the prompts do not persist. How to achieve this ?

You already directed script to the stdin of the ssh command so the script is trying to read what is in script (but before flushes what is left there, which is a good practice for reading passwords interactively.

If you want to automate interactive actions, use expect script. If you want to use the script above, copy it first to the server and then run it there as

localUser$ ssh -t userA@remoteHost "./script"
Jakuje
  • 24,773
  • 12
  • 69
  • 75
0

Instead of sudo su userB, you can use sudo -S su - userB

Roopak A Nelliat
  • 2,009
  • 3
  • 19
  • 26