Reputation: 99
Problem: I am attempting to create a set of scripts that will prompt the user for an IP/hostname & password (both I have access to) & then SSH into the VM. The second script runs a set of commands to baseline the environment with various updates and services.
When run locally, this script works correctly. It displays the hostname and asks for verification to run the updates on that environment.
-------------------baseline.sh
#!/bin/bash
#filename: baseline.sh
echo "Are you sure you want to run your script on $(hostname -f)?"
OPTIONS="yes no"
select opt in $OPTIONS; do
if [ "$opt" = "yes" ]; then
echo Running Script...
#do script things
exit
elif [ "$opt" = "no" ]; then
echo Canceling Run...
exit
else
echo Please Input Either the # 1 or 2.
fi
done
Here is the output:
is-mbp-jsmith:Setting Up Dev Env's jsmith $ sh baseline.sh
Are you sure you want to run your script on is-mbp-jsmith.somecompany.com?
1) yes
2) no
#? 1
Running Script...
This scripts is working as intended on my local machine
-------------------------------------------modifyDevEnv.sh
Here is the script I am using to pipe the script above into a VM.
#!/bin/bash
#filename: modifyDevEnv.sh
echo Enter a hostname/IP for the Dev Environment to connect to.
read HOST
ssh $HOST -l root 'bash -s' < baseline.sh
Here is the output I get when running this script:
is-mbp-jsmith:Setting Up Dev Env's jsmith $ sh modifyDevEnv.sh
Enter a hostname/IP for the Dev Environment to connect to.
10.58.88.53
[email protected]'s password:
Are you sure you want to run your script on vm-jsmith.somecompany.com?
1) yes
2) no
#? 1) yes
2) no
#? is-mbp-jsmith:Setting Up Dev Env's jsmith $
when run over ssh, the script looks like it runs the option command twice and then immediately ends. It never gives allows the user to input an option for yes/no
I dont know if it has something to do with asking for user input over ssh that the script doesnt like, but this issue has me stumped. I'm fairly new to Bash (first actual script I'm attempting) but I dont see anything wrong with the syntax. Is there something I am missing?
Upvotes: 0
Views: 1549
Reputation: 123410
Here's a simpler way of reproducing your problem:
$ cat script
read -p "Enter name: " name
echo "Hello $name"
$ bash script
Enter name: World
Hello World
$ ssh localhost 'bash -s' < script
Enter name:
Hello
Connection to localhost closed.
This happens because <
means "read from this file instead of from the terminal".
The simplest way to run a local script remotely without copying it is embedding it in the ssh command:
$ ssh -t localhost "$(< script)"
Enter name: World
Hello World
Connection to localhost closed.
Note that this will run the script in the login shell, rather than in whatever the shebang specifies.
Upvotes: 2
Reputation: 530882
You have two different processes reading from the same input stream: bash -s
, and the script that bash -s
is executing. It will be much simpler to just copy the script to the remote machine, then executing it with standard input tied to the terminal.
#!/bin/bash
#filename: modifyDevEnv.sh
echo Enter a hostname/IP for the Dev Environment to connect to.
read HOST
scp baseline.sh root@$HOST:
ssh $HOST -l root basline.sh
Upvotes: 1