Net::T...:Cisco(3pm)
π NAME π
Net::Telnet::Cisco β bestie, it's literally how you talk to a Cisco router π
Ok so quick vibe check: the manual name up top is Net::T...:Cisco(3pm), from User Contributed Perl Documentation. That's the whole "who dis" of this page. Now let's get into it β¨
β¨ SYNOPSIS β¨
Here's the whole glow-up in one screenshot, no cap:
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\n";
} else {
warn "Can't enable: " . $session->errmsg;
}
$session->close;
Look at her. Creating a session, logging in, running a show version, popping into enable mode, checking her privileges, closing up shop. Main character energy from start to finish π
π¦ DESCRIPTION π¦
So Net::Telnet::Cisco is giving "Net::Telnet, but with a Cisco-router-sized upgrade" π It hands you additional functionality on top of Net::Telnet, specifically for dealing with Cisco routers.
And the icon behavior? cmd() β she reads the router's error messages, the ones that start with a % β and she stashes them into $obj->errmsg(). Which means errmode can do its automatic error-handling thing. So instead of you manually catching every little router sass, it's handled. Slay π
β οΈ CAVEATS β οΈ
Bestie, listen. Before you use Net::Telnet::Cisco, you should have a good understanding of Net::Telnet. Read its documentation first, and then come back here to see the improvements. Fr fr, do the reading. π
Some things are genuinely easier with UCD's C-based SNMP module, or the all-perl Net::SNMP. And SNMP is lowkey flexing three advantages:
- it's faster π
- it handles errors better πͺ
- it doesn't use any VTYs on the router π
But SNMP has some limitations too, so for anything you can't pull off with SNMP, there's Net::Telnet::Cisco. That's her moment. That's her era. Periodt. πΈ
π METHODS π
new β make a brand-new Net::Telnet::Cisco object β¨
$session = Net::Telnet::Cisco->new(
[Always_waitfor_prompt => $boolean,] # 1
[Autopage => $boolean,] # 1
[Ignore_warnings => $boolean,] # 0
[More_prompt => $matchop,] # '/(?m:^\s*--More--)/',
[Normalize_cmd => $boolean,] # 1
[Send_wakeup => $when,] # 0
[Waitfor_pause => $milliseconds,] # 0.1
[Warnings => $matchop,] # see docs
# Net::Telnet arguments
);
okay so this literally just creates a new object, bestie. that's the whole job. and if you read that and went "wait, an object??" β no stress, go read perldoc perlboot. that's the vibe check you need. π
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,]);
every single one of these arguments is optional as of v1.05, which is genuinely so nice of it. some routers are quirky and don't even ask for a username β they just open the conversation by demanding a password straight up. iconic behaviour honestly. π
cmd β send a command π¨
$ok = $obj->cmd($string);
$ok = $obj->cmd(String => $string,
[Output => $ref,]
[Prompt => $match,]
[Timeout => $secs,]
[Cmd_remove_mode => $mode,]);
@output = $obj->cmd($string);
@output = $obj->cmd(String => $string,
[Output => $ref,]
[Prompt => $match,]
[Timeout => $secs,]
[Cmd_remove_mode => $mode,]
[Normalize_cmd => $boolean,]);
so Normalize_cmd got added to the default Net::Telnet args, and what it does is let you temporarily flip whether backspace, delete, and kill characters get parsed in the command output. (and yes, that parsing is happening by default, just so you know.) β¨
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. this new one is built to match prompts like "router$ ", "router# ", "router> (enable) ", and "router(config-if)# ".
okay so let's actually take a closer look at this, because it's giving main character regex:
(?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
[\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 that got published back in 1.03 was "/^\s*[\w().-]*[\$#>]\s?(?:\(enable\))?\s*$/". yeah. as you can literally see, the prompt got drastically overhauled in 1.05. so if your code suddenly starts timing out after you upgrade Net::Telnet::Cisco β this is the very first thing you should go investigate. do not pass go. π¨
enable β enter enabled mode π
$ok = $obj->enable;
$ok = $obj->enable($password);
$ok = $obj->enable([Name => $name,] [Password => $password,]
[Passcode => $passcode,] [Level => $level,]);
this method bumps your privilege level up to enabled mode, i.e. root. so basically the glow-up. π
if the caller hands it a single argument, that argument gets used as the password. but if you want more control β including the ability to set the privilege-level β you have to go with the named-argument scheme.
enable() returns 1 on success and undef on failure.
is_enabled β Am I root? π΅οΈ
$bool = $obj->is_enabled;
a lil trivial check to see whether we've got a root-style prompt β either with the word "(enable)" in it, or with 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 privilege")"instead.
disable β leave enabled mode πͺ
$ok = $obj->disable;
this method exits the router's privileged mode. we're logging out of the VIP section, babes. π
fhopen β use already open filehandle for I/O π
$ok = $obj->fhopen($fh);
this method associates the open filehandle $fh with $obj for further I/O. and listen β filehandle $fh must already be opened. no opening it on the way in, that's not the deal.
the value 1 is returned success, and the error mode action is performed on failure.
ios_break β send a break (control-^) π₯
$ok = $obj->ios_break;
you may have to use errmode(), fork, or threads to break at the an appropriate time. (yes, that's exactly how the original says it, bestie, we don't fix it, we honour it. π)
last_cmd β displays the last command π
$match = $obj->last_cmd;
last_cmd() will return '' if the program does not yet have a last command.
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 a Match β they'll return control on a successful match of your argument(s) or the default prompt. set always_waitfor_prompt to 0 and they'll only return control for your arguments. your rules, your vibe. β¨
this method has no effect on login(). login() will always wait for a prompt. no negotiating.
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. but by adding a very very small delay before calling the parent class's waitfor(), that bug is eliminated. so if you ever find a reason to modify this from its 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's expecting human eyes to be reading the output, not programs β rude of it honestly. humans hit the spacebar to scroll page by page, and autopage() mimics exactly that behaviour. heads up though: 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). so if you're genuinely excited by the thought of having raw control characters like ^H (backspace), ^? (delete), and ^U (kill) sitting in your command output β turn this feature off. live your truth. π
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 then just... don't display the expected login prompts. awkward. so this sends a newline in the hopes that it spurs the routers to print something. like a lil poke. ππ
'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. that's all it does. π
warnings β Matchop used by ignore_warnings(). β οΈ
$boolean = $obj->warnings;
$boolean = $obj->warnings($matchop);
Default value:
/(?mx:^% Unknown VPN
|^%IP routing table VRF.* does not exist. Create first$
|^%No CEF interface information
|^%No matching route to delete$
|^%Not all config may be removed and may reappear after reactivating
)/
not all strings that begin with a '%' are really errors. some are just warnings. Cisco calls these the CIPMIOSWarningExpressions.
π EXAMPLES π
π Paging
v1.08 slid in with internal autopaging support for cmd(). The moment a '--Page--' prompt pops up on screen, we just yeet a space right back at it. It works! But bestie⦠it's slow. You're so much better off sending one of these right after login():
# To a router
$session->cmd('terminal length 0');
# To a switch
$session->cmd('set length 0');
π Logging
Wanna see the whole session transcript? Just call input_log(). Iconic behaviour, honestly.
e.g.
my $session = Net::Telnet::Cisco->new(Host => $router,
Input_log => "input.log",
);
Peep input_log() in Net::Telnet for the deets.
Input logs are easy-to-read translated transcripts where all the control characters and telnet escapes have been cleaned up. Immaculate. If you want to view the raw session instead, go see dump_log() in Net::Telnet. And if you're getting all tricky and using print() alongside cmd(), you may also wanna use output_log().
ποΈ Big output
Trying to dump the entire BGP table? (like, "show ip bgp") The default buffer size is 1MB, so babes you will 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. You do not need to change the prompt for each line, okay? 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 "\n" character.
e.g.
$session->cmd("ping\n$protocol\n$ip\n$repeat\n$datagram\n"
. "$timeout\n$extended\n$sweep\n");
πΎ Backup via TFTP
This backs up the running-confg to a TFTP server. The backup file is in the form "router-confg". Make sure that file exists on the TFTP server or the transfer will FAIL! Not a drill, bestie. π
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\n\n\n");
} elsif ($ios_version >= 11) {
@out = $session->cmd("copy running-config tftp\n$backup_host\n"
. "$device-confg\n");
} elsif ($ios_version >= 10) {
@out = $session->cmd("write net\n$backup_host\n$device-confg\n\n");
}
} elsif ($type eq "switch") {
@out = $session->cmd("copy system:/running-config "
. "tftp://$backup_host/$device-confg\n\n\n");
}
π«Ά 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::TelnetNet::SNMP- UCD NetSNMP - http://www.netsnmp.org/
- RAT/NCAT - http://ncat.sourceforge.net/
β¨ AUTHOR β¨
Joshua_Keroes@eli.net $Date: 2002/06/18 17:17:03 $
It would greatly amuse the author if you would send email to him and tell him how you are using Net::Telnet::Cisco. Like, he genuinely wants to know. π₯Ή
As of Mar 2002, 170 people have emailed me. 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, and Andre Bonhote. π«Ά
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.
And that's a wrap on the whole page, bestie! π This lil' slice of manpage goodness came from perl v5.40.1, dated 2025-08-15, and it lives at Net::T...:Cisco(3pm) under User Contributed Perl Documentation. Go forth and telnet responsibly. πβ¨
π¦ where this page comes from
- ubuntu resolute: libnet-telnet-cisco-perl 1.12-3 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- ubuntu stonking: libnet-telnet-cisco-perl 1.12-3 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms