I need to create a bash script that reads a txt file on a remote (Windows 2003) server, gets the IP-addresses out of it and then fetches the folders to copy out of another txt file. (all files are auto-generated) The IP addresses that don't have a folder_list file should be ignored. At the moment I can get my txt via FTP and read it out.
I would already be very happy if I had a script that could ECHO the folders based on the ftpservers.txt file, later on I will use this to transfer the folders via FTP.
It's kind of complicated, but please check the attachment (JPEG)
This is an example of the layout of the main file: ftpservers.txt ; contains the IP-addresses, usernames and password. Only the 1st,4th,7th,10th,13th,16th,... line are important in the first place.
line 1 = IP-address
line 2 = FTP-user
line 3 = FTP-pwd
line 4 = IP-address
line 5 = FTP-user
... etc ...
line 33 = FTP-user
line 34 = FTP-pwd
line 35 = IP-address
line 36 = FTP-user
line 37 = FTP-pwd
#-END-
In the same folder there are some files (4 at the moment) named for example 212.114.xxx.xxx_folderlist. Where the first part is the IP address. These files contain the folders
I guess for each IP it finds it gives a _folderlist.txt output... (?)
btw, I already have this piece of code that gives me the IP's back
ftp -n -v immserv1.xx.xx.be << EOT
ascii
user UN password
prompt
cd BACKUP
get ftpservers.txt
ls
EOT
iplist=$(sed -n 1~3p ftpservers.txt)
for LINE in $iplist; do
echo "$LINE"
# ping -c 1 $LINE
done
This is step one. Next step is to display the folders that match with the IP's _folderlist. I've added a printscreen.
awk 'NR%3==1{a[++i]=$0}END{for(f in a){print a[f] "_folderlist.txt"}}' ftpservers.txt
This was in my bash script and the output of it was in my previous post. It would be weird I have a different output?
At the moment I don't have access to the servers, I will try again tomorrow. Can you maybe suggest a bash solution for the second step (concerning the _folderlists)? I will try it next thing tomorrow. Thx in advance
Darned... It gives me this output for every IP address.
80.xx.xx.96
_folderlist.txt: No such file or directory
80.xx.xx.146
_folderlist.txt: No such file or directory
212.xx.xx.172
_folderlist.txt: No such file or directory
...etc...
I made a new script with only this inside:
awk 'NR%3==1{a[++i]=$0}END{for(f in a){print a[f];system("cat " a[f] "_folderlist.txt")}}' ftpservers.txt
EDIT: I found it! There had to be a conversion from a windows txt format to unix txt format first, which I achieved like this
Now I would need something like an IF clause, if there is a folderlist I have to transfer the files via FTP. What isn't a problem, it's only the if clause...
Really weird, I've tested this command on a Debian and on a HP Unix system , it works properly on both systems with an echo command so it should work with a cat command. With the given input file I get this:
Indeed, I hope you saw it works perfectly now! Only now I need to have an IF clause for the IP's where there's a folderlist for. See previous post.
Muchos gracias
FTP is not really a problem here, though I will check out the thread. But what I would need here is:
awk '{ sub("\r$", ""); print }' ftpservers.txt > unixfile.txt
awk 'NR%3==1{a[++i]=$0}END{for(f in a){print a[f];system("cat " a[f] "_folderlist.txt")}}' unixfile.txt
If _folderlist.txt exists then
ftp myserver
user UN PW
**transfer folders**
else
**show: No such file or directory (or something like that)**
fi
done
You can give this a try. The awk command prints the ipaddress and the filename separated by a space.
The 1st while loop reads the ipaddress and the filenames and the 2nd loop gives the contents (directories) of the files.
awk 'NR%3==1{a[++i]=$0}END{for(f in a){print a[f], a[f] "_folderlist.txt"}}' unixfile.txt |
while read ipaddress flist
do
while read dir
do
# log in with $ipaddress
# copy $dir .....
done < $flist
done
Thanks a bunch Franklin, You're a genious! That's exactly what I need. I'm now trying to establish an FTP connection by using $ipaddress and transfer the $dir 's.
I'll let you know when I'm that far
Regards,
Laurens
EDIT: one more thing, how can I get all .txt files at once.
ftp -n -v immserv1.xx.xx.be << EOT
ascii
user U/N P/W
prompt
cd BACKUP
###get *.txt###
ls
EOT
Something like get *.txt , but that ain't working. Thx