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

I have a list of hosts that I would like to run this script on in a file - how would I call that list and make this script log onto every box on the list and run a command

thank you!!!
#!/usr/bin/perl -w use Net::Telnet; $t = new Net::Telnet(Timeout => 2810, Prompt => '/%/', Host => 'ip address' ); $t->login( "user", "password" ); { $t->cmd( "your command here" ); } $t->close;

Replies are listed 'Best First'.
Re: run command on multiple hosts
by sweetblood (Prior) on Aug 06, 2004 at 19:06 UTC
    Check out Perldoc -f open
    The basic idea tho is:
    # Untested use strict; use Net::Telnet open(FH, "host_list_file") || die "Yikes $!"; while (<FH>) { # your telnet stuff here }
    Update
    Don't forget to use strict;

    HTH

    Sweetblood

Re: run command on multiple hosts (use threads;)
by BrowserUk (Patriarch) on Aug 06, 2004 at 19:57 UTC

    Untested as I don't have access to multiple telnet hosts, but it should work correctly.

    The Net::Telnet stuff is based entirely on your sample. I never used it, nor even read the manual.

    If you're dealing with more than a half a dozen or so threads, it would be better to use a fixed size pool rather than one per... That's slightly more complicated, but only slightly.

    #! perl -slw use strict; use threads qw[ async ]; use Net::Telnet; sub remoteCmd { my( $host, $user, $pass, @cmds ) = @_ my $t = new Net::Telnet( Timeout => 2810, Prompt => '/%/', Host => $host ); $t->login( $user, $pass ); $t->cmd( $_ ) for @cmds; $t->close; } my @hosts = ( [ qw[ 111.111.111.001 user pass ] ], [ qw[ 111.111.111.002 user pass ] ], [ qw[ 111.111.111.003 user pass ] ], [ qw[ 111.111.111.004 user pass ] ], [ qw[ 111.111.111.005 user pass ] ], [ qw[ 111.111.111.006 user pass ] ], [ qw[ 111.111.111.007 user pass ] ], ); my @cmds = ( 'cd ~', 'ls -l' ); ## These three lines do all the work. my $count : shared = @hosts; async{ remoteCMD( @$_, @cmds ); $count-- }->detach for @hosts; sleep 1 while $count--;

    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
    "Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon
Re: run command on multiple hosts
by matija (Priest) on Aug 06, 2004 at 19:34 UTC
    Assuming you don't want to wait for the hosts to execute the command one after the other, take a look at Parallel::ForkManager - it will simplify process management for you.
      hi 1)wt is multiple host process? how can i use multiple host? 2) See i m having 2 lan card in a same pc. i m using my one lan card on statical ip on it i used to run one java based software runs on java console (batch file) and it is ip bases software. and another lan card on DHCP setting for local network. Then how can i used my both network symultanously? Is there any extra setting to be done in my pc?. u can revert me on ketan.chaudhari@relianceada.com Regards Ketan Chaudhari
Re: run command on multiple hosts
by Anonymous Monk on Sep 27, 2018 at 06:56 UTC
    #!/usr/bin/perl -w use Net::Telnet; $t = new Net::Telnet(Timeout => 2810, Prompt => '/%/', Host => '10.201.157.22' ); $t->login( "control" ); { $t->cmd( "set airlink state disabled write reboot" ); } $t->close;

    2018-09-28 Athanasius added code tags