Operating System - Linux
1820190 Members
4063 Online
109620 Solutions
New Discussion юеВ

perl telnet on multiple host

 
SOLVED
Go to solution
Jan Sladky
Trusted Contributor

perl telnet on multiple host

hi all,
I try telnet on multiple host with perl. Hosts are stored in hosts.txt but I don't now how to use them. (I can only use ARGV[0] from line)
telnet.pl 10.3.0.29

----
#!/usr/bin/perl

use strict;
use Net::Telnet ();

my $host = $ARGV[0];
my $username = "user";
my $password = "passwd";
my $conn = new Net::Telnet (Timeout => 1,
Errmode => 'return',
);


$conn->open($host);
$conn->login($username,$password);
print $conn->cmd('uname -a');
$conn->close();
-----

I tried

for ($i = 0; `cat hosts.txt`; ++$i) but it doesn't work.

How can I retrive hosts form file and use them ?

thank you

Jan
GSM, Intelligent Networks, UNIX
3 REPLIES 3
Senthilmurugan
Frequent Advisor
Solution

Re: perl telnet on multiple host

Hello Jan,

You can retrieve the content the file using the following method.

#!/usr/bin/perl

use strict;
use Net::Telnet ();

open(FILE, "telnet.txt") or die("Unable to open file");
@data = ;

$ending_value = scalar(@data);

for($counter=0 ; $counter < $ending_value ; $counter++)
{

my $host = $data[$counter];
my $username = "user";
my $password = "passwd";
my $conn = new Net::Telnet (Timeout => 1,Errmode => 'return',);
$conn->open($host);
$conn->login($username,$password);
print $conn->cmd('uname -a');
$conn->close();

}

close(FILE);


use appropriate username and password.

NOTE: You should have installed the Net/Telnet.pm Perl module in your system


Regards,
Senthil Murugan


Slawomir Gora
Honored Contributor

Re: perl telnet on multiple host

Hi,

you can do:


unless (open(FILE, $hostfile)) {
print STDERR "Can't open $hostfile\n";
return;
}
while (my $host = ) {
chop($host);
....
here your code
....
}
Jan Sladky
Trusted Contributor

Re: perl telnet on multiple host

thanks, it works fine ;-)
Only my() funct. had to be added

#!/usr/bin/perl

use strict;
use Net::Telnet ();

open(FILE, "hosts.txt") or die("Unable to open hosts.txt ");
my @hosts = ;
my $ending_value = scalar(@hosts);

for(my $counter=0 ; $counter < $ending_value ; $counter++)
{

my $host = $hosts[$counter];
my $username = "user";
my $password = "passwd";
my $conn = new Net::Telnet (Timeout =>1,
Errmode => 'return',);
$conn->open($host);
$conn->login($username,$password);
print $conn->cmd('uname -a');
$conn->close();

}

close(FILE);
GSM, Intelligent Networks, UNIX