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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checking FTP connectivity to remote host

Status
Not open for further replies.

talisker123gjs

Technical User
Aug 6, 2003
14
GB
Hi
If I try to ftp an invalid IP address, I get no result (which is obvious) and the command seems to be hanging.

HOW DO I TRAP THIS KIND OF ERROR ?

For eg., I am using FTP in a "here" document within a shell script. If the remote system isn't available or if there is a network problem, how can my script trap such errors?

I am using the ksh on SunOS 5.6.

Thanks
 
'ping' first.
'ftp' next checking the return status of 'ftp' itself [${?}].

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Best probably to just use a more reliable language
than shell for this.
I don't understand why people continue to use here docs
for automating ftp when better tools are all over the
place including expect: http:/expect.nist.gov, tcl and
perl.

Brief tcl example:
Code:
#!/usr/bin/tclsh
package require ftp 2.3.1
#procedure to prompt user for input#
proc getin {msg} {
   puts -nonewline stdout $msg
   flush stdout
   return [gets stdin]
}

proc progressive {bytes} {
     puts -nonewline stdout &quot;#&quot;
     flush stdout
     return $bytes
}


   if {![llength $argv]} { error &quot;Cannot call script without host(1) and username(2)&quot;}
      set host [lindex $argv 0]
      set uname [lindex $argv 1]


    if {[set handle [ftp::Open $host $uname [getin &quot;Please enter your password: &quot;]  -progress progressive]] >= 0} {
       puts &quot;Opened connection successfully to $host\n&quot;
       after 2500 
       ftp::Close $handle
     } else {
          error &quot;Unable to open connectiion to host: $host&quot;
     }

 
1. tcl and expect are not generally available for some OS
distributions. Implementing a solutions with a 'gnerally
available' tools sometimes is a prerequisite.

2. not being fluent with perl is another reason.

3. not being fully aware of the 'gottachas' of the 'here-docs'
and knowing the ways around it is another reason.

4. you're as good as your 'tool-box' is. And then you have
'tek-tips'.

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
You're right vlad..but if there is a chance
of saving yourself the pain of writing a
script that may fail due to any number of
reasons, AND doesn't really provide
good network error detection, almost any
other solution is preferable IMO.


 
marsd
I have worked on Applications such as CONNECT DIRECT and XFB.CFT for doing FTP stuff. These applications provide secure FTP and good error-trapping.
However, for certain reasons, I was asked to use a shell script and hence I wanted to know if there're any error trapping mechanisms in shell scripts. I didn't have much hope that the Korn shell would provide me with a solution, but I started this thread to determine if there is a korn shell solution that I'm unaware of. Anyway, thanks for your Tcl code and relevant information.

vgersh99
Even if the remote system is available, checking for an exit status of 0 (ksh) need not always indicate successful FTP. Also, I agree with your 4-point post.
 
the most common way of 'error trapping mechanisms' with here-doc with ftp is to redirect the stderr of of ftp and 'parse' the resulting error file for errors/success:

ftp -n -v > 2> /tmp/errFile.txt <<EOF
ftp commands here
....
EOF

parseErrFile '/tmp/errFile.txt'

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top