Ftp scripting question


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Ftp scripting question
# 1  
Old 10-16-2008
Ftp scripting question

Hi guys,

I'm trying to create an ftp script that will read in a value from input, then search for files with the input value, and then ftp the files..

So far I've got my read answer set up, and it'll ls -lrt out my files with the value read answer var I given it...

My question is, what would be the best way to ftp these files... There'll be around 31 one of these files, so I don't want to write 31 separate ftp commands in the script..

Is there a way to pass the files somehow to the one ftp command? I know how to make driver files but can't figure out how I'd pass the info into one and get it to work..

Any suggestions what the best way to do this would be?

Thanks,
Jaz

**EDIT**
Using .ksh if it makes any difference
# 2  
Old 10-16-2008
Java

I am just giving a basic idea and you can modify it as per your requriment

Code:
servername="xx.x.xx.xx";
username="user";
password="pswd";

var=`ls`
cmd=`echo mput $var`
echo $cmd

ftp -nv $servername <<EOF > ftp.log
user $username $password
prompt
$cmd
EOF

mput - will ftp multiple files(with interatcive mode by default)
prompt - will toggle the interactive mode "on" and "off"

Hope this is something what you are looking for!

Regards
Dileep
# 3  
Old 10-16-2008
Ok, not sure how that would work for me...

The script is going to be used to ftp files from an archive dir.. When the script is run it will ask the user for the date of the files they're looking for.
It will then list out the files (which will look like LTR1.txt-20081016 and on confirmation will ftp them..
This is what I have so far. The ftp part will happen in the first part of the 'then' statemen.

echo "Please enter the date of the files you are looking for in the format yyyymmdd"
read answer

SFILE=LTR*-$answer*
ls -lrt $SFILE | cut -d " " -f 22
#
echo ""
echo "Are these the correct files you want to see?"
read answer1
if [ "$answer1" = "Y" ] || [ "$answer1" = "y" ]
then

The ftp will need to drop the archive date when its sending the file. So instead of LTR1.txt-20081016, it'll send LTR1.txt. Not sure if your way is what i'm looking for but it's a good start..
# 4  
Old 10-16-2008
Try this:

Code:
SFILE=LTR*-$answer*
ls -rt $SFILE
#
echo ""
echo "Are these the correct files you want to see?"
read answer1
if [[ "$answer1" = [Yy] ]]
then
        ls -rt $SFILE | awk '
                BEGIN { print "user username password" }
                {
                        srcfile=$1
                        destfile=$1
                        sub("-[0-9]{8}$","",destfile)
                        print "put",srcfile,destfile
                }
                END { print "bye" }
        ' | ftp -nv hostname
fi

Naturally you will need to change username, password and hostname to appropriate values. Note that I changed the cut -d " " -f22 as it didn't work on the system I was testing on, and it's unnecessary anyway if you just leave out the -l.

Last edited by Annihilannic; 10-17-2008 at 12:15 AM..
# 5  
Old 10-16-2008
Wow, thanks man... I'll have to test it out in a bit to see if it works for me..
Where about would you put where you want destination folder to be?

Can you explain how some of the code works?

What does the BEGIN do?
What does the line sub("-[0-9]{8}$","",destfile) actually do?
And should there be $ before srcfile & destfile in the line print "put",srcfile,destfile are they not variables?

Thanks for your help?

Last edited by Jazmania; 10-17-2008 at 12:16 AM..
# 6  
Old 10-17-2008
I've colourised my previous post slightly. The part highlighted in avocado green is an awk script, not normal shell script, so the language is a little different. That's why the variables don't have $'s in front of them.

The BEGIN clause is processed before awk reads any of the input (which it reads from stdin). Similarly the END clause is processed after it has finished reading all the input. Any other expression { commands } clauses are processed whenever the expression is true, but in the case of this script I've left out the expression because we want to execute the prints for every input line. See man awk for more details about the awk language.

The sub() function substitutes a dash followed by 8 digits with nothing in the destination filename.
# 7  
Old 10-17-2008
Thanks for that, had done a man awk and read up a little on it..

Was wondering where about I would put where the destination folder command?
I thought it would go within the awk script like below, but I'm not sure if i'm correct. Can you clear this up for me


ls -rt $SFILE | awk '
BEGIN { print "user username pwd" }
{
srcfile=$1
destfile=$1
sub("-[0-9]{8}$","",destfile)
print "cd /home/test_files"
print "put",srcfile,destfile
}
END { print "bye" }

Last edited by Jazmania; 10-17-2008 at 12:44 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

FTP scripting issue

I dont have enough bits to post in emergency, but I have an urgent FTP issue. A client of ours wants us to pull files from his server onto our server at which point we can process it. I ask a friend what to do, but I was just give the things to do, not how to do it. I havent written a Unix... (4 Replies)
Discussion started by: MJCreations
4 Replies

2. Shell Programming and Scripting

Scripting with FTP Process

Hi Guys, Good day ULF! I have a general "auto-ftp" code which looks something like this: #!/bin/sh cd $1 ftp -v -n $2 << EOF user $3 $4 prompt cd $5 bin mput $6 quit EOF This works pretty well, but I'm thinking of how can I make a code by maybe including on this code, for... (5 Replies)
Discussion started by: rymnd_12345
5 Replies

3. Shell Programming and Scripting

Need FTP scripting

Hi, Please can you tell me the script to transfer file from server A to Server B (1 Reply)
Discussion started by: e1994264
1 Replies

4. Shell Programming and Scripting

FTP scripting Help

Hi, I'm trying to do an FTP script that will read from a list of files and send only the files in that list. Would this work? Does anyone have anything simpler. ftp -nv <<EOF open server user username password get $(nawk -F_ -f bbb.awk Filelisting.txt) EOF (5 Replies)
Discussion started by: bbbngowc
5 Replies

5. Shell Programming and Scripting

FTP-Shell scripting

Hi Everybody, I have come across a problem while doing FTP using shell scripting. So I request any of you can give some idea of how to go about solving the problem but i request you not to give the solution please. Because i'm a java developer newly into Unix shell progrmming and am very enthu... (1 Reply)
Discussion started by: RSC1985
1 Replies

6. Shell Programming and Scripting

Shell scripting - FTP

Can some one help with script to track ftp logins on AIX server.. (1 Reply)
Discussion started by: ddk2oo5
1 Replies

7. Shell Programming and Scripting

FTP scripting

Hello, I'm sure this is an easy one for you UNIX pros. I would like to create and and automated an FTP process. The script should do this: 1. Log into a remote system 2. change to a specified directory 3. copy local files to the remote system 4. quit ftp 5. wait until the next schedule ... (7 Replies)
Discussion started by: bbbngowc
7 Replies

8. Answers to Frequently Asked Questions

Automate FTP / Scripting FTP Transfers

One of our most frequent questions is how to automate ftp transfers. There are several approaches. Since I'm writing this post, we will start with my favorite technique. :) In Automated FTP task I present a simple example of my ksh co-process technique. And note that later in this thread I... (0 Replies)
Discussion started by: Perderabo
0 Replies

9. Shell Programming and Scripting

scripting a ftp process

I am trying to automate a process in one of two ways: 1) ftp .txt files from a unix server to a Windows 2000 server. I want to do this in a script. Can I supply the login and password to the Windows server inside the script? 2) I tried this and failed: sftp .txt files from the unix server to... (2 Replies)
Discussion started by: gbernard
2 Replies

10. UNIX for Dummies Questions & Answers

Telnet FTP scripting

Hi quick question from a unix newbie Working on a project to get me using unix, the point of this project is to find a printer on the network check for jobs in the printer if the printer has no jobs do nothing if the printer has jobs then check the status for errors and e-mail the user. This... (2 Replies)
Discussion started by: w33man
2 Replies
Login or Register to Ask a Question