skip to Main Content

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


  1. Please try something like this

    ssh -t [email protected] << EOF
     command1
     command2
     exit
    EOF
    

    OR

    SSHPASS=password sshpass -e ssh -tt -o "StrictHostKeyChecking no" [email protected] 'echo "password" | sudo -Sv && bash -s' < script.sh && exit
    
    
    Login or Signup to reply.
  2. 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 that ssh passes across the connection via standard input. Something like

    SSHPASS=password sshpass -e ssh [email protected] 
      '{ echo "password"; cat; }| sudo -Sv bash' < script.sh
    

    The cat ensures that everything ssh sends over the connection is read by sudo after it reads the password from standard input.


    What was wrong

    Your first command

    SSHPASS=password sshpass -e ssh 
                             -tt 
                             -o "StrictHostKeyChecking no" 
                             [email protected] 'echo "password" |
      sudo -Sv &&
    bash -s' < script.sh
    

    starts by running sshpass. It logs into the remote host, runs echo "password", then logs out. That output is piped to sudo -Sv, which, having no command to run, just ignores its input and exits successfully. Then bash runs the code read from your script locally.

    Your second command,

    cat script.sh | 
      SSHPASS=password sshpass -e ssh -tt [email protected] 'echo "password" |
      sudo -Sv &&
    bash -s'
    

    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, starting bash -s which blocks, waiting for input.


    Incidentally, your script can be simplified to

    cd /etc/sysconfig/network-scripts
    {
      sed -e 's/BOOTPROTO="dhcp"/BOOTPROTO="static"/g' 
          -e 's/NAME="ens3"/NAME="ens3:0"/g' 
          -e 's/DEVICE="ens3"/DEVICE="ens3:0"/g' ifcfg-ens3
      echo 'PEERDNS="yes"'
      echo 'IPADDR=192.168.122.201'
      echo 'NETMASK=255.255.255.0'
      echo 'GATEWAY=192.168.122.1'
    } > ifcfg-ens3:0
    systemctl restart network
    

    No sudo (because the script is executed with sudo), and one sed command (reading from the original file) and multiple echo all writing to the same output file.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search