I am running a CentOS 7 VM that I want to set a static IP to.
Running SSHPASS=password sshpass -e ssh -tt -o "StrictHostKeyChecking no" [email protected] 'echo "password" | sudo -Sv && bash -s' < script.sh
executes all the commands in script.sh
but, after completion, the terminal stays inside the VM, irregardless of whether I have the exit
command at the end of my bash script or not.
The reason for piping my password is to skip entering it in the terminal.
Here is my bash script:
sudo cp /etc/sysconfig/network-scripts/ifcfg-ens3 /etc/sysconfig/network-scripts/ifcfg-ens3:0
sudo sed -i 's/BOOTPROTO="dhcp"/BOOTPROTO="static"/g' /etc/sysconfig/network-scripts/ifcfg-ens3:0
sudo sed -i 's/NAME="ens3"/NAME="ens3:0"/g' /etc/sysconfig/network-scripts/ifcfg-ens3:0
sudo sed -i 's/DEVICE="ens3"/DEVICE="ens3:0"/g' /etc/sysconfig/network-scripts/ifcfg-ens3:0
echo 'PEERDNS="yes"' | sudo tee -a /etc/sysconfig/network-scripts/ifcfg-ens3:0
echo 'IPADDR=192.168.122.201' | sudo tee -a /etc/sysconfig/network-scripts/ifcfg-ens3:0
echo 'NETMASK=255.255.255.0' | sudo tee -a /etc/sysconfig/network-scripts/ifcfg-ens3:0
echo 'GATEWAY=192.168.122.1' | sudo tee -a /etc/sysconfig/network-scripts/ifcfg-ens3:0
sudo systemctl restart network
exit
Ideally, I would like to execute these commands in the same script but I can’t seem to figure out what the problem is.
Interesting note: if I run cat script.sh | SSHPASS=password sshpass -e ssh -tt [email protected] 'echo "password" | sudo -Sv && bash -s'
the console stops on the exit
command (not inputted) and if I press enter to input it, the VM stalls and I can only exit with CTRL+C.
UPDATE
If I run ./script.sh
that has the following contents…
ssh -tt [email protected] 'echo "password" | sudo -Sv && bash -s' << EOF
echo
sudo cp /etc/sysconfig/network-scripts/ifcfg-ens3 /etc/sysconfig/network-scripts/ifcfg-ens3:0
sudo sed -i 's/BOOTPROTO="dhcp"/BOOTPROTO="static"/g' /etc/sysconfig/network-scripts/ifcfg-ens3:0
sudo sed -i 's/NAME="ens3"/NAME="ens3:0"/g' /etc/sysconfig/network-scripts/ifcfg-ens3:0
sudo sed -i 's/DEVICE="ens3"/DEVICE="ens3:0"/g' /etc/sysconfig/network-scripts/ifcfg-ens3:0
echo 'PEERDNS="yes"' | sudo tee -a /etc/sysconfig/network-scripts/ifcfg-ens3:0
echo 'IPADDR=192.168.122.201' | sudo tee -a /etc/sysconfig/network-scripts/ifcfg-ens3:0
echo 'NETMASK=255.255.255.0' | sudo tee -a /etc/sysconfig/network-scripts/ifcfg-ens3:0
echo 'GATEWAY=192.168.122.1' | sudo tee -a /etc/sysconfig/network-scripts/ifcfg-ens3:0
exit
EOF
Everything works. Which leads me to believe that the problem is related to sudo systemctl restart network
. Confirmed by adding sudo systemctl restart network; exit
at the end of the script. Maybe it has something to do with the nature of this command.
2
Answers
Please try something like this
OR
Your goal is to edit a file in your VM. Neither of your commands runs anything except
echo "password"
on the VM.What (should) work
The following isn’t tested. What you want is to have
sudo
run remotely, reading from the script thatssh
passes across the connection via standard input. Something likeThe
cat
ensures that everythingssh
sends over the connection is read bysudo
after it reads the password from standard input.What was wrong
Your first command
starts by running
sshpass
. It logs into the remote host, runsecho "password"
, then logs out. That output is piped tosudo -Sv
, which, having no command to run, just ignores its input and exits successfully. Thenbash
runs the code read from your script locally.Your second command,
pipes the contents of your script to
sshpass
, which again logs into the remote host and executes the command, ignoring your script, and exiting.sudo
then ignores its input and exits successfully, startingbash -s
which blocks, waiting for input.Incidentally, your script can be simplified to
No
sudo
(because the script is executed withsudo
), and onesed
command (reading from the original file) and multipleecho
all writing to the same output file.