Net::Telnet (match prompt)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Net::Telnet (match prompt)
# 1  
Old 11-30-2010
Net::Telnet (match prompt)

Hi,
The code below is used to telnet to list of devices and configure them. The program executes in this manner:
1. telnet to the first device in file.txt
2. one the telnet command is executed a "press any key to continue" prompts.
3. once a return key is executed it ask for username, password, enable pass.
4. and then configures.

Well, the code below works fine as per the above 4 statements. My question is the file also contains a few devices which will not prompt for "press any key to continue". They only ask for Username and password. Can any one please assist to modify the code so that it check for the prompt "press any key to continue" and return key is executed and if it does not prompt it goes for username and rest of the code...

thanks in advance

Code:
use Net::Telnet;
my $filepath="C:\\file.txt";
my @output;

my $username="user";
my $passwd="passwd";
my $enapass="ena";

open (FILE, "<$filepath");
    @records=(<FILE>);
    foreach $host(@records)
    {
    chomp ($host);
    print "$host\n";
    TELNET();
    } 
close (FILE);

 sub TELNET {
        my ($host) = @_;
          my $t;  
        if (eval {$t = Net::Telnet->new(Host => $host)})  
        {
          print STDERR "Configuring $host\n";
          $t->open($host);
          $t->waitfor('/Press any key to continue/');
          $t->print('');
          $t->waitfor('/Username:\s*/');
          $t->print($username);
          $t->waitfor('/Password:\s*/');
          $t->print($passwd);
          $t->print('enable');
          $t->waitfor('/Password:\s*/');
          $t->print($enapass);
          $t->print('config t');
          push(@output, my @lines=$t->cmd('logging 10.10.10.10'));
          print "@output";
          $t->close;
} else 
{  print "failed"; 
}




Last edited by sureshcisco; 11-30-2010 at 09:29 PM..
# 2  
Old 11-30-2010
Why not rely on the timeout feature and only send <CR> if waitfor succeeds (ie dosn't timeout):

Code:
 $t->print('') if $t->waitfor('/Press any key to continue/') == 1;

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

SSH and telnet long delay to recieve prompt.

Hi guys. You'd have to excuse me a bit, as I'm a noob. I really try to avoid asking questions and do research for whatever linux issues that may arise. I am experiencing a long wait for the shell to come up when I ssh or telnet into a Sunos 5.10 environment. It takes 70 seconds to give me... (12 Replies)
Discussion started by: gpenco
12 Replies

2. UNIX for Dummies Questions & Answers

How to disconnect telnet prompt with port no in script.

Hi Gurus, I am trying to write a script for checking the status of linux servers by connecting via telnet with port no but to terminate i have to manually type "quit" .how can i terminate the telnet session in script itself.For E.g ========================================= telnet ipaddress... (3 Replies)
Discussion started by: kapil514
3 Replies

3. UNIX for Dummies Questions & Answers

Auto-exit from telnet prompt

Hi, Hope all your are doing well. Need a suggestion from you. I am writing an automated shell script that will effectively check the telnet connectivity with different backends from present deployment server. Ideally, this script reads each backend hostname from a configuration file and fires... (4 Replies)
Discussion started by: bhaskar_m
4 Replies

4. Shell Programming and Scripting

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);... (5 Replies)
Discussion started by: ahmed_zaher
5 Replies

5. Shell Programming and Scripting

how do i access db2 instance via telnet command prompt

Hi I have a perl script code in which connecting with db2 database and doing some process. My perl script code and db2 database server present in the same unix server. I am connecting and executing perl script code via windows telnet. Now my question is i could not able to connect db2 server... (4 Replies)
Discussion started by: solo123
4 Replies

6. Shell Programming and Scripting

Telnet in command prompt

Hi, i have typed telnet yahoo.com 80 in command prompt it displays as a blank command prompt page titling as Telnet Yahoo.com Other than that i am not able to get anything. can anyone sort me out the reason for this (12 Replies)
Discussion started by: satheeshkr_cse
12 Replies

7. Shell Programming and Scripting

Pressing "Enter/Space bar" using Net::TELNET? in Perl

I'm trying to learn how to get my script to execute the enter button when it telnets into a router and the router displays output but you need to press the space bar or enter button to continue displaying my output of the router. How is this done? (0 Replies)
Discussion started by: xmaverick
0 Replies

8. Shell Programming and Scripting

Using Net::Telnet in Perl to connect to an adsl modem

Hello, First of all, congratulations on this forum! Very mice material! This is my fist thread and it has to do with connecting to an adsl modem and executing some commands. Heres what I do: $username = 'admin'; $passwd = 'admin'; $telnet = new Net::Telnet ( Timeout=>10,... (2 Replies)
Discussion started by: Ravendark
2 Replies

9. Shell Programming and Scripting

problem with Net::Telnet

#!/usr/bin/perl use strict; use warnings; use Net::Telnet (); my $t = new Net::Telnet (Timeout => 10, Prompt => '/.*(#|>|\))\s*$/'); my $remote_host='home'; $t->open(Host => $remote_host, Port =>23); $t->login('root', 'pass'); the error that I get... (4 Replies)
Discussion started by: lassimanji
4 Replies

10. Windows & DOS: Issues & Discussions

Need Help on "waitfor" command in net::Telnet Module in PERL

Hi, Can anybody help me in writing command "waitfor" for string "C:\WINNT\Profiles\mfcf0508>" while using net::Telnet module. I tried the below format : $telnet->waitfor('/"C\:\WINNT\Profiles\mfcf0508>".*$/i'); Getting error as : pattern match timed-out Plz help me (3 Replies)
Discussion started by: sudhakaryadav
3 Replies
Login or Register to Ask a Question