LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 12-18-2009, 09:45 PM   #1
gimpy530
Member
 
Registered: Oct 2007
Posts: 98

Rep: Reputation: 16
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
 
Old 12-18-2009, 11:59 PM   #2
AutoBot
Member
 
Registered: Mar 2002
Location: I can see you from here.
Distribution: Gentoo 1.3b
Posts: 182

Rep: Reputation: 34
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?
 
Old 12-19-2009, 12:18 AM   #3
AutoBot
Member
 
Registered: Mar 2002
Location: I can see you from here.
Distribution: Gentoo 1.3b
Posts: 182

Rep: Reputation: 34
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
 
Old 12-19-2009, 08:15 AM   #4
gimpy530
Member
 
Registered: Oct 2007
Posts: 98

Original Poster
Rep: Reputation: 16
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
 
Old 12-19-2009, 10:22 PM   #5
gimpy530
Member
 
Registered: Oct 2007
Posts: 98

Original Poster
Rep: Reputation: 16
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

Last edited by gimpy530; 12-20-2009 at 05:19 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
xen: Running Firefox remotely via ssh -X actually runs locally. ordinary Linux - Networking 9 02-27-2008 10:02 AM
Accessing Samba remotely via SSH (whats the commands ?) Dark Carnival Linux - Networking 4 11-18-2006 08:21 AM
Access Denied when I try to login remotely to my SSH server running on Cygwin. andrew_cz General 0 05-02-2006 09:46 AM
Running a shell script remotely via 'ssh <hostname> <command>' davee Linux - General 4 10-09-2005 01:38 AM
executing multiple commands by ssh jpan Linux - General 1 10-22-2004 02:12 PM


All times are GMT -5. The time now is 06:28 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration