FTP Linux Bash Script ??

kmthien

Senior member
Oct 8, 2002
363
0
0
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
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
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`;
}
 

kt

Diamond Member
Apr 1, 2000
6,015
1,321
136
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
 

kmthien

Senior member
Oct 8, 2002
363
0
0
Hi,

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

kmthien
 

Armitage

Banned
Feb 23, 2001
8,086
0
0
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()

 

Nothinman

Elite Member
Sep 14, 2001
30,672
0
0
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.
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
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..)
 

Armitage

Banned
Feb 23, 2001
8,086
0
0
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.
 

Nothinman

Elite Member
Sep 14, 2001
30,672
0
0
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 =)
 

Armitage

Banned
Feb 23, 2001
8,086
0
0
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"

 

notfred

Lifer
Feb 12, 2001
38,241
4
0
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.
 

Armitage

Banned
Feb 23, 2001
8,086
0
0
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.
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
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/(..)(.)//;
 

Armitage

Banned
Feb 23, 2001
8,086
0
0
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!!!


 

notfred

Lifer
Feb 12, 2001
38,241
4
0
Originally posted by: ergeorge

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

But it does exactly the same thing as:

print "Just another perl hacker,";

 

larva

Member
Jul 15, 2001
185
0
0
Hi notfred,

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

larva
 

larva

Member
Jul 15, 2001
185
0
0
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
 

larva

Member
Jul 15, 2001
185
0
0
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
 

larva

Member
Jul 15, 2001
185
0
0
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
 

Nothinman

Elite Member
Sep 14, 2001
30,672
0
0
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.
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |