我处于无法加载外部软件的环境中。我们没有Net::SSH,我无法加载它。我使用ssh键和管道ssh滚动了自己的代码。我现在可以在远程服务器上运行任何命令,而无需手动登录和键入命令,但是我试图捕获自己服务器上的输出。由于使用管道 shell ,我无法将屏幕输出捕获到文件中。

这是非常粗糙的通用代码:

#!/usr/bin/perl -w 
## 
  
my ($ip)        = @ARGV; 
my $rpm_logfile = "rpms"; 
  
print "The IP file is $ip\n"; 
  
open(my $IN, "<", $ip) || die "Could not find filename $ip $!"; 
open(my $OUT, ">>", $rpm_logfile) || die "Could not open file $rpm_logfile $!"; 
  
while (<$IN>) { 
  chomp; 
  my $my_ip = $_; 
  
  if (not defined $my_ip) { 
    die "Need an IP after the command.\n"; 
  } 
  # ssh key was set up, so no password needed 
  open my $pipe, "|-", "ssh", "$my_ip", or die "can't open pipe: $!"; 
  
  # print the machine IP in the logfile 
  # and pretty print the output. 
  print $OUT "$my_ip \n***************\n"; 
  
  # run the command on the other box via the ssh pipe 
  print {$pipe} "rpm -qa"; 
  
}; #end while INFILE 
  
close $IN; 
close $OUT; 

在这种情况下,@ ARGV是一个文本文件,其中包含IP地址,每行一个。

它可以将rpm -qa输出到屏幕,但是我无法将输出捕获到$ OUT文件句柄中。我只是不想在这个角落附近,我知道我真的很接近实现它。

请您参考如下方法:

您确定为此需要所有perl吗?基本的for循环怎么样?如果在本地计算机上运行命令“ssh $ip rpm -qa”,则输出将转到标准输出,您可以使用它执行任何操作。

$ for ip in `cat iplist.txt` 
> do 
>   echo -e "${ip}\n----------------" 
>   ssh $ip rpm -qa 
>   echo "++++++++++++++++" 
> done 

或全部一行:
(for ip in `cat iplist.txt`; do echo -e "${ip}\n----------------" ; ssh $ip rpm -qa ; echo "++++++++++++++++"; done) > rpms 


评论关闭
IT序号网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!