Sponsored Content
Top Forums Shell Programming and Scripting Perl telnet to router run commands from file Post 302873529 by numele on Tuesday 12th of November 2013 10:04:03 AM
Old 11-12-2013
Perl telnet to router run commands from file

I have a perl script that is called with a router name command list file and output file. The command file can be very large, up to 3k of commands. At first I dumped the command list file to an array and ran the entire file on the router and captured the output. It worked ok for a command list in the hundreds but not thousands. So I changed it to the below to read each line of the command list and wait for a prompt.

It works well but since I am a perl beginner I was hoping some experienced perl people can have a look and see if this can be done any better.

Code:
called like this: perl myscript.pl -r <router_name> -f <command_file> -o <output_file>
 
#!/usr/local/bin/perl
use Net::Telnet;
use diagnostics;
use strict;
use Getopt::Std;
my %opts = ();
getopts('f:o:r:', \%opts);
my $file = $opts{'f'};
my $router = $opts{'r'};
my $output = $opts{'o'};
open PASSFILE, "</home/bin/secure/pass" or die $!;
my $password = <PASSFILE>;
chomp $password;
close PASSFILE;
$password =~ /^\s*([^:]+)\:([^:]+)\s*$/;
my $USERNAME = $1;
my $PASSWORD = $2;
my $t = new Net::Telnet (Timeout=>600,Errmode=>'return');
   $t->max_buffer_length(100000000);
   $t->open($router);
   $t->waitfor('/Telnet password[: ]*/i');
   $t->print("$PASSWORD\n");
   $t->waitfor('/.*\>*/');
   $t->print("enable\n");
   $t->waitfor('/password[: ]*$/i');
   $t->print("$PASSWORD\n");
   $t->waitfor('/.*\#$/');
   $t->print("term length 0\n");
   $t->waitfor('/.*\#$/');
  open CMDS,"<$file" or die $!;
    foreach my $line (<CMDS>) {
     chomp($line);
         $t->print("$line\n");
           my @config = $t->waitfor('/.*\#$/');
           open OUT,">>$output" or die $!;
           foreach my $config (@config) {
           print OUT "$config";
           }
           close OUT;
   }
   close CMDS;
   $t->close;

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl telnet to cisco router and compare the ping ms

All Please help, i will telnet to router to obain the ping status and compare, if higher than normal latency, i will have further action.. if i do the telent and in perl script then .... e.g the result i obtain from the router will be =' Success rate is 100 percent (5/5), round-trip... (4 Replies)
Discussion started by: optimus
4 Replies

2. Shell Programming and Scripting

Script to run on a router

I want to make a Perl script to telnet to a router and apply commands, and get the output of the commands in a file. Can any one help me in that? (2 Replies)
Discussion started by: moutaz1983
2 Replies

3. Shell Programming and Scripting

Webpage to Telnet via Perl and Expect: Telnet problem?

Somewhat long story: I have a simple Perl CGI script that uses Expect to Telnet to a device and grab some data, and then spits it back to Perl for display on the Webpage. This works for many devices I've tried, but one device just fails, it keeps rejecting the password on this device, only... (1 Reply)
Discussion started by: jondo
1 Replies

4. UNIX for Dummies Questions & Answers

Command to run multiple commands from a file.

I need a command, which could run mutliple commands from a file. Let's say, I have mv fileA1 fileB1 mv fileA2 fileB2 ..... mv fileA20 fileB20 I put these commands in a file, then I need a command to run the file as a whole so that I don't need to type 20 times... Anyone tell me how to... (8 Replies)
Discussion started by: kaixinsjtu
8 Replies

5. UNIX for Dummies Questions & Answers

check for a file and run some commands

Hi all, Can you guys please help me with this... I am on AIX and need to prepare a script which will 1. check for a file named do_backup in the current file system 2. If the file exists i need to run some commands and exit, if the file doesn't exist then sleep for 15 mins and try... (2 Replies)
Discussion started by: family_guy
2 Replies

6. Shell Programming and Scripting

Telnet to router and execute command

I am trying to write a shell script to execute some commands.. telnet <IP address of the router> wait 10 echo "username" echo "password" sh log exit but i am unable to execute it properly ..please help me .. thanks in advance Sri (1 Reply)
Discussion started by: srikanthus2002
1 Replies

7. Shell Programming and Scripting

Perl: connect to network devices, run set of commands

I am trying to write a script for my own use that will allow me to connect to network devices, then run a set of commands. I start with a list of ips in a text file. Each ip is on its own line. I start with a second file of commands. Each command on one line. for illustration .. the cmd.txt... (2 Replies)
Discussion started by: popeye
2 Replies

8. Linux

How to run commands with pipe from text file?

Hello, I have standard loop while read -r info; do command $info done < info in info text file I have multiple commands each on line that I want to execute. When I used them in console they worked, but not with this loop. This is one of the commands in info file: grep... (4 Replies)
Discussion started by: adamlevine
4 Replies

9. Shell Programming and Scripting

Shell script run in a case statement call to run a php file, also Perl

Linux System having all Perl, Python, PHP (and Ruby) installed From a Shell script, can call a Perl, Python, PHP (or Ruby ?) file eg eg a Shell script run in a case statement call to run a php file, also Perl or/and Python file??? Like #!/usr/bin/bash .... .... case $INPUT_STRING... (1 Reply)
Discussion started by: hoyanet
1 Replies

10. UNIX for Beginners Questions & Answers

Script require to do telnet & run few commands

I am new in scripting, I need script(BASH) which do telenet to one of the elements & run few commands , after running it successfully it will check status of files & exit. Help is greatly appreciated 🙏 (2 Replies)
Discussion started by: Vinesh_123
2 Replies
Config::Pit(3pm)					User Contributed Perl Documentation					  Config::Pit(3pm)

NAME
Config::Pit - Manage settings SYNOPSIS
use Config::Pit; my $config = pit_get("example.com", require => { "username" => "your username on example", "password" => "your password on example" }); # if the fields are not set, open setting by $EDITOR # with YAML-dumped default values (specified at C<require>). # use $config->{username}, $config->{password} DESCRIPTION
Config::Pit is account setting management library. This library automates editing settings used in scripts. Original library is written in Ruby and published as pit gem with management command. You can install it by rubygems: $ sudo gem install pit $ pit set example.com # open setting of example.com with $EDITOR. And Config::Pit provides ppit command which is pit command written in Perl. See: $ ppit help FUNCTIONS
Config::Pit::get(setting_name, opts) Get setting named "setting_name" from current profile. my $config = Config::Pit::get("example.com"); This is same as below: my $config = pit_get("example.com"); opts: require Specify fields you want as key and hint (description or default value) of the field as value. my $config = pit_get("example.com", require => { "username" => "your username on example.com", "password" => "your password on example.com" }); "require" specified, module check the required fields all exist in setting. If not exist, open the setting by $EDITOR with merged setting with current setting. Config::Pit::set(setting_name, opts) Set setting named "setting_name" to current profile. Config::Pit::set("example.com"); #=> will open setting with $EDITOR opts: data Config::Pit::set("example.com", data => { username => "foobar", password => "barbaz", }); When "data" specified, will not open $EDITOR and set the data directly. config Config::Pit::set("example.com", config => { username => "config description or default value", password => "same as above", }); Open $EDITOR with merged setting with specified config. Config::Pit::switch(profile_name); Switch profile to "profile_name". Profile is setting set: $ pit get foobar # foo bar... $ pit switch devel Switch profile to devel $ pit get foobar # bar baz $ pit switch Switch profile to default $ pit get foobar # foo bar... AUTHOR
cho45 <cho45@lowreal.net> LICENSE
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. SEE ALSO
<http://lowreal.rubyforge.org/pit/> is pit in Ruby. bin/ppit is pit command in Perl. perl v5.10.1 2010-01-14 Config::Pit(3pm)
All times are GMT -4. The time now is 02:40 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy