LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Bash script - reading from text file (https://www.linuxquestions.org/questions/programming-9/bash-script-reading-from-text-file-258523/)

twantrd 11-23-2004 05:27 PM

Bash script - reading from text file
 
Hi There,

Got a question for you bash experts out there :). I have a file (client_ip):

192.168.3.100 twantrd
192.168.3.101 johntrd
192.168.3.102 ricktrd

As you can see, it contains the ip address and hostname of the machine associated with it. I pretty much need a bash script that will ssh into each ip and grab files to be backed and store it back on the server under the appropriate hostname. So it's like:

Server A ---(ssh)---> 192.168.3.100:/home/RAID/
192.168.3.100 ---(rsync)---> Server A:/home/backup/twantrd/

Server A ---(ssh)---> 192.168.3.101:/home/RAID/
192.168.3.101 ---(rsync)---> Server A:/home/backup/johntrd/

etc...

I have no problem creating a for loop to iterate through the file and grab the IP's and ssh'ing into them. My big problem is how to rsync the data to the appropriate hostname folder on ServerA. My partial script is below. Please help if someone can!! I truly appreciate it!! Thanks!

#!/bin/sh

myserver="blah.twantrd.com"
dir="/home/backup/"
backupdir="/home/RAID"
clientip=`cat client_ip | egrep -v '^#' | awk '{print $1}'`
clienthost=`cat client_ip | egrep -v '^#' | awk '{print $2}'`

#
#This will rsync the client's data to the server
#
for ip in $clientip; do
$ssh $ip "(cd $backupdir; rsync -avz * $myserver:$dir)"
done

P.S Forgot, this is all done on the server side (Server A).


-twantrd

J_Szucs 11-23-2004 08:54 PM

I think you could cycle through each line of the hosts file like this:
IFS="
"
for line in `cat $hosts | egrep -v '^#'` ; do
client_ip=${line/ */}
client_host=${line/* /}
#Put your ssh command here
done

Note: your variable "client_ip" refers to two different things, thats not good: first it stores the name of the file, then it stores the ip of each client. So I changed the name of the variable storing the filename to "hosts".

twantrd 11-23-2004 10:59 PM

Thanks for the input but that still didn't work. "client_ip" and "client_host" both have the ip address and the hostname. Hmmm....

-twantrd

nitin_batta 11-23-2004 11:20 PM

Hi,

Are you having an issue with the with scripting or rsync not working between server A and the clients

twantrd 11-24-2004 12:38 AM

I *was* having an issue with just iterating the ip address with the associated hostname in the for loop section. Pretty much I wanted to ssh into each of those ip's and rsync the data back to Server A under the path of the hostname that was associated with just that IP. Hmmmm, that sounded kind of confusing :).

Anyhow, I just wanted to say thanks to those who tried to help. I got the script working. I was working on this and all of a sudden a lightbulb came on in my head and I found out how. If u guys are interested...I'll post the code. Thanks again!!!

-twantrd


All times are GMT -5. The time now is 10:23 AM.