0

In one section of a bash script, I need to ssh into a remote box, change to root, then deploy an rpm. My code is getting stuck after it changes to root and I'm not sure why. Any help would be greatly appreciated. Ideally I would like it to ssh in, switch to root, deploy the rpm, then exit the ssh session.

user="user"
host="hostname"
echo "Enter password: "
read -s pw
ssh -t "$user"@"$host" "sudo su; $pw; rpm -Uvh rpmtodeploy.rpm;"

This is what I get currently:

user@localhost:~$ bash rpm.sh

Enter password:

[root@hostname /home/user]#

cakes88
  • 1,857
  • 5
  • 24
  • 33

1 Answers1

1

Unless you background a command, bash waits for it to exit before executing the next command. Since su is spawning an interactive shell, it will continue to run until you close it. You should use sudo directly:

sudo rpm -Uvh rpmtodeploy.rpm

If you have sudo access to su but not to rpm, use the -c option:

sudo su -c 'rpm -Uvh rpmtodeploy.rpm'
jordanm
  • 33,009
  • 7
  • 61
  • 76
  • How would I pass my password through the first command? sudo rpm -Uvh rpmtodeploy.rpm – cakes88 Nov 21 '13 at 23:36
  • @Konnor You don't. Configure `sudo` to allow `NOPASSWD` for the `rpm` command. If you are unable to do that, you will have to use an `expect` script. – jordanm Nov 21 '13 at 23:38
  • Ah ok, I was looking into that earlier. Thanks @jordanm – cakes88 Nov 21 '13 at 23:40