Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

UNIX (Solaris) FTP -

Status
Not open for further replies.
Aug 6, 2003
6
US
I need to logon into an FTP server under different user names, passwords and retrieve one or more uniquely named files per FTP account. Ideally, I would like to sequentially store the user name, password, and filename information in one file like:

(machine1) (user1) (pass1) (file1)
(machine2) (user2) (pass2) (file2)
.
EOF
and pass it into an FTP function that references the machine name, user name, password, and filename as variables....... The function would perform the ftp (machine name), user, pass, get, bye for each row/record in the parameter file.....

This seems easy to do....... Being new to UNIX, I am not having much luck....

I am new to UNIX, any help/or suggestions would greatly be appreciated.

Thanks -
StartingUnix
 
You could try something like this:

1) Create the ip-password file:

cat - <<EOF >/tmp/ftp.dat
111.22.33.440 ftpuser ftpuser /usr/mydir/file1
111.22.33.441 oracle oracle /usr/mydir/file2
EOF

2) Create a script like this:

----- cut --------------------------------------
#!/bin/ksh

echo >/tmp/ftp.cmd
while read ip uid pw fil
do
fn=${fil##*/}
{
echo &quot;open $ip&quot;
echo &quot;user $uid $pw&quot;
echo &quot;bin&quot;
echo &quot;get $fil /export/${fn}_$ip&quot;
echo &quot;close&quot;
} >>/tmp/ftp.cmd
done </tmp></tmp>

----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
Hi LKBrwnDBA -

Thanks for the reply.... It is not clear to me how/where the script references the ip-password file...... In other words, which command feeds the password file into the script........

Additional help is appreciated.....

StartingUnix
 
The script somehow got truncated, it should read like this:

#!/bin/ksh

echo >/tmp/ftp.cmd
while read ip uid pw fil
do
fn=${fil##*/}
{
echo &quot;open $ip&quot;
echo &quot;user $uid $pw&quot;
echo &quot;bin&quot;
echo &quot;get $fil /export/${fn}_$ip&quot;
echo &quot;close&quot;
} >>/tmp/ftp.cmd
done </tmp/ftp.dat <<-- this feeds the data file

ftp -vs </tmp/ftp.cmd <<-- this was missing.

----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
LKBrwnDBA - That worked out really well... Thank you.......

How would I need to modify the script if I wanted to append a date/time stamp onto
the script instead of the IP address???

Thanks again -

StartingUnix
 
Like this:

#!/bin/ksh

CURDT=$(date +%Y%m%d%H%M%S)
echo >/tmp/ftp.cmd
while read ip uid pw fil
do
fn=${fil##*/}
{
echo &quot;open $ip&quot;
echo &quot;user $uid $pw&quot;
echo &quot;bin&quot;
echo &quot;get $fil /export/${fn}_$CURDT&quot;
echo &quot;close&quot;
} >>/tmp/ftp.cmd
done < /tmp/ftp.dat

ftp -vs < /tmp/ftp.cmd

----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
LKBrwnDBA - Thank You.. - You the Man.....

I was able to route the results of the FTP to a log file for ftp succcess inspection by
changing the last line to read

ftp -vs </tmp/ftp.cmd >> ftp_result

However, when I tried to label this file with the user id (uid) variable i didn't have any luck.... ftp_result_$uid...

How would I need to modify the logic of the script so that it will iterate the results of the FTP into a log file that gets dynamically named with the user id?

I shouldn't be asking too many more questions.....

Learning alot.....

Thanks again -
StartingUnix
 
Try something like this:
Code:
ftp_result_$(id -u)

Hope This Help
PH.
 
on Solaris 'id' has no '-u'.
Try it like that:

ftp_result_$(id | nawk -F'[()]' '{print $2}')

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
The u is coming from $uid. If you read the top of this thread, you will see that the uid field is being populated from a read of a datafile which contains the
machine name, userid, password, filename fields.....

I trying to allocate the ftp status file with the userid ($uid) as part of the name...

I tried using the nawk statement suggestion... Upon execution, this returned a statement saying 'nawk: no program given'.....

Other suggestions appreciated.
 
oh, ok - sorry about that.
if you have 'uid' available from the 'read', how 'bout:
ftp_result_${uid}

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
As the uid is varying from line to line...
Code:
#!/bin/ksh
CURDT=$(date +%Y%m%d%H%M%S)
while read ip uid pw fil; do
 fn=${fil##*/}
 {
  echo &quot;open $ip&quot;
  echo &quot;user $uid $pw&quot;
  echo &quot;bin&quot;
  echo &quot;get $fil /export/${fn}_$CURDT&quot;
  echo &quot;close&quot;
 } | ftp -vs >>ftp_result_$uid
done < /tmp/ftp.dat

Hope This Help
PH.
 
Thanks PHV - making lots of progress here.....

Now, I am trying to interogate the results of the ftp_result_$uid file and am not having good luck.....

I have tried putting the block of code (below) above and beneath the Done command. Neither one worked....

set UNKNOWN_HOST = `grep -i 'unknown host' ftp_result_$uid | wc -l`
set LOGIN_FAIL = `grep -i '^530 ' ftp_result_$uid | wc -l`
set FILE_ERR = `grep -i '^550 ' ftp_result_$uid | wc -l`
set FILE_TRANSFER_OK = `grep -i '^226 ' ftp_result_$uid | wc -l`
#
if ($UNKNOWN_HOST) then
echo 'FTP Failed - cannot connect to host' >> ftp_result_$uid
else if ($LOGIN_FAIL) then
echo 'FTP Login Failed - Invalid User ID/Password' >> ftp_result_$uid
else if ($FILE_ERR) then
echo 'File has not arrived yet' >> ftp_result_$uid
#continue if this is the case- we expect this condition on the get statement
else if ($FILE_TRANSFER_OK) then
echo 'File Transfer successful... ' >> ftp_result_$uid
endif
endif
endif
endif

Is there another approach that may work better?

Thanks in advance,
StartingUnix
 
First, you need to put the equals (=) without the 'set' and without spaces.
Second, the test should be within the loop, because of the $uid variable.
Third, the if's are constructed incorrectly.
What I don't understand is why you wish to write a message to the same log you are reading from?

Try this:


#!/bin/ksh
CURDT=$(date +%Y%m%d%H%M%S)
while read ip uid pw fil; do
fn=${fil##*/}
{
echo &quot;open $ip&quot;
echo &quot;user $uid $pw&quot;
echo &quot;bin&quot;
echo &quot;get $fil /export/${fn}_$CURDT&quot;
echo &quot;close&quot;
} | ftp -vs >ftp_result_$uid
UNKNOWN_HOST=`grep -i 'unknown host' ftp_result_$uid | wc -l`
LOGIN_FAIL=`grep -i '^530 ' ftp_result_$uid | wc -l`
FILE_ERR=`grep -i '^550 ' ftp_result_$uid | wc -l`
FILE_TRANSFER_OK=`grep -i '^226 ' ftp_result_$uid | wc -l`
#
if test $UNKNOWN_HOST -gt 0
then
echo &quot;$uid FTP Failed - cannot connect to host&quot; >> ftp_result_$uid
elif test $LOGIN_FAIL -gt 0
then
echo &quot;$uid FTP Login Failed - Invalid User ID/Password' >> ftp_result_$uid
elif test $FILE_ERR -gt 0
then
echo &quot;$uid File has not arrived yet&quot; >> ftp_result_$uid
#continue if this is the case- we expect this condition on the get statement
elif test $FILE_TRANSFER_OK -gt 0
then
echo &quot;$uid File Transfer successful... &quot; >> ftp_result_$uid
fi
done < /tmp/ftp.dat

----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top