jadelester
Technical User
I need help with a script I am working on...
First, let me say that my scripting knowledge is limited, and what I have gotten so far has been either from what I do know, snipits from this site, or help from friends and books.
What I am trying to do is create a script where depending on what files are different between the local and remote servers, get the new files from the remote server, and depending on where they come from, store them in an appropiate directory, and change the first character in the filename to correspond with the location it was pulled from (i.e change file named G0000V00 to D0000V00 for dallas files, or K0000V00 for kansas files, etc (note that the "0's" will change, but will always be numeric).
Currently I have a script that will FTP to the server and log the output of "ls" to a local file, I then pass the file thru sed and diff (on a local and remote file list created by ls output) a few times to clean it up to get just the remote files that are not present on the local server.
Here is what I have so far:
This part creates a cleaned file with the remote files that are different from the local files in a specified directory.
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
#!/usr/bin/ksh
# set variables
export IP_ADDRESS=209.51.15.30
export REMOTE_BASE_DIR=/home/jade/ftptest/
export UID=********
export PASWD=*******
export LOCAL_BASE_DIR=/export/home/jlester/ftptest/
export REMOTE_FILE_LIST=$LOCAL_BASE_DIR/remote_ls.output
export LOCAL_FILE_LIST=$LOCAL_BASE_DIR/local_ls.output
export TMP_FILE=$LOCAL_BASE_DIR/tmp
#temp variables
export FOO=$LOCAL_BASE_DIR/foo
export FOO2=$LOCAL_BASE_DIR/foo2
export FOO3=$LOCAL_BASE_DIR/foo3
# generate list of files on remote server.
ftp -inv <<-EOF >$TMP_FILE
open $IP_ADDRESS
user $UID $PASWD
cd $REMOTE_BASE_DIR
ls
quit
EOF
NUM_LINES=`wc -l <$TMP_FILE |sed 's/ //g'`
let LAST_GOOD_LINE=$NUM_LINES-6
sed -n "8,${LAST_GOOD_LINE}p" $TMP_FILE | sort > $REMOTE_FILE_LIST
# generate list of files on local server, and populate the temporary file with "get " before each filename.
ls $LOCAL_BASE_DIR | sort > $LOCAL_FILE_LIST
diff $LOCAL_FILE_LIST $REMOTE_FILE_LIST >$FOO
grep '>' $FOO | sed 's/^..//' > $FOO2
cat $FOO2 |sed 's/^/get /g' >$FOO3
exit 0
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Next comes the part that I am having the most trouble with. What I am trying to do is read in a control file with all the information for the servers I need to check. Note that there are multiple entries to this file. The control file format is:
IP:UIDASS:RemoteDir:LocalDir
ex: 127.0.0.1:UID:/path/to/remotefile/:/path/to/local/file
The main problem that I am having is that I am dealing with several remote servers, and several different files per server. I need to be able to read the file into the script, and loop thru each line, AND somehow read the file "foo3" to determine which files are the different files, and initiate an FTP session to get the remote files (again storing them in the correct directory, with the first character changed).
This is what I have so far...
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
#!/bin/ksh
CfgFile=/export/home/jlester/scripts/tempftplist.conf
LogFile=/export/home/jlester/ftptest/ftplog
sed 's/:/ /g' $CfgFile | while read IPAddr UserName Password RDir LDir
do
#cat <<-FOO
ftp -inv <<-BAR >$LogFile
open $IPAddr
user $UserName $Password
cd $RDir
*** This is where I need help ********
bye
BAR
#FOO
echo
done
exit 0
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Of course I want to combine all of this into one script and log the output, and put some error checking in, but right now it is one step at a time, I just want to make it work. If there are any ways you can suggest to accomplish what I am needing, or see something I may have forgot, please feel free to speak up!
Sorry for being long winded, but I am trying to be as concise as possible. Thanks in advance for any help help you can provide.
Thanks
Jade Lester
First, let me say that my scripting knowledge is limited, and what I have gotten so far has been either from what I do know, snipits from this site, or help from friends and books.
What I am trying to do is create a script where depending on what files are different between the local and remote servers, get the new files from the remote server, and depending on where they come from, store them in an appropiate directory, and change the first character in the filename to correspond with the location it was pulled from (i.e change file named G0000V00 to D0000V00 for dallas files, or K0000V00 for kansas files, etc (note that the "0's" will change, but will always be numeric).
Currently I have a script that will FTP to the server and log the output of "ls" to a local file, I then pass the file thru sed and diff (on a local and remote file list created by ls output) a few times to clean it up to get just the remote files that are not present on the local server.
Here is what I have so far:
This part creates a cleaned file with the remote files that are different from the local files in a specified directory.
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
#!/usr/bin/ksh
# set variables
export IP_ADDRESS=209.51.15.30
export REMOTE_BASE_DIR=/home/jade/ftptest/
export UID=********
export PASWD=*******
export LOCAL_BASE_DIR=/export/home/jlester/ftptest/
export REMOTE_FILE_LIST=$LOCAL_BASE_DIR/remote_ls.output
export LOCAL_FILE_LIST=$LOCAL_BASE_DIR/local_ls.output
export TMP_FILE=$LOCAL_BASE_DIR/tmp
#temp variables
export FOO=$LOCAL_BASE_DIR/foo
export FOO2=$LOCAL_BASE_DIR/foo2
export FOO3=$LOCAL_BASE_DIR/foo3
# generate list of files on remote server.
ftp -inv <<-EOF >$TMP_FILE
open $IP_ADDRESS
user $UID $PASWD
cd $REMOTE_BASE_DIR
ls
quit
EOF
NUM_LINES=`wc -l <$TMP_FILE |sed 's/ //g'`
let LAST_GOOD_LINE=$NUM_LINES-6
sed -n "8,${LAST_GOOD_LINE}p" $TMP_FILE | sort > $REMOTE_FILE_LIST
# generate list of files on local server, and populate the temporary file with "get " before each filename.
ls $LOCAL_BASE_DIR | sort > $LOCAL_FILE_LIST
diff $LOCAL_FILE_LIST $REMOTE_FILE_LIST >$FOO
grep '>' $FOO | sed 's/^..//' > $FOO2
cat $FOO2 |sed 's/^/get /g' >$FOO3
exit 0
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Next comes the part that I am having the most trouble with. What I am trying to do is read in a control file with all the information for the servers I need to check. Note that there are multiple entries to this file. The control file format is:
IP:UIDASS:RemoteDir:LocalDir
ex: 127.0.0.1:UID:/path/to/remotefile/:/path/to/local/file
The main problem that I am having is that I am dealing with several remote servers, and several different files per server. I need to be able to read the file into the script, and loop thru each line, AND somehow read the file "foo3" to determine which files are the different files, and initiate an FTP session to get the remote files (again storing them in the correct directory, with the first character changed).
This is what I have so far...
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
#!/bin/ksh
CfgFile=/export/home/jlester/scripts/tempftplist.conf
LogFile=/export/home/jlester/ftptest/ftplog
sed 's/:/ /g' $CfgFile | while read IPAddr UserName Password RDir LDir
do
#cat <<-FOO
ftp -inv <<-BAR >$LogFile
open $IPAddr
user $UserName $Password
cd $RDir
*** This is where I need help ********
bye
BAR
#FOO
echo
done
exit 0
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Of course I want to combine all of this into one script and log the output, and put some error checking in, but right now it is one step at a time, I just want to make it work. If there are any ways you can suggest to accomplish what I am needing, or see something I may have forgot, please feel free to speak up!
Sorry for being long winded, but I am trying to be as concise as possible. Thanks in advance for any help help you can provide.
Thanks
Jade Lester