【问题标题】:automating FTP session自动化 FTP 会话
【发布时间】:2013-08-16 13:31:39
【问题描述】:

我从 perl 脚本中摘录了以下用于自动化 FTP 会话的摘录,我希望有人能解释它是如何工作的。

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

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

以前这个脚本直接在 Solaris 机器上运行(即它不使用 rsh),我修改了它并想出了这个似乎可行的方法。但是我不知道如何,特别是我不理解第一行末尾的&lt;&lt; ! 位。它看起来有点像这里的文档,但我不太确定。

欢迎任何解释。

【问题讨论】:

  • 它看起来很脆弱且容易出错,我会坚持使用 Net::FTP 和 Net::SSH2

标签: perl bash ftp ksh


【解决方案1】:

你说得对,&lt;&lt; 是一个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 获取整个命令并在您的远程主机上运行它。

正如用户 1146334 的回答所示,此命令不符合最小意外原则。至少,通过将其更改为来减少混乱

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

或者更好,正如 cmets 中提到的 mapeec,使用 Net::FTP 和 Net::SSH2。

【讨论】:

  • 我喜欢使用 'HEREDOC' 作为 heredoc 分隔符的想法。必须同意这段代码令人困惑。显然原作者也一头雾水。使用适当的 perl 模块会更好,但当然我正在使用现有代码,重写所有内容并不总是可行的。至少他们是这么告诉我的。
【解决方案2】:

你看过手册页吗?

-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'​​ing 并在每行内联而不是从文件中提供一个新命令。

【讨论】:

  • 是的,我确实看过手册页,但我仍然不明白。 &lt;&lt; 是做什么的? ! 退出 FTP 会话,这有什么帮助?我想要的是逐步解释脚本的工作原理。哪些部分正在由哪些流程等进行迭代。
猜你喜欢
  • 2012-05-12
  • 1970-01-01
  • 2014-01-19
  • 1970-01-01
  • 1970-01-01
  • 2023-04-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-01
相关资源
最近更新 更多