Perl variables inside Net::Telnet::Cisco Module doesn't work

I am writing perl script to configure Cisco device but Variables inside Net::Telnet::Cisco Module doesn't work and passed to device without resolving.

Please advise.

here is a sample of script:

use Net::Telnet::Cisco;
$device = "10.14.199.1";
($o1, $o2, $o3, $o4) = split(/\./,$device);
print "$o1\n";
print "$o2\n";
print "$o3\n";
print "$o4\n";
my $session = Net::Telnet::Cisco->new(Host => '$device');
$session->login('cisco', 'cisco');
$session->cmd('conf t');
@output = $session->cmd('ip dhcp excluded-address 172.18.$o3.1 172.18.$o3.20');
print "@output\n";
@output = $session->cmd('ip dhcp excluded-address 10.14.$o3.130 10.14.$o3.145');
print "@output\n";
 
$session->close;

The variables with red color don't resolved.
please advise.

---------------------------

I got the following output:

C:\Users\Administrator>perl c:\Scripts\dhcp.pl
Can't locate auto/Net/Telnet/Cisco/autosplit.ix in @INC (@INC contains: F:/Perl/
site/lib F:/Perl/lib .) at F:/Perl/lib/AutoLoader.pm line 173.
at F:/Perl/lib/Net/Telnet/Cisco.pm line 18
10
14
199
1
unknown remote host: $device at c:\Scripts\dhcp.pl line 12

when I manually resolve the $device variable, I got the following output:

 
C:\Users\Administrator>perl c:\Scripts\dhcp.pl
Can't locate auto/Net/Telnet/Cisco/autosplit.ix in @INC (@INC contains: F:/Perl/
site/lib F:/Perl/lib .) at F:/Perl/lib/AutoLoader.pm line 173.
at F:/Perl/lib/Net/Telnet/Cisco.pm line 18
10
14
199
1
Last command and router error:
R2(config)#ip dhcp excluded-address 172.18.$o3.1 172.18.$o3.20
                                                        ^
% Invalid input detected at '^' marker.

-----------------------
note: ^ is under $ not as it appears.
------------------------

The answer is quite simple: single quotes, unlike double quotes, won't interpolate the content of the variable, but take the string as is. See perldoc perlop for details on quoting.

1 Like

Hello,

Thanks for your reply, it work fine but I have another problem.

this the whole script:

use Net::Telnet::Cisco;
$device = "10.14.199.1";
($o1, $o2, $o3, $o4) = split(/\./,$device);
print "$o1\n";
print "$o2\n";
print "$o3\n";
print "$o4\n";
my $session = Net::Telnet::Cisco->new(Host => "$device");
$session->login("cisco", "cisco");
$session->cmd("conf t");
@output = $session->cmd("ip dhcp excluded-address 172.18.$o3.1 172.18.$o3.20");
print "@output\n";
@output = $session->cmd("ip dhcp excluded-address 10.14.$o3.130 10.14.$o3.145");
print "@output\n";
@output = $session->cmd("ip dhcp excluded-address 10.14.$o3.230 10.14.$o3.254");
print "@output\n";
@output = $session->cmd("ip dhcp pool Users-pool");
print "@output\n";
@output = $session->cmd("network 10.14.$o3.128 255.255.255.128");
print "@output\n";
@output = $session->cmd("default-router 10.14.$o3.129");
print "@output\n";
@output = $session->cmd("dns-server 10.11.0.80 10.11.0.81 10.11.0.79 192.168.250.250 192.168.250.251");
print "@output\n";
@output = $session->cmd("ip dhcp pool Remedy-pool");
print "@output\n";
@output = $session->cmd("network 10.15.$o3.0 255.255.255.0");
print "@output\n";
@output = $session->cmd("default-router 10.15.$o3.1");
print "@output\n";
@output = $session->cmd("dns-server 10.11.0.80 10.11.0.81 10.11.0.79 192.168.250.250 192.168.250.251");
print "@output\n";
@output = $session->cmd("ip dhcp pool Voice");
print "@output\n";
@output = $session->cmd("network 172.18.$o3.0 255.255.255.0");
print "@output\n";
@output = $session->cmd("default-router 172.18.$o3.1");
print "@output\n";
@output = $session->cmd("option 150 ip 172.17.40.10");
print "@output\n";
$session->close;

but I have the following error:

C:\Users\Administrator>perl c:\Scripts\dhcp.pl
Can't locate auto/Net/Telnet/Cisco/autosplit.ix in @INC (@INC contains: F:/Perl/
site/lib F:/Perl/lib .) at F:/Perl/lib/AutoLoader.pm line 173.
 at F:/Perl/lib/Net/Telnet/Cisco.pm line 18
10
14
199
1
 
command timed-out at c:\Scripts\dhcp.pl line 17

As for the autosplit.ix error: how did you install the module, and what Perl distribution do you use? ActiveState?

The timeout probably means that the device didn't answer after a certain period of time. Check the documentation for Net::Telnet::Cisco on how to increase the timeout, or if the device has a problem with the command at line 17.

1 Like

Thanks for your interest.

I have active perl 5.8.9 on windows but I don't find the "Net::Telnet::Cisco" module by default so I download only "Cisco.pm" file and paste it in perl. (it seems that this is not correct, isn't it?).

Please advise where I can find the whole package and how to install it??

on the other hand the device doesn't have a problem with any command.

Thanks,

It's available through ActiveStates PPM, although not in the default repository. Installation instructions can be found here.

As for the timeout, look through the documentation for the new() method, there's a timeout parameter that can be set globally, as well as for each cmd() individually. Test the commands with a device to find the time it needs to complete successfully.