I have this update script i am working on and i am using ssh to log onto the machine and simply run apt-get update && apt-get upgrade -y. Now the thing is i want to be able to say something like "hostname" updated successfully.
Here's what i have written right now:
#!/bin/bash
ip=(192.168.1.23 192.168.1.40 192.168.1.41 192.168.1.42 192.168.1.43)
#ssh-key=~/.ssh/id_ed25519
for i in "${ip[@]}"; do {
ssh -t victor@$i "sudo apt-get update && sudo apt-get upgrade -y"
if [ $? -eq 0 ]; then
echo "Update went successfully";
else
echo "$(tput smul)$(tput setaf 1)Update failed$(tput rmul)";
exit 1;
fi
} done;
with the same ssh -t command i was thinking if i could get the hostname and assign it to a variable at the same time i'm using the first ssh session. That is so i dont have to write in my password for the ssh key twice.
So something like this:
hostname=$"(ssh -t victor@$i hostname && sudo apt-get update && sudo apt-get upgrade -y"
And then i just echo $hostname.
if [ $? -eq 0 ], it's better just to put thesshcommand into theifstatement in-line:if ssh -t "victor<$i" "..."; thenhostname=${hostname%$'\r'}, not the exit status of thesshcommand (and if you just removed thehostname=line without making any other changes, you'd be checking the exit status ofcat). Please adopt the edits in the current version of my answer if you want to detect failedapt-getcommands reliably.