LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Running multiple commands remotely via SSH in a script (https://www.linuxquestions.org/questions/linux-general-1/running-multiple-commands-remotely-via-ssh-in-a-script-776796/)

gimpy530 12-18-2009 09:45 PM

Running multiple commands remotely via SSH in a script
 
In a script I am writing I am trying to add logic so that the script can figure out if a remote server uses rpm or dpkg and then run the appropriate command to print a list of installed packages. This works locally, but I need to get it to work through SSH and I have no idea how to do that. The relevant portion of the script is below. It would also be nice to find a way to not need the full path to the executables but I'm not real concerned about that.

So anyone know how to make this code work via SSH?

Code:

if [ -x /usr/bin/dpkg ]; then
dpkg --get-selections
elif [ -x /bin/rpm ]; then
rpm -qa
else
echo "Line $LINENO - Unable to find rpm or dpkg command to create list of installed packages."
fi


AutoBot 12-18-2009 11:59 PM

I don't think your stating your problem properly, or at least I don't understand. You want your script to use ssh to retrieve the data from the servers?

AutoBot 12-19-2009 12:18 AM

Something like this, but you will need to supply the password or pub_key to the server but you get the idea?

Code:

SSHDPKG='ssh root@host -l username dpkg --get-selections'
SSHRPM=ssh root@host -l username rpm -qa'

if [ -x /usr/bin/dpkg ]; then
$SSHDPKG
elif [ -x /bin/rpm ]; then
$SSHRPM
else
echo "Line $LINENO - Unable to find rpm or dpkg command to create list of installed packages."
fi


gimpy530 12-19-2009 08:15 AM

Thanks AutoBot, but that wouldn't really do what I need. What I am trying to do is have a backup server talk to multiple remote servers, figure out whether the remote server has rpm or dpkg, then execute the appropriate command to list the installed packages, all on the remote client via SSH. With your code that would perform the if statement on the backup server, which is not what I want. I need the if statements AND rpm/dpkg commands to be ran on the remote client via SSH. I already have key based auth set up....except for the backup server can't SSH to itself using a key, but I'll fix that later.

The reason I am trying to do it this way is in my backup script I have a array which holds the backup sources ($LINBACKUPSRC). I then use a 'for' loop to perform the backup command ($RSYNC) on each machine in the array to move it to the backup server ($BACKUPDEST). This works perfectly with the rsync portion of my backup, but I wanted to add a list of installed packages to the backup of each machine as well. The relevant portions of my code are below so you can see what it is doing. This code works perfectly, I just want to add the ability to print a list of installed packages too and not have worry about the fact 'Indigo' uses dpkg and 'Teal' uses rpm, hence the 'if' statements to figure that out for me.

Am I just rambling or does that make any sense? If anyone wants the entire script, just ask and you can have it but there are minor problems in it still.

Code:

CURDATE=$(date +%m-%d-%y) #If the script runs past midnight, this will still show the date the run started since the date is set here and simply remembered for the remainder of the script.
                          #e.g. You start a script Monday evening and it runs until Tuesday morning, this will still show a date of Monday.  This is to keep logs consistent.
TIME='date +%r' #Change your time format here.
OUTLOG=/media/data/Logs/Backup/$CURDATE-STDOUT.log #Change log location and name here.
ERRLOG=/media/data/Logs/Backup/$CURDATE-STDERR.log #Change log location and name here.
LINOS='/etc /boot /var/log /home /root' #Change source directories for Linux machines here.
BACKUPDEST='white@192.168.1.150::data' #Change rsync destination here, Google and man pages are your friends for finding out how to set up an rsync daemon.
RSYNC='rsync -alpEAhR --stats --delete --exclude .gvfs' #Change rsync options here.
BACKUPUSER='backupuser' #Enter the user used by SSH here, see the "Prerequisites" section above for details.
LINBACKUPSRC=( "Indigo" "Teal" ) #Add Linux backup sources here.

Code:

#Backup Linux OS
for i in "${LINBACKUPSRC[@]}";do
echo "`$TIME` - Backing up $i - OS - START"|tee -a $OUTLOG $ERRLOG
        ssh $BACKUPUSER@$i sudo $RSYNC $LINOS $BACKUPDEST/Backup/$i 1>>$OUTLOG 2>>$ERRLOG
echo "`$TIME` - Backing up $i - OS - COMPLETE"|tee -a $OUTLOG $ERRLOG
done


gimpy530 12-19-2009 10:22 PM

Figured it out.

Here is the entire loop in the script:
Code:

#Backup Linux OS, add another elif line in the section below for another package managment system if you don't use rpm or dpkg, or adjust the path if needed.
for i in "${BKUPSRC[@]}";do
echo "`$TIME` - Backing up $i - OS - START"|tee -a $OUTLOG $ERRLOG
        ssh $SSHUSER@$i -p $SSHPORT "
    if [ -x /usr/bin/dpkg1 ]; then
        dpkg --get-selections 1>/home/backupuser/$CURDATE-Installed-Packages.log
    elif [ -x /bin/rpm1 ]; then
        rpm -qa 1>/home/backupuser/$CURDATE-Installed-Packages.log
    fi
        " 1>>$OUTLOG 2>>$ERRLOG
        scp -P $SSHPORT $SSHUSER@$i:/home/backupuser/$CURDATE-Installed-Packages.log $BKUPDIR/$i 1>/dev/null 2>>$ERRLOG
    if [ $? != 0 ]; then
        echo "Line $LINENO - Unable to create list of installed packages on $i: Check if /bin/rpm or /usr/bin/dpkg exists (or adjust the path in the script), that \$BKUPDIR is set correctly, \
and that the system supports scp."|tee -a $OUTLOG $ERRLOG
    fi
        ssh $SSHUSER@$i -p $SSHPORT rm -f /home/backupuser/$CURDATE-Installed-Packages.log 1>>$OUTLOG 2>>$ERRLOG
        ssh $SSHUSER@$i -p $SSHPORT sudo $RSYNC $LINOS $BKUPDEST/Backup/$i 1>>$OUTLOG 2>>$ERRLOG
echo "`$TIME` - Backing up $i - OS - COMPLETE"|tee -a $OUTLOG $ERRLOG
done



All times are GMT -5. The time now is 11:19 AM.