Sponsored Content
Full Discussion: problem with Net::Telnet
Top Forums Shell Programming and Scripting problem with Net::Telnet Post 302314586 by lassimanji on Friday 8th of May 2009 07:14:13 PM
Old 05-08-2009
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 when I run this file from the command prompt is

problem connecting to "home", port 23 :unknow error at line 9.
I cant seem to figure out whats wrong with it
 

10 More Discussions You Might Find Interesting

1. Solaris

boot net problem

Hi my friends when I want to make boot net for my server I have receive: Time out ARP/RARP note: I have use Ok boot net (this is my Syntax) so what is the problem? (2 Replies)
Discussion started by: bintaleb
2 Replies

2. 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

3. Shell Programming and Scripting

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

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 (1 Reply)
Discussion started by: sudhakaryadav
1 Replies

4. 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

5. Shell Programming and Scripting

Need help on use of "cmd" command in net::Telnet module in PERL

in "cmd" command i want to copy the ouput of the command excuted to a particular file in a directory. How to do this..?? Ex : $telnet->cmd(String => 'allip:acl=a1;',Prompt => '/</'); i want to copy o/p of the command "allip:acl=a1;" in a log file in a particular directory. Plz suggest.. (1 Reply)
Discussion started by: sudhakaryadav
1 Replies

6. 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

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

NET::FTP Problem

I am facing some problem in the NET::FTP Library in perl script in the HPUX. I am using NET::FTP to ftp files from 23 remote servers. Except 1 server everything is working fine. I am getting this error from one server . but normal FTP is working fine for that server. everytime few files are ftped... (1 Reply)
Discussion started by: mohanm
1 Replies

9. Shell Programming and Scripting

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,... (1 Reply)
Discussion started by: sureshcisco
1 Replies

10. 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
Cisco(3pm)						User Contributed Perl Documentation						Cisco(3pm)

NAME
Net::Telnet::Cisco - interact with a Cisco router SYNOPSIS
use Net::Telnet::Cisco; my $session = Net::Telnet::Cisco->new(Host => '123.123.123.123'); $session->login('login', 'password'); # Execute a command my @output = $session->cmd('show version'); print @output; # Enable mode if ($session->enable("enable_password") ) { @output = $session->cmd('show privilege'); print "My privileges: @output "; } else { warn "Can't enable: " . $session->errmsg; } $session->close; DESCRIPTION
Net::Telnet::Cisco provides additional functionality to Net::Telnet for dealing with Cisco routers. cmd() parses router-generated error messages - the kind that begin with a '%' - and stows them in $obj->errmsg(), so that errmode can be used to perform automatic error-handling actions. CAVEATS
Before you use Net::Telnet::Cisco, you should have a good understanding of Net::Telnet, so read it's documentation first, and then come back here to see the improvements. Some things are easier to accomplish with UCD's C-based SNMP module, or the all-perl Net::SNMP. SNMP has three advantages: it's faster, handles errors better, and doesn't use any VTYs on the router. SNMP does have some limitations, so for anything you can't accomplish with SNMP, there's Net::Telnet::Cisco. METHODS
new - create new Net::Telnet::Cisco object $session = Net::Telnet::Cisco->new( [Autopage => $boolean,] # 1 [More_prompt => $matchop,] # '/(?m:^[s]*--More--)/', [Always_waitfor_prompt => $boolean,] # 1 [Waitfor_pause => $milliseconds,] # 0.1 [Normalize_cmd => $boolean,] # 1 [Send_wakeup => $when,] # 0 [Ignore_warnings => $boolean,] # 0 [Warnings => $matchop,] # see docs [Errors => $matchop,] # see docs # Net::Telnet arguments [Binmode => $mode,] [Cmd_remove_mode => $mode,] [Dump_Log => $filename,] [Errmode => $errmode,] [Fhopen => $filehandle,] [Host => $host,] [Input_log = => $file,] [Input_record_separator => $chars,] [Option_log => $file,] [Ors = => $chars,] [Output_log => $file,] [Output_record_separator => $chars,] [Port => $port,] [Prompt = => $matchop,] # see docs [Rs => $chars,] [Telnetmode => $mode,] [Timeout => $secs,]); ); Creates a new object. Read `perldoc perlboot` if you don't understand that. login - login to a router $ok = $obj->login($username, $password); $ok = $obj->login([Name => $username,] [Password => $password,] [Passcode => $passcode,] # for Secur-ID/XTACACS [Prompt => $match,] [Timeout => $secs,]); All arguments are optional as of v1.05. Some routers don't ask for a username, they start the login conversation with a password request. cmd - send a command $ok = $obj->cmd($string); $ok = $obj->cmd(String => $string, [Output => $ref,] [Cmd_remove_mode => $mode,] [Errmode => $mode,] [Input_record_separator => $chars,] [Ors => $chars,] [Output_record_separator => $chars,] [Prompt => $match,] [Rs => $chars,] [Timeout => $secs,]); @output = $obj->cmd($string); @output = $obj->cmd(String => $string, [Output => $ref,] [Cmd_remove_mode => $mode,] [Errmode => $mode,] [Input_record_separator => $chars,] [Ors => $chars,] [Output_record_separator => $chars,] [Prompt => $match,] [Rs => $chars,] [Timeout => $secs,]); Normalize_cmd has been added to the default Net::Telnet args. It lets you temporarily change whether backspace, delete, and kill char- acters are parsed in the command output. (This is performed by default) prompt - return control to the program whenever this string occurs in router output $matchop = $obj->prompt; $prev = $obj->prompt($matchop); The default cmd_prompt changed in v1.05. It's suitable for matching prompts like "router$ ", "router# ", "router> (enable) ", and "router(config-if)# " Let's take a closer look, shall we? (?m: # Net::Telnet doesn't accept quoted regexen (i.e. qr//) # so we need to use an embedded pattern-match modifier # to treat the input as a multiline buffer. ^ # beginning of line ? # optional linefeed [w.-]+ # router hostname s? # optional space (?: # Strings like "(config)" and "(config-if)", "(config-line)", # and "(config-router)" indicate that we're in privileged (config[^)]*) # EXEC mode (i.e. we're enabled). )? # The middle backslash is only there to appear my syntax # highlighter. s? # more optional space [$#>] # Prompts typically end with "$", "#", or ">". Backslash # for syntax-highlighter. s? # more space padding (?: # Catalyst switches print "(enable)" when in privileged (enable) # EXEC mode. )? s* # spaces before the end-of-line aren't important to us. $ # end of line ) # end of (?m: The default prompt published in 1.03 was "/^s*[w().-]*[$#>]s?(?:(enable))?s*$/". As you can see, the prompt was drastically overhauled in 1.05. If your code suddenly starts timing out after upgrading Net::Telnet::Cisco, this is the first thing to investigate. enable - enter enabled mode $ok = $obj->enable; $ok = $obj->enable($password); $ok = $obj->enable( [Name => $name,] [Password => $password,] [Passcode => $passcode,] [Level => $level,] ); This method changes privilege level to enabled mode, (i.e. root) If a single argument is provided by the caller, it will be used as a password. For more control, including the ability to set the priv- ilege-level, you must use the named-argument scheme. enable() returns 1 on success and undef on failure. is_enabled - Am I root? $bool = $obj->is_enabled; A trivial check to see whether we have a root-style prompt, with either the word "(enable)" in it, or a trailing "#". Warning: this method will return false positives if your prompt has "#"s in it. You may be better off calling "$obj->cmd("show privi- lege")" instead. disable - leave enabled mode $ok = $obj->disable; This method exits the router's privileged mode. ios_break - send a break (control-^) $ok = $obj->ios_break( [ additional strings to print, ... ] ); You may have to use errmode(), fork, or threads to break at the an appropriate time. last_prompt - displays the last prompt matched by prompt() $match = $obj->last_prompt; last_prompt() will return '' if the program has not yet matched a prompt. always_waitfor_prompt - waitfor and cmd prompt behaviour $boolean = $obj->always_waitfor_prompt; $boolean = $obj->always_waitfor_prompt($boolean); Default value: 1 If you pass a Prompt argument to cmd() or waitfor() a String or Match, they will return control on a successful match of your argu- ment(s) or the default prompt. Set always_waitfor_prompt to 0 to return control only for your arguments. This method has no effect on login(). login() will always wait for a prompt. waitfor_pause - insert a small delay before waitfor() $boolean = $obj->waitfor_pause; $boolean = $obj->waitfor_pause($milliseconds); Default value: 0.1 In rare circumstances, the last_prompt is set incorrectly. By adding a very small delay before calling the parent class's waitfor(), this bug is eliminated. If you ever find reason to modify this from it's default setting, please let me know. autopage - Turn autopaging on and off $boolean = $obj->autopage; $boolean = $obj->autopage($boolean); Default value: 1 IOS pages output by default. It expects human eyes to be reading the output, not programs. Humans hit the spacebar to scroll page by page so autopage() mimicks that behaviour. This is the slow way to handle paging. See the Paging EXAMPLE for a faster way. normalize_cmd - Turn normalization on and off $boolean = $obj->normalize_cmd; $boolean = $obj->normalize_cmd($boolean); Default value: 1 IOS clears '--More--' prompts with backspaces (e.g. ^H). If you're excited by the thought of having raw control characters like ^H (backspace), ^? (delete), and ^U (kill) in your command output, turn this feature off. Logging is unaffected by this setting. more_prompt - Matchop used by autopage() $matchop = $obj->prompt; $prev = $obj->prompt($matchop); Default value: '/(?m:^(?:[s]*--More--)/', Please email me if you find others. send_wakeup - send a newline to the router at login time $when = $obj->send_wakeup; $when = $obj->send_wakeup( 'connect' ); $when = $obj->send_wakeup( 'timeout' ); $when = $obj->send_wakeup( 0 ); Default value: 0 Some routers quietly allow you to connect but don't display the expected login prompts. Sends a newline in the hopes that this spurs the routers to print something. 'connect' sends a newline immediately upon connection. 'timeout' sends a newline if the connection timeouts. 0 turns this feature off. I understand this works with Livingston Portmasters. ignore_warnings - Don't call error() for warnings $boolean = $obj->ignore_warnings; $boolean = $obj->ignore_warnings($boolean); Default value: 0 Not all strings that begin with a '%' are really errors. Some are just warnings. By setting this, you are ignoring them. This will show up in the logs, but that's it. warnings - Matchop used by ignore_warnings(). $boolean = $obj->warnings; $boolean = $obj->warnings($matchop); Default value: '/(?mx:^%s?Unknown VPN |^%s?IP routing table VRF.* does not exist. Create first$ |^%s?No CEF interface information |^%s?No matching route to delete$ |^%s?Not all config may be removed and may reappear after reactivating |^%s?Warning: )/', Not all strings that begin with a '%' are really errors. Some are just warnings. Cisco calls these the CIPMIOSWarningExpressions. errors - Matchop used to catch special-cased errors. $boolean = $obj->errors; $boolean = $obj->errors($matchop); Default value: '/(?mx:^Unknown command "[^"]*" Use 'help' for more info.)/', Some errors don't begin with a '%'. Trap them here. EXAMPLES
Paging v1.08 added internal autopaging support to cmd(). Whenever a '--Page--' prompt appears on the screen, we send a space right back. It works, but it's slow. You'd be better off sending one of the following commands just after login(): # To a router $session->cmd('terminal length 0'); # To a switch $session->cmd('set length 0'); Logging Want to see the session transcript? Just call input_log(). e.g. my $session = Net::Telnet::Cisco->new(Host => $router, Input_log => "input.log", ); See input_log() in Net::Telnet for info. Input logs are easy-to-read translated transcripts with all of the control characters and telnet escapes cleaned up. If you want to view the raw session, see dump_log() in Net::Telnet. If you're getting tricky and using print() in addition to cmd(), you may also want to use output_log(). Big output Trying to dump the entire BGP table? (e.g. "show ip bgp") The default buffer size is 1MB, so you'll have to increase it. my $MB = 1024 * 1024; $session->max_buffer_length(5 * $MB); Sending multiple lines at once Some commands like "extended ping" and "copy" prompt for several lines of data. It's not necessary to change the prompt for each line. Instead, send everything at once, separated by newlines. For: router# ping Protocol [ip]: Target IP address: 10.0.0.1 Repeat count [5]: 10 Datagram size [100]: 1500 Timeout in seconds [2]: Extended commands [n]: Sweep range of sizes [n]: Try this: my $protocol = ''; # default value my $ip = '10.0.0.1'; my $repeat = 10; my $datagram = 1500; my $timeout = ''; # default value my $extended = ''; # default value my $sweep = ''; # default value $session->cmd( "ping $protocol $ip $repeat $datagram $timeout $extended $sweep "); If you prefer, you can put the cmd on a single line and replace every static newline with the " " character. e.g. $session->cmd("ping $protocol $ip $repeat $datagram " . "$timeout $extended $sweep "); Backup via TFTP Backs up the running-confg to a TFTP server. Backup file is in the form "router-confg". Make sure that file exists on the TFTP server or the transfer will fail! my $backup_host = "tftpserver.somewhere.net"; my $device = "cisco.somewhere.net"; my $type = "router"; # or "switch"; my $ios_version = 12; my @out; if ($type eq "router") { if ($ios_version >= 12) { @out = $session->cmd("copy system:/running-config " . "tftp://$backup_host/$device-confg "); } elsif ($ios_version >= 11) { @out = $session->cmd("copy running-config tftp $backup_host " . "$device-confg "); } elsif ($ios_version >= 10) { @out = $session->cmd("write net $backup_host $device-confg "); } } elsif ($type eq "switch") { @out = $session->cmd("copy system:/running-config " . "tftp://$backup_host/$device-confg "); } Sending control characters Use print() if you expect to get a prompt back. Use cmd() if you don't. $session->print("c^"); # send control-^ $session->cmd("cZ"); # send control-Z See also: "ios_break()" SUPPORT
http://NetTelnetCisco.sourceforge.net/ Mailing lists nettelnetcisco-announce is for important security bulletins and upgrades. Very low traffic, no spam, HIGHLY RECOMMENDED! http://lists.sourceforge.net/lists/listinfo/nettelnetcisco-announce nettelnetcisco-users is for usage discussion, help, tips, tricks, etc. http://lists.sourceforge.net/lists/listinfo/nettelnetcisco-users nettelnetcisco-devel is for uber-hackers; you know who you are. http://lists.sourceforge.net/lists/listinfo/nettelnetcisco-devel Help/discussion forums http://sourceforge.net/forum/?group_id=48856 Bug tracker http://sourceforge.net/tracker/?group_id=48856 SEE ALSO
Net::Telnet Net::SNMP UCD NetSNMP - http://www.netsnmp.org/ RAT/NCAT - http://ncat.sourceforge.net/ AUTHOR
Joshua_Keroes@eli.net $Date: 2002/12/31 00:12:32 $ It would greatly amuse the author if you would send email to him and tell him how you are using Net::Telnet::Cisco. As of Mar 2002, over 200 people have emailed me or posted to the Net::Telnet::Cisco site. N::T::C is used to help manage over 14,000 machines! Keep the email rolling in! THANKS
The following people understand what Open Source Software is all about. Thanks Brian Landers, Aaron Racine, Niels van Dijke, Tony Mueller, Frank Eickholt, Al Sorrell, Jebi Punnoose, Christian Alfsen, Niels van Dijke, Kevin der Kinderen, Ian Batterbee, Leonardo Cont, Steve Meier, Andre Bonhote, Rob Patrick, FtR, James "mcaizjb3" Brown, and Hiro "Paul" Protagonist. Paul gets a ++ for code-ninja skills. Institutions: infobot.org #perl, perlmonks.org, sourceforge.net, the geeks at geekhouse.org, and eli.net. Send in a patch and we can make the world a better place. COPYRIGHT AND LICENSE
Copyright (c) 2000-2002 Joshua Keroes, Electric Lightwave Inc. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. perl v5.8.8 2005-08-26 Cisco(3pm)
All times are GMT -4. The time now is 02:06 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy