Operating System - Linux
1819928 Members
3055 Online
109607 Solutions
New Discussion юеВ

script to login to ftp and check

 
yuva_mca
Occasional Advisor

script to login to ftp and check

I need the shell script which suppose to check the ftp transation.
Like the script has to login to the ftp site with corresponding username and password. If ftp have any issues while executing the script, script suppose to throw the message with corresponding issue(eg : problem with port or problem server or etc).

Thanks
Yuva
10 REPLIES 10
Muthukumar_5
Honored Contributor

Re: script to login to ftp and check

You can try as,

ftp -i -n <<-EOF
user
ls
mget
!ls
EOF

You can check script results as echo ${?} after EOF statement.

hth.
Easy to suggest when don't know about the problem!
Muthukumar_5
Honored Contributor

Re: script to login to ftp and check

ftp -i -n <<-EOF
user
ls
mget
!ls
EOF 1>/tmp/output.log 2>/tmp/error.log
if [[ ${?} -eq 0 ]]
then
echo "FTP: Success $(date)"
else
echo "FTP: Failure $(date)"
fi

You can view /tmp/output.log for STDOUT messages and /tmp/error.log for STDERR informations like problem with port or connectivity problem.

PS: Change the commands you want to execute there.

hth.
Easy to suggest when don't know about the problem!
Ivan Ferreira
Honored Contributor

Re: script to login to ftp and check

I think that echo $? wont work to identify if there where a problem with the transacion, for example:

# echo bye |ftp -i localhost
ftp: connect: Connection refused
# echo $?
0

You should use the log file created as described above and find for a "transfer ok" message. The message vary depending of the ftp server.

For example, for vsftpd:


227 Entering Passive Mode (127,0,0,1,75,234)
150 Opening BINARY mode data connection for file (0 bytes).
226 File send OK.

So, the verification should be something like:

CORRECT=`grep -w OK $LOGFILE |wc -l`

if [ $CORRECT = 1 ]; then
echo "Transfer correct"
else
echo "There was a problem with the file transfer, check $LOGFILE"
fi
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
yuva_mca
Occasional Advisor

Re: script to login to ftp and check

#!/bin/bash
ftp -i -n 192.0.0.1 <<-EOF
user anonymous anonymous
ls
EOF

This is the script I have used to test. But it is getting hang. so I have killed the process. I hope as per the script it won't take the plain password. Some one please write the script and test it and let me know. It would be great help for me.

Thanks
Yuva
Ivan Ferreira
Honored Contributor

Re: script to login to ftp and check

You last command should be "bye" to end the ftp transaction.
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
yuva_mca
Occasional Advisor

Re: script to login to ftp and check

#!/bin/bash
ftp -i -n 192.0.0.1 <<-EOF
user anonymous anonymous
ls
bye
EOF

I am using the above script. Still it is get hang. Please check your system and let me know.

Thanks
Yuva
Ivan Ferreira
Honored Contributor

Re: script to login to ftp and check

Remove the - in <<-EOF, should be <
Ensure that the last EOF is not indented.

#!/bin/bash
ftp -i -n 192.0.0.1 <user anonymous anonymous
ls
bye
EOF
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
yuva_mca
Occasional Advisor

Re: script to login to ftp and check

#!/bin/bash
ftp<<**
open 192.1.1.1
anonymous
ls
**

The above script is working. But It is asking the password while executing the script. When I mentioned the password in the script is not taking.It seems we need to give the password at the time of executing the script.

Please check the same and let me know. I donn't want to enter the password manually. we need to mention the password in the script and it suppose to take the password while executing the script. Please suggest the same.

Thanks
Yuvaraj
Muthukumar_5
Honored Contributor

Re: script to login to ftp and check

Use like,

ftp -i -n 192.1.1.1<<**
user anonymous
ls
**

It will take. If you use open then it will prompt for username and password.

Use the ftp script in my prev. threads.

hth.
Easy to suggest when don't know about the problem!
yuva_mca
Occasional Advisor

Re: script to login to ftp and check

ftp -i -n 192.1.1.1<<**
user anonymous anonymous
ls
**

It is getting hand. Please check and let me know.

Thanks
Yuva