in reply to Re: Multiple Device Logon execute the same command
in thread Multiple Device Logon execute the same command

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; }

Replies are listed 'Best First'.
Re^3: Multiple Device Logon execute the same command
by vinoth.ree (Monsignor) on Mar 27, 2015 at 14:27 UTC
    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...
      I am doing this on a Windows System. I am having problems with Net::SSH::Perl because I could not get the MATH::Pari to install.

        I'd recommend checking out Strawberry Perl. Looking at the latest 32-bit and 64-bit versions available:

        • 32-bit version comes with Net::SSH2, Math::Pari and Math::BigIntGMP modules.
        • 64-bit version comes with Net::SSH2 and Math::BigIntGMP modules.

        I personally have not used any Net::SSH modules, but I believe that Net::SSH2 should be able to do what you're wanting to do. I have to admit that after taking a very quick look at the documentation for Net::SSH2, I'm not sure if I would know how to use it properly. However, I've been meaning to check out Control::CLI, which will use Net::SSH2 for SSH connections. The documentation provides some example code to log into multiple devices and issue commands to multiple devices, which is the basic functionality that you're after. Since the interface looks a lot like the interface of Win32::SerialPort (which I have used before), I think that I personally would have a small learning curve with trying to learn how to use Control::CLI.