• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

FTP Linux Bash Script ??

kmthien

Senior member
Hi guys,

I need a script to do the following procedure,

1) Check to see if /var/temp/ contain *.bin file
2) If no, abort this script. Else continue...to step 3
3) Start FTP to www.temp.com
4) If FTP fail, abort this script. Else continue to step 5
5) Transfer all the /var/temp/*.bin to remote server
6) Close connection
7) move all /var/temp/*.bin to /var/temp/backup
8) end

kmthien
 
Debugging, error checking, etc are left as an excercide for the reader, and it's in perl, not a bash script:

#!/usr/bin/perl

use Net::FTP;

@filesToPut;

opendir F, "/var/temp";

while(my $temp = readdir F){
if($temp =~ m/\.bin$/i){
push @filesToPut, $temp;
}
}
closedir(F);

unless($filesToPut[0]){
print "No files to put to server\n";
exit;
}

$ftp = Net::FTP->new("www.domain.com", Debug => 0);
$ftp->login("username",'password');
$ftp->cwd("/pub");
foreach my $line(@filesToPut){
$ftp->put("/var/temp/$line");
}
$ftp->quit;

foreach my $line(@filesToPut){
`mv /var/temp/$line /var/temp/backup/$line`;
}
 
It's been a while.. but this should work.

#!/bin/sh

Server="ftpserver.domain.com"
User="ftploginuser"
Password="ftppassword"

cd /var/temp/

if ( -e *.bin )
then
ftp -n $Server << End-Ftp-Session
user $User $Password
binary
put *.bin
bye
End-Ftp-Session
if ( -d /var/temp/backup )
then
mkdir /var/temp/backup
fi
cd /var/temp/backup
mv /var/temp/*.bin /var/temp/backup
else
echo "no *.bin files found"
fi
 
Just for fun, here's python (please forgive all the '_', whitespace is significant in Python, but this forum doesn't preserve it)
Untested also, but based on a script I have that does something similar

#!/usr/bin/python

import os
import ftplib
import glob

files = glob.glob("/var/temp/*.bin")

if len(files) == 0:
____sys.exit(0)

ftp = ftplib.FTP("www.temp.com")

try:
____ftp.login() #put username, password here if necesary
except:
____print "ERROR: Failed opening remote site."
____sys.exit(-1)

for file in files:
____file_hndl = open(file, 'r')
____ftp.storbinary('STOR ' + os.path.basename(file), file_hndl, 1024)
____file_hndl.close()
____os.rename(file, "var/temp/backup/" + os.path.basename(file))

ftp.quit()

 
Where did u check in the BASH script whether the ftp connection is success or not ? Thanks !

It's a pain to check things like that in bash because all you can really check is the output text and application exit code. If you know your ftp client returns different exit codes for different errors just check $? after you run the command.

I would really recommend either Perl or at the very least ncftp, perl gives you full control because it's a full fledged programming language and ncftp has batch commands.
 
Originally posted by: ergeorge
Just for fun, here's python (please forgive all the '_', whitespace is significant in Python, but this forum doesn't preserve it)
Untested also, but based on a script I have that does something similar

#!/usr/bin/python

import os
import ftplib
import glob

files = glob.glob("/var/temp/*.bin")

if len(files) == 0:
____sys.exit(0)

ftp = ftplib.FTP("www.temp.com")

try:
____ftp.login() #put username, password here if necesary
except:
____print "ERROR: Failed opening remote site."
____sys.exit(-1)

for file in files:
____file_hndl = open(file, 'r')
____ftp.storbinary('STOR ' + os.path.basename(file), file_hndl, 1024)
____file_hndl.close()
____os.rename(file, "var/temp/backup/" + os.path.basename(file))

ftp.quit()

python rocks! 😀

just been learning it lately, and i love it. btw, that's python, even though the page is php, it's executing a python script (haven't taken time to set up mod_python yet..)
 
Originally posted by: BingBongWongFooey
Originally posted by: ergeorge
Just for fun, here's python (please forgive all the '_', whitespace is significant in Python, but this forum doesn't preserve it)
Untested also, but based on a script I have that does something similar

#!/usr/bin/python

import os
import ftplib
import glob

files = glob.glob("/var/temp/*.bin")

if len(files) == 0:
____sys.exit(0)

ftp = ftplib.FTP("www.temp.com")

try:
____ftp.login() #put username, password here if necesary
except:
____print "ERROR: Failed opening remote site."
____sys.exit(-1)

for file in files:
____file_hndl = open(file, 'r')
____ftp.storbinary('STOR ' + os.path.basename(file), file_hndl, 1024)
____file_hndl.close()
____os.rename(file, "var/temp/backup/" + os.path.basename(file))

ftp.quit()

python rocks! 😀

just been learning it lately, and i love it. btw, that's python, even though the page is php, it's executing a python script (haven't taken time to set up mod_python yet..)

Yep.it does 😀
The ftp library interface is really fugly & poorly documented though 🙁 ... I've been meaning to find or write a wrapper for it, but haven't found the time yet. urllib interface is much nicer, but only does file retrieval.
 
Am I the only person on this forum that actually likes perl?

And I'd like to add that perl's Net::FTP module is really simple and works well =)
 
Originally posted by: Nothinman
Am I the only person on this forum that actually likes perl?

And I'd like to add that perl's Net::FTP module is really simple and works well =)

Ugh ... I hate perl. And I really tried to like it.
That bit about perl being "write-once" language is right on. I had to maintain some perl scripts for awhile ... when it came time for major mods the guys that wrote them didn't understand them anymore. It ended up being easier to rewrite them in python.

Description I saw once of a largish perl script: "Looked like the gory remnants of an explosion in an ASCII factory"

 
Originally posted by: ergeorge
Originally posted by: Nothinman
Am I the only person on this forum that actually likes perl?

And I'd like to add that perl's Net::FTP module is really simple and works well =)

Ugh ... I hate perl. And I really tried to like it.
That bit about perl being "write-once" language is right on. I had to maintain some perl scripts for awhile ... when it came time for major mods the guys that wrote them didn't understand them anymore. It ended up being easier to rewrite them in python.

Description I saw once of a largish perl script: "Looked like the gory remnants of an explosion in an ASCII factory"

It's not that hard to read.
 
Originally posted by: notfred
Originally posted by: ergeorge
Originally posted by: Nothinman
Am I the only person on this forum that actually likes perl?

And I'd like to add that perl's Net::FTP module is really simple and works well =)

Ugh ... I hate perl. And I really tried to like it.
That bit about perl being "write-once" language is right on. I had to maintain some perl scripts for awhile ... when it came time for major mods the guys that wrote them didn't understand them anymore. It ended up being easier to rewrite them in python.

Description I saw once of a largish perl script: "Looked like the gory remnants of an explosion in an ASCII factory"

It's not that hard to read.

The author of a particular piece of source is always a major factor in its readability, regardless of the language. And these guys were no gems in that respect. But perl seems to go out of its way to make writing obfuscated code easy, or even neccesary.
 
Originally posted by: notfred
Originally posted by: ergeorge
But perl seems to go out of its way to make writing obfuscated code easy

Yes, in fact it does 🙂

...or even neccesary

They've tried to avoid that, though.

$_="krJhruaesrltre c a cnp,ohet";$_.=$1,print$2while s/(..)(.)//;

See?
See?!?
That's exactly the kind of sh|t I'm taking about!!!
😀

 
Hi notfred,

I tried ur script but a message saying that : "bad interpreter : No such file or directory" ! Pls help !

larva
 
Hi notfred,

When I tried to delete the route from local server to remote server and run the perl script u given, an error message saying that
"Can't call method "login" on an undefined value at /script/ftp2.pl line 22"

ftp2.pl is the script u given and line 22 is $ftp->login("username","password");

can u help me to add in some code to this script where when fail to connect the remote server, create or append a log file in /var/temp/log/ where the file contain the date and time of the incident.
If there is a possibility that when transfering files to the remote server and the remote server goes down suddenly, what will this script do with this incident ?
Thank you !

larva
 
Hi notfred,

can u help me to add in some code to this script where when fail to connect the remote server, create or append a log file in /var/temp/log/ where the file contain the date and time of the incident. Thank you !

larva
 
Hi notfred,

Whenever the crontab execute the perl script with root account, root will receive an email for each execution of the script. Is there anyway to disable this and instead of sending email, log it into a file ?

larva
 
cron always emails any output the script makes to the owner of the cronjob. You can either just use 'print's and redirect the output to a file for logging or you can get more sophisticated and use syslog, I havn't personally used any of the perl syslog modules though. To redirect the output just put '> /path/to/log 2>&1' after the command.
 
Back
Top