Send "perl -e '...' " command through SSH, from a perl script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Send "perl -e '...' " command through SSH, from a perl script
# 1  
Old 07-14-2010
Bug 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:

Code:
perl -e 'my $keyf; my $foo; my $bar; my $comment; my @output; open(F, "</etc/ssh/sshd_config"); \\
while(<F>){ if(m/AuthorizedKeysFile/){s/%u/root/; s/AuthorizedKeysFile //; $keyf = $_;}} close(F); open(K, "<$keyf"); \\
while(<K>){chomp; ($foo, $bar, $comment) = split(/ /); $comment = $comment . " "; push(@output, $comment); $foo = undef; $bar = undef;} close(K);foreach(@output){print;}'

Any help is greatly appreciated!

EDIT: I managed to solve part of the problem by removing the double quotes inside the command and replacing them with qq|<...>| .

The error message now is:

Code:
bash: -c: line 0: syntax error near unexpected token `F,'

I assume I got around part of the quoting problem, although I don't understand why bash fails at F, and not at open().

Still in desperate need of help!

Thank you.

Last edited by clrg; 07-14-2010 at 09:06 AM.. Reason: Additional information
# 2  
Old 07-14-2010
The most resaonable approach is:
Code:
scp myperl.pl me@remotehost; ssh 'me@remotehost ; chmod +x myperl.pl; ./myperl.pl'

# 3  
Old 07-14-2010
Thank you for your response.

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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Expect: spawn id exp5 not open while executing "expect "$" { send "sudo su -\r" }"

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)
Discussion started by: Siddharth shivh
3 Replies

2. Tips and Tutorials

My "Bread and Butter" Process Keep Alive Perl Script....

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)
Discussion started by: Neo
1 Replies

3. Shell Programming and Scripting

Perl failure with "main::$fn" used only once:" in error logs

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)
Discussion started by: bashily
3 Replies

4. Shell Programming and Scripting

exec perl in expect script yields "invalid command"

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)
Discussion started by: dpouliot
4 Replies

5. Shell Programming and Scripting

What do people think of the "Beginning Perl" book for learning Perl?

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)
Discussion started by: budfoxcat
2 Replies

6. Shell Programming and Scripting

Problem using "system" command in perl

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)
Discussion started by: evolabo
2 Replies

7. Shell Programming and Scripting

Perl script with "chkconfig --list" cmd

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)
Discussion started by: bassma
2 Replies

8. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

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)
Discussion started by: shis100
7 Replies

9. Shell Programming and Scripting

Need help on use of "cmd" command in net::Telnet module in PERL

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)
Discussion started by: sudhakaryadav
1 Replies

10. Windows & DOS: Issues & Discussions

Need Help on "waitfor" command in net::Telnet Module in PERL

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)
Discussion started by: sudhakaryadav
3 Replies
Login or Register to Ask a Question