1

我正在使用以下代码连接 ftp 节点。我只想知道如何检查我是否无法连接到 ftp 服务器或 ftp 服务器没有响应。在任何情况下,它都会提醒我 ftp 服务器正常或关闭。实际上我想嵌入普通的 bash 代码来检查连接性。

#!/bin/ksh  
ftp -nv <<EOF  
open xxx.xx.xx.xx  
user xxx xxxxxxxxx  
bye  
EOF  
4

3 回答 3

1

grepping ftp 的输出怎么样?我不确定您的 ftp 版本在成功上传后会返回什么,但类似于:

#!/bin/ksh

(
ftp -nv <<EOF
open xxx.xx.xx.xx
user xxx xxxxxxxxx
bye
EOF
) | grep -i "success"
if [[ "$?" -eq "0" ]]
then
        echo "FTP SUCCESS"
else
        echo "FTP FAIL"
fi

应该管用..

于 2012-05-03T19:38:30.270 回答
0

我之前遇到过同样的问题,通过检查 ftp 命令的输出解决了它。尽管如此,还是觉得它很奇怪,所以我决定使用 PERL。

#!/usr/bin/perl
use strict;
use warnings;
use Net::FTP;

# open connection
my $ftp = Net::FTP->new("127.0.0.1");
if (! $ftp) {
    print "connection failed!";
    exit 1;
}

# in case you would need to test login too
# if (! $ftp->login("username", "password")) {
#    print "login failed!";
#    exit 2;
#}

$ftp->close();
exit 0;
于 2012-05-10T17:20:11.370 回答
0

使用此命令检查 ftp 服务器是否可用:

sleep 1 | telnet ftp.example.com 21 2> /dev/null | grep -c 'FTP'

它能做什么:

  • 通过端口 21 建立与 ftp.example.com 的连接(将端口 22 用于 sftp)
  • 等待一秒钟,然后终止连接
  • 忽略“远程主机关闭的连接”-来自 telnet 的响应为“2> /dev/null”
  • 如果来自寻址服务器的响应包含“FTP”,则返回“1”,否则返回“0”。

如果您要检查的 ftp 服务器的预期欢迎响应与标准响应不同,您可能需要调整 grep 模式“FTP”,标准响应通常如下所示:

   Trying 93.184.216.34...
   Connected to ftp.example.com.
   Escape character is '^]'.
   220 FTP Service
于 2019-11-05T10:15:11.390 回答