1

I'm in an environment where I can't load outside software. We don't have Net::SSH and I can't load it. I rolled my own using ssh keys and piping ssh. I can run any command now on the remote server without manually logging in and typing it, but I'm trying to capture the output on my own server. I'm having trouble capturing the screen output into a file because of the piped shell.

Here's the very rough generic code:

#!/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 in this case is a text file with IP addresses in it, one per line.

It works to output the rpm -qa to the screen, but I can't capture that output into the $OUT filehandle. I'm just not thinking around this corner and I know I'm really close to getting it.

3
  • 1
    As for Perl, you open-ed the process to write to it, so its STDIN now is attached to the $pipe, that you write to. So you can't get its STDOUT. Instead, you can do exactly the same as in the fine bash solution -- run qx(ssh $pi ...) (backticks), which returns the output. Commented Sep 15, 2016 at 4:55
  • Yeah, that's correct. open is a one-way process --either write or read, but not both. One of the things that I tried in the gyrations was qq(ssh $ip...), but qq is double quotes not backticks. Had I just changed a letter, I would have had it. Or just placed backticks around the command. Commented Sep 15, 2016 at 11:57
  • Correct. Note though that there are a number of ways of running (or open-ing) a process which allow you to capture everything about it. There are many SO posts about that. It's just that in this case you don't need any of it since simple backticks qx does it. Follow the link on this docs page to a spot in perlop where all these are discussed. Commented Sep 15, 2016 at 17:49

1 Answer 1

3

Are you sure you need all that perl for this? How about a basic for-loop? If you run the command "ssh $ip rpm -qa" on your local machine, the output will go to your stdout and you can do whatever you like with it.

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

or all on one line:

(for ip in `cat iplist.txt`; do echo -e "${ip}\n----------------" ; ssh $ip rpm -qa ; echo "++++++++++++++++"; done) > rpms
Sign up to request clarification or add additional context in comments.

2 Comments

Sometimes, late at night, we have these "oh duh" moments. This is one of them. That works. I'll run with it from here. Thanks!
Glad to help, and I've had plenty of those moments myself. I like to try and answer one question when stackoverflow has helped me out with my own duh moments and there have been several today ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.