3

The goal is to change some parameters on a lot of devices on connections that are kind of unreliable (wireless)--according to csv that has the format: IP,Parameter,Parameter

I would like to make a bash script that uploads a file, then checks the contents of it against the local copy, then run other commands using only bash, ssh, cat, and diff on the remote end. Only basic tools are available on the remote linux servers (Busybox), and there are thousands of them, so scaling is important. I would very much prefer to do all of this in a single SSH session, where authentication only has to happen one way, and one time. Pretend for the moment that security is not an issue. I am looking into how to scale rsa public key distribution to all of these, but for now, I want this to work with password authentication. Please don't make this into a banter about security practices. Perhaps there is a much easier way to accomplish the goal mentioned above. If so, please share. Thanks.

So the steps are:

  1. Log into the remote box.
  2. Copy the local file to the remote box in a specific directory.
  3. Run diff, or some type of check for file copy errors.
  4. If error, stop script, otherwise continue.
  5. Run other commands on remote box.
  6. Output the results of the commands to a local file.
  7. Close the session.
  8. Rinse and repeat.

Below is sort of pseudocode. But it gives you an idea of what I am trying to do. diff does not see the output of cataloging the local file, so it thinks they are not the same.

IFS=\,
while read IP PARAM1 PARAM2; do

cat /home/user/uploadscript.sh | sshpass -p $password ssh -v -o StrictHostKeyChecking=no -l uname 192.168.x.x 'cat > /tmp/script.sh && cat | diff -sb - /tmp/script.sh && IP="IP" && P1="$PARAM1" && P2="$PARAM2" && ./script.sh'

done < list_of_ipaddresses.txt >> log.txt
  • 1
    Run diff, or some type of check for file copy errors. -- not needed. SSH checks integrity of transferred messages. Only if you don't believe the filesystem below. You can use ControlMaster to make your code less awful. – Jakuje Feb 26 '16 at 9:35
  • Yeah, I wasn't sure if I needed diff. Wow, ControlMaster! I think this is the answer! Googling how to use it now. Also I read about ssh-agent which looks useful for key auth. Thank you for your response! And, sorry to have you look at my awful code. – Matt D Feb 26 '16 at 16:37
0

The only command you need is scp. Like any other command, scp returns the status code 0 to indicate success or nonzero to indicate an error. If scp returns 0, the file was uploaded successfully.

if scp …; then
  echo >&2 "The file was uploaded successfully."
else
  echo >&2 "There was an error uploading the file."
fi

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.