FTP and "GET_LOGS" Script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting FTP and "GET_LOGS" Script
# 1  
Old 12-30-2011
FTP and "GET_LOGS" Script

Hi Good day!

I have a script which is set in CRON entry and runs every 0900H, this will collect all the needed logs from each server. Let's call it collector.sh.

I have 5 servers being the first server (server1) acts as the master server in the cluster. I deployed another script in the server1 whose function is to initiate an FTP connection to a server then get the logs then dump it to the assigned directory on server1. This is the code:
Code:
#!/bin/sh

HOST='IP_ADDR'
USER='user'
PASSWD='pass'
FILE='/home/user/USER_LOGS/*'

ftp -ni $HOST <<END_SCRIPT
quote USER $USER
quote PASS $PASSWD
mget $FILE
quit
END_SCRIPT
exit 0

Now my problem is that I've built five of this just to perform the output that I want. Can someone add/improve the script above in order for me not to create 5 scripts for 5 FTP connections, but instead 1 script which will FTP connect to each server and perform the command respectively.


br,
rymnd_12345
# 2  
Old 12-30-2011
This may help point you in the right direction:

Code:
#!/bin/sh

# declare an array
declare -a HOSTS

# create an array to hold the host ip addresses
HOSTS=(ipaddress1 ipaddress2 ipaddress3 ipaddress4 ipaddress5)
USER='user'
PASSWD='pass'
FILE='/home/user/USER_LOGS/*'

# get the total number of hosts in the array
NUM_OF_HOSTS=${#HOSTS[@]}

# loop through the hosts in the array and feed 
# each one to ftp
for((HOST=0; HOST<$NUM_OF_HOSTS; HOST++))
do
ftp -ni ${HOSTS[$HOST]} <<END_SCRIPT
quote USER $USER
quote PASS $PASSWD
mget $FILE
quit
END_SCRIPT
done

exit 0

Hope this helps.
# 3  
Old 12-30-2011
Hi in2nix4life,

YES! Thanks so much my friend, a great help! Happy New Year Smilie


Cheers,
rymnd_12345
# 4  
Old 12-30-2011
No problem.

Happy New Year.
# 5  
Old 01-02-2012
here's a much simpler version ...

Code:
#!/bin/sh

USER='user'
PASSWD='pass'
FILE='/home/user/USER_LOGS/*'

# loop through the hosts list and feed 
# each one to ftp
for HOST in ipaddress1 ipaddress2 ipaddress3 ipaddress4 ipaddress5
do
ftp -ni $HOST <<END_SCRIPT
quote USER $USER
quote PASS $PASSWD
mget $FILE
quit
END_SCRIPT
done

exit 0

here's another version where the hosts are in an external list ... you can then add/subtract hosts to the list without having to change the script ...

the hostlist will look like ...
Code:
ipaddress1
ipaddress2
ipaddress3
ipaddress4
ipaddress5

and the script will look like ...
Code:
#!/bin/sh

LIST='/home/user/hostlist'
USER='user'
PASSWD='pass'
FILE='/home/user/USER_LOGS/*'

# loop through the hosts list and feed 
# each one to ftp
for HOST in `cat $LIST`
do
ftp -ni $HOST <<END_SCRIPT
quote USER $USER
quote PASS $PASSWD
mget $FILE
quit
END_SCRIPT
done

exit 0

btw, putting in a user login password in a script is highly discouraged for security purposes ... if remote servers are all Unix/Linux, you would be better off using scp with keys (see man scp, man ssh and man sshd) whenever possible ...

Code:
#!/bin/sh

LIST='/home/user/hostlist'
USER='user'
FILE='/home/user/USER_LOGS/*'
LOCALDIR='/home/user/COLLECTED_LOGS'

# loop through the hostlist and feed 
# each one to scp
for HOST in `cat $LIST`
do
    scp -p $USER@$HOST:$FILE $LOCALDIR
done

exit 0

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. AIX

Apache 2.4 directory cannot display "Last modified" "Size" "Description"

Hi 2 all, i have had AIX 7.2 :/# /usr/IBMAHS/bin/apachectl -v Server version: Apache/2.4.12 (Unix) Server built: May 25 2015 04:58:27 :/#:/# /usr/IBMAHS/bin/apachectl -M Loaded Modules: core_module (static) so_module (static) http_module (static) mpm_worker_module (static) ... (3 Replies)
Discussion started by: penchev
3 Replies

2. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

3. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

4. UNIX for Dummies Questions & Answers

"Help with bash script" - "License Server and Patch Updates"

Hi All, I'm completely new to bash scripting and still learning my way through albeit vey slowly. I need to know where to insert my server names', my ip address numbers through out the script alas to no avail. I'm also searching on how to save .sh (bash shell) script properly.... (25 Replies)
Discussion started by: profileuser
25 Replies

5. Red Hat

files having Script which works behind "who" & "w" commands

Dear All, plz print the path of files which have the script of "who" & "w" commands. thnx in advance. (6 Replies)
Discussion started by: saqlain.bashir
6 Replies

6. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

7. AIX

"too big" and "not enough memory" errors in shell script

Hi, This is odd, however here goes. There are several shell scripts that run in our production environment AIX 595 LPAR m/c, which has sufficient memory 14GB (physical memory) and horsepower 5CPUs. However from time to time we get the following errors in these shell scripts. The time when these... (11 Replies)
Discussion started by: jerardfjay
11 Replies

8. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies

9. UNIX for Dummies Questions & Answers

An Auto-Login "ftp" Script

We are running SCO Unix under Unixware 2.1.3 (really old!). I would appreciate if someone could show me the code of a script to automatically "ftp" one or more files to another SCO Unix computer on the nightly basis, then gracefully termninate the ftp session. Thanks for your help. (3 Replies)
Discussion started by: tphan
3 Replies
Login or Register to Ask a Question