PerlCramps has asked for the wisdom of the Perl Monks concerning the following question:

My fellow monks; being that I am new to perl. I am asking for help with a perl script that would allow me to logon to multiple devices collecting the device name from a text file. Execute the same command on all devices in list and export collected data into one text file call DeviceResults.txt. This works in the AIX host shell.
for server in `cat serverlist`; do printf"20s" $server; ssh -o "BatchM +ode yes" -q$server oslevel -s; done
I am hoping that there is one monk with the help I need. Thanks

Replies are listed 'Best First'.
Re: Multiple Device Logon execute the same command
by ww (Archbishop) on Mar 06, 2015 at 18:14 UTC

    What have you tried? What references have you explored? Did a web search not suggest approaches?

    Short version of the local approach: We're here to help you learn. We're not here to act as code-a-matic. Show -- post your code-- you've made some effort; report -- in detail -- how your attempt fails; and post any warnings or error messages

    See also: Super Search; Tutorials; websites like learn.perl.org; and such texts as Learning Perl



    Questions containing the words "doesn't work" (or their moral equivalent) will usually get a downvote from me unless accompanied by:
    1. code
    2. verbatim error and/or warning messages
    3. a coherent explanation of what "doesn't work actually means.
      Thank You ..
Re: Multiple Device Logon execute the same command
by vinoth.ree (Monsignor) on Mar 06, 2015 at 18:22 UTC
    Hi PerlCramps,

    So you want to write a perl script which reads the list of server name from a file and execute a set of commands in remote server.

    Why dn't you try yourself writing a sample script and execute? Here I am giving you the hints to try or search in google.

    1.How to read and write a text file in perl?

    2.How to execute a command in remote host and extract the output?

    So if you could try writing script and if you face any problem please post as, lot of monks are here to help you.


    All is well. I learn by answering your questions...
      Vinoth.ree I toke your advise and tried to get a little smart with perl. I created this script but I am have a problem getting the result part to read the contents of the varible assigned. Please tell me what I am doing wrong; also if there is a better way I am all ears. Thanks SCRIPT
      #!/usr/bin/perl -w use strict; use warnings; main (@ARGV); sub main { # This array contains the list of hosts my @host = ("ajsfdevbcs11", "ajsfdevbcs12"); my $i; for($i = 0; $host[$i]; $i++) { # message ($host[$i]); } # This is the logon ID my $user = "tsasa067"; # message ($user); # This is the logon password my $security = 'F@therL0^eMe'; # message ($security); # These are the commands I would like to run my @cmd = ("switchname", "chassisshow", "switchstatusshow", "switc +hshow"); my $j; for($j = 0; $cmd[$j]; $j++) { # message ($cmd[$j]); } my @results = 'plink -ssh -pw = $security -l = $user $host'; print $results[0]; } sub message { my $m = shift or return; print ("$m\n"); } sub error { my $e = shift || 'unkown error'; print("$0: $e\n"); exit 0; }
        Hi PerlCramps,

        Did you read how to execute command in remote machine?

        You can execute commands on remote machines from a Perl script using the Net::SSH::Perl module.

        This module allows you to execute a command remotely and receive the STDOUT, STDERR, and exit status of that remote command.

        One big advantage of Net::SSH::Perl over other methods is that you can automate the login process, that way you can write fully automated perl scripts, no console interaction is required in order to authenticate in the remote machine.

        Ex:

        #!/usr/bin/perl use Net::SSH::Perl; my $host = "perlhowto.com"; my $user = "user"; my $password = "password"; #-- set up a new connection my $ssh = Net::SSH::Perl->new($host); #-- authenticate $ssh->login($user, $pass); #-- execute the command my($stdout, $stderr, $exit) = $ssh->cmd("ls -l /home/$user");

        Execution of Net::SSH::Perl commands can be quite slow if you don't have the Math::BigInt::GMP module; so be sure that you have that module installed (or install it if you don't) in order to avoid the slowness problem.


        All is well. I learn by answering your questions...
      thank you I will work on the information you listed.
Re: Multiple Device Logon execute the same command
by Anonymous Monk on Mar 06, 2015 at 18:25 UTC
    Just Looking for help !!!

    Then what you need is not to repost your question in the wrong section less than an hour later, but to read How do I post a question effectively? and have a little patience. Please use <code> tags to format your code properly, and spend some more time describing your expected behavior and what Perl code you have tried so far.