Send "perl -e '...' " command through SSH, from a perl script
Hey guys
I am trying to send a perl -e command to a number of systems using SSH. The command should retrieve some information for me. The problem is, the remote shell tries to interpolate my variables and doesn't get it should take the command literally and just execute it.
Below the code. You don't need to read all of it. The only problem I have is to send the command LITERALLY to the remote shell so it will be executed correctly, without anything in between behaving smart and interpolating variables or even executing parts of the code.
Code:
tgdhipa2@usoadm01:~$ cat getinfo.pl
#!/usr/bin/perl
############################################################################
############################################################################
## ##
## Collect information from a list of machines ##
## ******************************************* ##
## ##
## Copyright (C) 2010 Patrick Hirt (patrick.hirt/at/gmail/dot/com) ##
## ##
## This program is free software: you can redistribute it and/or modify ##
## it under the terms of the GNU General Public License as published by ##
## the Free Software Foundation, either version 3 of the License, or ##
## (at your option) any later version. ##
## ##
## This program is distributed in the hope that it will be useful, ##
## but WITHOUT ANY WARRANTY; without even the implied warranty of ##
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ##
## GNU General Public License for more details. ##
## ##
############################################################################
############################################################################
use strict;
use warnings;
# **************************************************************************#
# Configure program here #
# Command to send to the machine. #
# The resulting string of this command #
# will be displayed per hostname (if any) #
# **************************************************************************#
open(C, "<command");
my $command = <C>;
close(C);
# **************************************************************************#
# Machines to check, seperate with a whitespace #
# **************************************************************************#
my @serverlist = qw( usoadm01 msunadm01 );
# **************************************************************************#
# SSH command used to connect to the machines, #
# in most cases does not need to be modified #
# **************************************************************************#
#my $sshcmd = "ssh -t -q -o " . "\"Batchmode yes\" " . "-o " . "\"ConnectTimeout 5\" " . "-l root ";
my $sshcmd = "ssh -t -q -o " . "\"Batchmode yes\" " . "-l root ";
# **************************************************************************#
# Don't modify program below here (unless necessary) #
# **************************************************************************#
# Declare some stuff
my $sshreturn;
my %output;
my $k;
my @v;
my $count = 0;
my $pointcount = 0;
my $percentage;
my $p1 = 0;
my $p2 = 0;
my $p3 = 0;
my $total = @serverlist;
# Inform the use we're about to start working
print("Collecting information...\n");
# Iterate server list
foreach(@serverlist) {
$count++;
$pointcount++;
if($pointcount == 3){
print(".");
$pointcount = 0;
}
$percentage = ($total / 100) * $count;
if($percentage > 18 and $percentage < 22 and $p1 == 0){
print(" 20% ");
$p1 = 1;
}
if($percentage > 48 and $percentage < 52 and $p2 == 0){
print(" 50% ");
$p2 = 1;
}
if($percentage > 78 and $percentage < 82 and $p3 == 0){
print(" 80% ");
$p3 = 1;
}
$sshreturn = `$sshcmd $_ '$command'`;
unless(!defined($sshreturn) or $sshreturn eq ''){
$output{"$_"} = "$sshreturn";
}
undef($sshreturn);
}
print("\n\nDone!\n");
print "\n";
foreach $k (sort(keys(%output))){
@v = sort(lc($output{"$k"}));
foreach(@v){
s/\n/ /g;
}
print("$k:\n@v\n\n");
}
exit(0);
In case you're wondering about the contents of the "command" file.. Here you go:
I thought about that. Unfortunately, a SCP for every host would double the time the script needs to run. I have 2600 machines to check, which takes my script approx. 40 minutes (just with one SSH command). If it's an scp and an ssh, it will take 2 hours.
I would like the script to be as fast as possible. If I don't find a solution, I'll implement your suggestion.
Hi All,
i am trying to ssh to a remote machine and execute certain command to remote machine through script.
i am able to ssh but after its getting hung at the promt and after pressing ctrl +d i am gettin the out put as
expect: spawn id exp5 not open
while executing
"expect "$" {... (3 Replies)
For the newbies, I should have posted this years ago....
Here is the standard (tiny) "bread and butter" perl script (on Linux) I use in my crontab files to insure key processes are alive ( just in case ! ) like httpd, named, sshd, etc.
The example below if for named......
... (1 Reply)
Hi all,
Can anyone guess why this is happening? I am new to Perl, so please help me to fix this error:
- I have a static html file which calls the cgi-perl script.
HTML Code:
<html>
<head>
<title> Hey Dude! </title>
</head>
<body>
<form method="POST"... (3 Replies)
I'm trying to execute something like this:
exec perl -i -pe 's/\015/\012/g' '${file}'
in my expect script and I get:
error "invalid command name \"perl\".
however, if I run
perl -i -pe 's/\015/\012/g' "/Users/Shared/menu-items.txt"
directly in my terminal, it runs fine. I'm an... (4 Replies)
Seems like it's written out pretty well for a beginner to learn, any thoughts or recommendations for other books? I'd post the link but I can't do that yet.
The book is called "Beginning Perl" by Simon Cozens (2 Replies)
Hello!!!
I'm trying to pass the output from bash command to perl variable in a perl script, and I used the "system" command to execute the bash statment and pass the result to perl string variable, in this perl script I used a variable $file that store data for using it as a regular expression.... (2 Replies)
Hi everybody;
I try to code a script perl to list all services that are active "ON" with a chkconfig -- list
the problem is that code list all services which are active or not.
#!/usr/bin/perl
use strict;
use warnings;
open(FILE,"-|") || exec "chkconfig --list |grep ON";... (2 Replies)
Hi,
I have line in input file as below:
3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL
My expected output for line in the file must be :
"1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL"
Can someone... (7 Replies)
in "cmd" command i want to copy the ouput of the command excuted to a particular file in a directory.
How to do this..??
Ex : $telnet->cmd(String => 'allip:acl=a1;',Prompt => '/</');
i want to copy o/p of the command "allip:acl=a1;" in a log file in a particular directory.
Plz suggest.. (1 Reply)
Hi,
Can anybody help me in writing command "waitfor" for string
"C:\WINNT\Profiles\mfcf0508>" while using net::Telnet module.
I tried the below format :
$telnet->waitfor('/"C\:\WINNT\Profiles\mfcf0508>".*$/i');
Getting error as : pattern match timed-out
Plz help me (3 Replies)