From d72fcd70c44a4bd2e3dbe35027a52f649ffae491 Mon Sep 17 00:00:00 2001 From: Dobrica Pavlinusic Date: Tue, 8 Jan 2019 11:42:23 +0100 Subject: [PATCH] read commands from STDIN if used as pipe --- dell-switch.pl | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/dell-switch.pl b/dell-switch.pl index 82debda..c48184f 100755 --- a/dell-switch.pl +++ b/dell-switch.pl @@ -3,6 +3,9 @@ use warnings; use strict; use autodie; +# example usage as pipe: +# ./ips | sed 's/^/ping /' | NO_LOG=1 ./dell-switch.pl sw-dpc + use Net::OpenSSH; use Data::Dump qw(dump); use Time::HiRes qw(sleep); @@ -19,7 +22,13 @@ require 'config.pl'; my $ip = shift @ARGV || die "usage: $0 IP command[ command ...]\n"; $ip = $1 if `host $ip` =~ m/has address (\S+)/; my @commands = @ARGV; -@commands = unless @commands; +if ( ! @commands && ! -t STDIN && -p STDIN ) { # we are being piped into + while(<>) { + push @commands, $_; + } +} else { + @commands = unless @commands; +} warn "\n## ssh $ip\n"; my $ssh = Net::OpenSSH->new($ip, user => $login, passwd => $passwd, default_ssh_opts => [-o => "StrictHostKeyChecking=no"]); -- 2.20.1