自动化FTP会话

时间:2013-08-14 14:24:12

标签: perl bash ftp ksh

我在perl脚本中有以下摘录来自动化FTP会话,我希望有人可以解释它是如何工作的。

system("rsh some_server ftp -in ftp.something.com << !
user anonymous someone\@somewhere.org
some ftp commands
bye");

背景。这个perl脚本在Linux机器上运行,它可以远程转换到Solaris机器。必须从Solaris计算机执行FTP会话,因为FTP站点执行IP地址检查。

以前这个脚本直接在Solaris机器上运行(即它没有使用rsh)我把它破解并想出了这个似乎有效。但是我不知道如何,特别是我不理解第一行末尾的<< !位。它看起来有点像文件,但我不太确定。

欢迎任何解释。

2 个答案:

答案 0 :(得分:2)

你是对的,<<是一个heredoc,通过以下警告(我在取出rsh命令时得到的)明确了这一点:

sh: line 2: warning: here-document at line 0 delimited by end-of-file (wanted `!')

构造

<< HEREDOC

作为标准输入读取所有内容,从HEREDOC到仅包含HEREDOC 的行或最多为文件结尾字符。在命令之后放置它时,它等同于

command < file

其中file包含heredoc中的文本。在您的情况下,HEREDOC而不是!分隔符为!,因此ftp 传递给!,而是$ cat file user anonymous someone\@somewhere.org some ftp commands bye $ ftp -in ftp.something.com < file 之后的所有内容是的。这相当于

rsh

system("rsh some_server ftp -in ftp.something.com << HEREDOC user anonymous someone\@somewhere.org some ftp commands bye HEREDOC"); 接受整个命令并在远程主机上运行它。

如user1146334的回答所示,此命令不会对最不意外的主体起作用。至少,通过将其更改为

来减少混淆
{{1}}

或者甚至更好,正如评论中提到的mpapec一样,使用Net::FTPNet::SSH2

答案 1 :(得分:0)

您是否看过该手册页?

-i    Turns off interactive prompting during multiple file transfers.

-n    Restrains ftp from attempting “auto-login” upon initial connection.  If auto-login is enabled, ftp will check the .netrc (see netrc(5)) file in the user's
       home directory for an entry describing an account on the remote machine.  If no entry exists, ftp will prompt for the remote machine login name (default is
       the user identity on the local machine), and, if necessary, prompt for a password and an account with which to login.


 The client host and an optional port number with which ftp is to communicate may be specified on the command line.  If this is done, ftp will immediately attempt
 to establish a connection to an FTP server on that host; otherwise, ftp will enter its command interpreter and await instructions from the user.  When ftp is
 awaiting commands from the user the prompt ‘ftp>’ is provided to the user.  The following commands are recognized by ftp:

 ! [command [args]]
             Invoke an interactive shell on the local machine.  If there are arguments, the first is taken to be a command to execute directly, with the rest of
             the arguments as its arguments.

所以基本上你是ftp'in并且在线提供新命令而不是从文件中提供。