如何捕获远程命令的输出然后发出更多远程命令?

时间:2009-07-29 06:32:11

标签: perl server-administration

我正在运行一个脚本,它登录到服务器然后执行命令 “passwd -n 0 -x 99999 -i -1 debug”用于消除调试用户的老化。 如果用户调试不存在,那么我想创建用户调试,更改密码,然后执行上述命令进行老化。

我该怎么办?

此致 婆薮仙

3 个答案:

答案 0 :(得分:1)

来自perlfunc(1)

          system LIST
               [...]
               The return value is the exit status of the program as returned
               by the "wait" call.  To get the actual exit value, shift right
               by eight (see below).

因此:

my $ret = system(qw/passwd -n 0 -x 99999 -i -1 debug/);
if ($ret != 0) {
  # failure handling code here
}

答案 1 :(得分:0)

使用puppet

如果您真的坚持手动操作,请使用getent passwd debug检查用户是否存在:

if [ $(getent passwd debug | wc -l ) = 0 ]; then
    adduser debug
fi

答案 2 :(得分:0)

我建议使用类似Expect的内容。它为您处理交互性。您可以登录服务器,执行命令,检查输出,发送更多输入等。如果您正在进行大量远程服务器管理,那么这是一个非常方便的工具。在The Perl Review第4.2期(2008年春季)

中甚至有一篇关于它的文章