Perlfect Solutions

Using perl to connect to remote hosts via telnet

Did you think it was easy?

Suppose you needed to open a connection to a remote host from within your perl program. One thing you would probably think of doing at first is the following:

open TELNET "|telnet $hostname"; print TELNET "$username\n"; print TELNET "$password\n"; ...

Unfortunately, if you try this, you'll find it doesn't work. The telnet program connects to the remote host but it completely ignores any commands you pipe to it. That's because the telnet program reads its input only from the terminal and not from standard input.

The most cunning among you might then think a workaround for this: Open a socket to port 23 (the default telnet port) of the remote machine and write directly to it, thus, bypassing the telnet program altogether. Well, although that might sound like a neat idea, it's not. And that's because the telnet protocol requires that certain control data be exchanged between the two machines by sending them along through the same socket connection.

So, it turns out that the problem is much more complicated than it seemed at first. We don't just need to write code to perform socket I/O, but also we need to write code that speaks the TELNET protocol. This is the bad news. The good news is that this code has already been written, and that its author was kind enough to bundle it in a useful module, Net::Telnet, available at CPAN.

Net::Telnet

Using Net::Telnet is pretty straightforward and simple. Let's first see a no-thrills example:

use Net::Telnet; $telnet = new Net::Telnet ( Timeout=>10, Errmode=>'die'); $telnet->open('camel.perlfect.com'); $telnet->waitfor('/login: $/i'); $telnet->print('bilbo'); $telnet->waitfor('/password: $/i'); $telnet->print('baggins'); $telnet->waitfor('/\$ $/i'); $telnet->print('who'); $output = $telnet->waitfor('/\$ $/i'); print $output;

This simple program connects to camel.perlfect.com with username and password, 'bilbo' and 'baggins' respectively, and the issues the command 'who' to get a list of logged in users. It then retrieves and prints the output. Although the code is self explanatory, here are a few things worth of noting:

  • The Errmode option in the constructor for the telnet object specifies what kind of behaviour we want the object to have when it encounters an error. die means that the program will die with an error message. The other option is return. This will cause the method that caused the error to return a false value. The error message can then be retrieved from Net::Telnet->errmsg.
  • The method waitfor() takes a regular expression as an argument and tries to match it in the stream that is transmitted from the remote host. Upon a successful match, the method returns all input before the match.

Shortcuts

You might notice that issuing commands involves repeating print() and waitfor() calls in a very much similar manner. We print a command and then we try to match a shell prompt. Net::Telnet provides a very nice mode of operation that rids you of some of the repetitive tt. All you need to do is to specify the regular expression that matches a prompt as a parameter in the object's constructor and then use the cmd() method to issue commands. Similarly to command issuing, Net::Telnet provides a handy method to simplify the login process, namely login(). The following example demonstrates these shortcuts.

use Net::Telnet; $telnet = new Net::Telnet ( Timeout=>10, Errmode=>'die' Prompt => '/\$ $/i'); $telnet->open('camel.perlfect.com'); $telnet->login('bilbo', 'baggins'); print $telnet->cmd('who');

This does the same as the previous example, but with much less typing. Note that the login() method matches the login and password prompts with the regular expressions /(login|username)[: ]*$/i and /password[: ]*$/i respectively.

So far we have covered the basics of the Net::Telnet module, enough to get you going with your first telnetting scripts. The module is rich with other features, so make sure you take the time to have a look at the documentation.

Happy TELNETing!

Online Documentation/Tutorials

  • The Net::Telnet documentation that comes with its distribution (Net::Telnet comes as part of the libnet bundle at CPAN) covers just about anything you would need to know about

Comments

Your name:
Your comments:

Security check *

 

amir   

Posted at 6:22am on Thursday, March 8th, 2007

too good

Xose   

Posted at 3:39am on Friday, March 23rd, 2007

good

Anonymous   

Posted at 4:54am on Thursday, March 29th, 2007

easily understandable + short = Very GOOD introduction

don   

Posted at 3:22pm on Wednesday, April 4th, 2007

good and easy to start a telnet in perl

Chris   

Posted at 3:33am on Friday, April 6th, 2007

Hello...

i have aquestion related to this Script. You connect to a host. Do you disconnect also? No, or?
thx

Ninomninom   

Posted at 4:07pm on Tuesday, April 17th, 2007

thanks for this document i was looking for an explantion for waitfor and i saw this site and it is explaining ver well. Thanks

Gimi   

Posted at 1:53pm on Wednesday, April 18th, 2007

Runing yous scrips i have this error
pattern match timed-out at

Ajay   

Posted at 11:57pm on Monday, April 23rd, 2007

can i connect to remote windows 2000 machine using NET::TELNET, as i'm not able to connect , actaully it is returing Junk, But same script is running perfectly fine on Windows XP machine ???????????//

aliaz   

Posted at 12:08pm on Friday, April 27th, 2007

I can't connect to windows 2000 server also
there 3 prompte
USERCODE:
PASSWORD
DOMAIN:
my code send me time out saying that the pattern /domain[: ]*/ can't be match

sirc   

Posted at 6:18pm on Sunday, May 27th, 2007

how can i handle the connection error.. if the machine im connecting to is down? how can i catch the error?

Tom   

Posted at 1:32pm on Tuesday, July 10th, 2007

Hi.
I have tried this code but I have recieved an error when I'm executing the script: "pattern match timed-out at line 38".

Line 38 is $telnet->waitfor('/$ $/i');
Someone can helping me solving this ?

djs   

Posted at 4:33pm on Monday, July 16th, 2007

you are not matching a prompt. it looks like you forgot to escape out your $, try this:

$telnet->waitfor('/$ $/i');

ob1   

Posted at 2:01pm on Wednesday, July 18th, 2007

do we need to send an initial carriage return if connecting to a terminal server? it seems to need this before a login prompt is displayed.

ob1   

Posted at 3:53pm on Wednesday, July 18th, 2007

never mind my previous post. print and waitfor combo worked great. thanks for a clear and concise article.

trying to run shell scripts   

Posted at 8:24am on Thursday, July 19th, 2007

Hi I was having problem running shell scripts
So the only way I got it to work was by estimating the time it takes to run so if it ran for 10 sec
I put a sleep 12 and then it worked. I sthere a way to do this without putting sleep commands, I thought the wait for would handle this.

nicky   

Posted at 11:25pm on Tuesday, August 7th, 2007

hi cool artical
can i ask some question?
what does $telnet->waitfor('/$ $/i'); do?
i dont really understand wat '/$ $/i' means u see

also isnt possible to write the result of the
$telnet->print('who');
into a notepad in my c drive? if so how would the code be like?

hb   

Posted at 7:44am on Wednesday, August 8th, 2007

Thank you for help! perldoc Net::Telnet was not so clear for me before.


In my script I have writen :



(translated to your example )

@output = $telnet->waitfor('/$ $/i'); print @output;

compare with :
$output = $telnet->waitfor('/$ $/i'); print $output;

RR   

Posted at 2:48pm on Wednesday, August 8th, 2007

I am having problem in connecting to the server
'timed-out waiting for command prompt at C:perl temptelnet.pl line 10'

although i am giving the same username and paasword that i am using manually.

Can someone help me out here

manoj   

Posted at 5:59am on Thursday, August 9th, 2007

I am facing problem.
i am executing command "cat test.txt" on remote machine through this module.this file contain number of lines & each line having numbers of fields, each of them seperated by tab.

when i get the output of that command & i try to split each line using tab as seperator.it does not work (split does not find tab in the line).

can some one haelo me. its urgent

Aparna   

Posted at 12:06am on Friday, August 17th, 2007

really good and made telnet easy thru perl

thaha   

Posted at 4:57am on Wednesday, August 29th, 2007

thanks a lot to provide such a nice intro to the telnet thru perl.

downloadsoftware.com@gmail.com   

Posted at 3:05am on Friday, September 7th, 2007

port 23 connection failed

bdizzel   

Posted at 1:13pm on Tuesday, September 18th, 2007

i was curious on what /$ $/i means any ideas?

Mark   

Posted at 11:45pm on Tuesday, September 18th, 2007

bdizzel ....

You mean waitfor('/$ $/i');?

I guess that this is waiting for the prompt "$ " (dollar, space). Note that the first dollar is escaped so it is interpreted as a character. The last dollar indicates end of line.

bdizzel   

Posted at 11:03am on Thursday, September 20th, 2007

thank you mark, i also have another question, when i used the command print $telnet->cmd('who'); , the very last line of the print is my prompt i recieve after i log in- bash2-:03$ ... i was wondering if there is a way to get that out of there, i was thinking about using chop or chomp but no success there... any help is appreciated thank you in advanced

McNally   

Posted at 9:40am on Monday, September 24th, 2007

For those who are confused reading the comments so far -- it appears to me that the commenting system is eating backslashes. Compare what's being displayed in the comments from bdizzel and others (i.e.: '/$ $/i') with what's in the code box in the article above -- you will find that in the latter there is an extra backslash character present before the first "$" to escape its special meaning as an end-of-line anchor in the pattern match. What the author is trying to match is the sequence (dollar sign)(space)(end-of-line). What the version without the backslash tries to match is (end-of-line)(space)(end-of-line), which is obviously much different.

Also, keep in mind that the prompt string that they're trying to match only makes sense if your prompt ends with a dollar sign ($) -- if you use a csh derivative, for example, and use the convention that your prompt ends in a percent sign (%) or if you like to feel importantly root-like and end your prompt with an octothorpe (aka (in US usage) pound sign, aka "#") -- well, then, you'll have to change your match expression.

Does that clear it up, bdizzel?

bdizzle   

Posted at 8:02am on Thursday, September 27th, 2007

excellent work McNally, i appreciate you taking the time to respond to my question, the prompt was confusing but this pretty much clears it up- thanks again

tez   

Posted at 3:43am on Friday, September 28th, 2007

Hi

I too am having problems with the sequence match. I am using TACACS on my Cisco equipment and have a multi line banner message that finishes with "Usercode:". I have tried all sorts of variations but I stil lfail to get it to work. For example.. waitfor('/[: ]*$/i'); I have tried a number of variations, can anyone help me at all?

sumant chhunchha   

Posted at 7:25am on Monday, October 8th, 2007

Hello,
I tried the things as above...I am trying to telnet with application port which is 2101..when i directly try from dos prompt as >telnet 2101.....it opens a telnet connection and I can fire command for that application however ere it is not showing any prompt to me..it shows blank scree and then i fire commands related to that. however using script it shows error "timed-out waiting for login prompt at telnet2.pl line 18"..... what can be the solution?
[code]
$telnet = new Net::Telnet ( Port=> 2101, Timeout=>10, Errmode=>'die');
$telnet->open('ps4131');
$telnet->login('administrator', 'password');
peint $telnet;
[/code]

Sumit   

Posted at 7:47pm on Friday, October 26th, 2007

hi my prompt is someting like
host:user:dir>
please help me setting the prompt =>'' pattern
I tried alot but nothing doing :(

Sumit   

Posted at 7:48pm on Friday, October 26th, 2007

hi my prompt is someting like
host:user:dir>
please help me setting the prompt =>'' pattern
I tried alot but nothing doing :(

Mikael   

Posted at 7:23pm on Friday, November 2nd, 2007

I think host:user:dir> would be as simple as:
$telnet->waitfor('/^host:user:dir>$/');

Salih   

Posted at 11:47am on Tuesday, November 20th, 2007

I have tried the code above several times . The steps of code are run succesfully untill it reaches line 8 where waitfor() is.

here is my command prompt during connection.

login:

Password:


switch8-12>


The perl script i coded.

1-use Net::Telnet;
2-$telnet = new Net::Telnet ( Timeout=>2, Errmode=>'die');
3-$telnet->open('192.168,0,12');
4-$telnet->waitfor('/login:/');
5-$telnet->print('salih');
6-$telnet->waitfor('/Password:/');
7-$telnet->print('S132A13');
8-$telnet->waitfor('/switch8-12>/');
9-$telnet->print('who');
10-output=$telnet->waitfor('/switch8-12>/');
11-print $output;


Wating for your comments.
Thanks in advance.
Cheers...
All for one, one for all.
(i think that is enough :)))) )

duke74s   

Posted at 4:42am on Monday, December 3rd, 2007

hi my prompt is like this
"[root@z1 ~]# "
- root : user, z1 : hostname, ~ : directory
I've never find the pattern. Please, help me!!

ptg   

Posted at 9:31am on Monday, December 3rd, 2007

I need to run the telnet script against devices with differnt login methods (individual useraccounts and direct password accounts).

Method1:

Username:xxx
Password:xxx
router#

Method2:
Password:xxx
router>enable
Password:xxx
router#

can we do a if and else using telnet->waitfor?

if ($telnet->waitfor('/Password: $/i')) {
$telnet->print("$ARGV[3]");
$telnet->waitfor('/>$/i');
$telnet->print('enable');
$telnet->waitfor('/Password: $/i');
$telnet->print("$ARGV[4]");
$telnet->waitfor('/#$/i');
$output = $telnet->print('');
}
else {
$telnet->waitfor('/Username: $/i');
$telnet->print("$ARGV[3]");
$telnet->waitfor('/Password: $/i');
$telnet->print("$ARGV[4]");
$telnet->waitfor('/#$/i');
$output = $telnet->print('');
}

Any help would be must appreciated!!! thanks!!!

RRP   

Posted at 5:10am on Thursday, January 3rd, 2008

HI Dear,
I found the following error message while trying to connect via telnet. I do not understand what is INC and what it does. I serched but did not find anything. Please help me on this. I am using windows XP. Is there any setting needs to be done for INC.
C:PerlsitelibURI>perl test2.pl
Can't locate Net/Telnet.pm in @INC (@INC contains: C:/Perl/lib C:/Perl/site/lib .) at test2.pl line 4.
BEGIN failed--compilation aborted at test2.pl line 4.

kwatkins   

Posted at 1:27pm on Thursday, January 3rd, 2008

Great, thanks

John Ryan   

Posted at 11:11am on Tuesday, January 8th, 2008

use Net::Telnet;
$telnet = new Net::Telnet ( Timeout=>10, Errmode=>'die');
$telnet->open('168.54.24.155');
$telnet->waitfor('/login: $/i');
$telnet->print('jryan');
$telnet->waitfor('/Password: $/i');
$telnet->print('5JKD223WER1');
$telnet->waitfor('/# $/i');
$telnet->print('uname -a');
$output = $telnet->waitfor('/# $/i');
print $output;
----------
doesn't matter what command I use (in the case above it is uname -a) it always return "1". what does it mean and how to fix it?

sujith   

Posted at 1:17am on Wednesday, January 9th, 2008

when i run this same program to connect to my server it return some error..
The error seems to be like this....

problem connecting to "192.168.1.100", port 23: Connection refused

Can any one help me out ..
cheers

Rami   

Posted at 1:08am on Tuesday, January 15th, 2008

Great ! :) now how to compile it ??

Thommy   

Posted at 6:52am on Tuesday, January 22nd, 2008

My Problem:

setting up a couple of devices via a Cisco Terminal Server.
I want to plug for example 5 Switches to this terminalserver 10.1.1.1 Port 2002 up to 2007

Then i want to set up the Vlan Interface and getting the configuration file from a tftp server.

But my main Problem is to execute commands on the terminal line because i have no login with a terminal server console.

My code:
my $session = Net::Telnet::Cisco::IOS->new(Host => '10.1.1.1', Port => 2002);

$session->cmd("nn"); # for getting a promt without login
@output = $session->cmd('sh run');
print @output;

but command ($session->cmd("nn");)timed-out at test.pl

I tried the same with a regular telnet session to a router and a login, this works
Can u help me?

Serge   

Posted at 6:50am on Wednesday, January 23rd, 2008

Thanks a lot!

shree   

Posted at 3:09am on Tuesday, January 29th, 2008

Actually i tried the below program:
use Net::Telnet;

$telnet = new Net::Telnet ( Timeout=>10,
Errmode=>'die');
$telnet->open('180.15.24.13');

# change this according to your telnet prompt
$telnet->waitfor('/login:/');
$telnet->print('root');

#change this according to your telnet password prompt
$telnet->waitfor('/Password:/');
$telnet->print('siemens');


# $telnet->waitfor('/sun4#/'); #change this to the prompt of the user you are testing
#print $telnet->cmd('./testing.sh');
#print $telnet->cmd('echo " hello " >> hello.csv');

$telnet->close();


but got an error in the command prompt saying:

Can't locate Net/Telnet.pm in @INC (@INC contains: d:/Perl/lib d:/Perl/site/lib .) at telnet.pl line 24.
BEGIN failed--compilation aborted at telnet.pl line 24.

Dave   

Posted at 11:08pm on Wednesday, January 30th, 2008

attn John Ryan.

I have the same problem, I had to do this:

$telnet->print('uname -a');
@output = $telnet->waitfor('/# $/i');
$output = "@output";
print $output;

It bugs me that it's note "right" but it works for now.

skipo   

Posted at 1:17pm on Thursday, February 14th, 2008

I have written a script to connect to a firewall and then telnet to a box on aht other side of that firewall (thaat's the way I have to connect). The purpose was to be able to telnet to a box without manually going thru teh intermediate steps. My code will run commands and return the results to me...until I tried to run vi. The $telnet->cmd('vi bob') just sits but never shows me the vi screen. (I assume because the comand has not completed.) is there a more interactive way to use the Net::Telnet on a character by character basis? to allow me to see the vi screen and interact with it?

Thanks,

redhatrak   

Posted at 12:16am on Wednesday, February 20th, 2008

I have tried this script and am getting an error when connecting to a box which actually one can ping to but cannot telnet. the telnet output of such a box is as below:
======
login: patqa1
patqa1's Password:
3004-010 Failed setting terminal ownership and mode.
======
For this error i get perl error as:
pattern match read eof at tt.pl line 24

How can this be handled? Help appreciated.

Thanks,

pepsi   

Posted at 11:43am on Wednesday, March 12th, 2008

Hi,

I am trying to telnet to a windows 2000 machine which has SLNet installed. I am using the following script

$telnet->open('addr1');
$telnet->login('Administrator', 'pass1');
$telnet->print('who');
@output = $telnet->waitfor('/>/');
$output = "@output";
print $output;

But the output is interspersed with junk characters. How do I ensure that these junk characters are not included in the result?

Ideally the result should be as shown below
********************************************
Administrator CONIN$ Mar 12 13:40

C:DOCUME~1ADMINI~1>
*********************************************

But when executed via script shown above, the output is displayed as shown below
*********************************************************
←[4;22H←[4;22Hwh←[4;23H←[4;24Ho←[5;1H←[5;1HAdministrator←[5;21HCONIN$←[5;34HMar
12 12:27←[7;1HC:DOCUME~1ADMINI~1 >
**********************************************************

Thanks

topepsi   

Posted at 4:04am on Wednesday, March 19th, 2008

i think you need to traverse the array @output per element. each element will contain per line.

might look something like this.
$output[0] = "********************************************"
$output[1] = "Administrator CONIN$ Mar 12 13:40";
... and so on

you might need a while or foreach to traverse the array

pepsi   

Posted at 8:51am on Wednesday, March 19th, 2008

topepsi, Thanks for your response. But it didn't work. Each array element still contains junk characters. Any other idea?

Shankar Kundargi   

Posted at 2:02am on Monday, March 24th, 2008

Hi,
I found the following error message while trying to connect via telnet. I am using windows XP. Is there any setting needs to be done for INC.
C:PerlsitelibURI>perl connect.pl
Can't locate Net/Telnet.pm in @INC (@INC contains: C:/Perl/lib C:/Perl/site/lib .) at connect.pl line 3.
BEGIN failed--compilation aborted at connect.pl line 3.
Please help me on this.

Tayyab   

Posted at 5:10am on Monday, March 24th, 2008

Hi Shankar,
May be you need to make a 'Net' repository in C:Perl/lib and then copy Telnet.pm there. I hope it helps.

Shankar Kundargi   

Posted at 7:09am on Monday, March 24th, 2008

Thanks Thayyab,
But the thing I already installed it and now I am getting Timed out error even if the login name and password are correct

Cisco_lad   

Posted at 11:25pm on Tuesday, April 8th, 2008

I am using below and it works fine, but only for one host.
how do I refernce host to a file of hosts. this might be trivial for u guys bit I am worse than a beginner.

Thanks in advance !

Cisco_lad

use Net::Telnet::Cisco;

my $session = Net::Telnet::Cisco->new(Host => '192.168.158.2');
$session->login('login', 'abc123');



# Enable mode
if ($session->enable("abc123") ) {


} else {
warn "Can't enable: " . $session->errmsg;
}

# Execute a command
my @output = $session->cmd('sh start | inc hostname');
print @output;
# Execute a command
@output = $session->cmd('sh ver | inc System image file is');
print @output;

$session->close;

Thai Tuan Anh   

Posted at 11:28pm on Wednesday, April 9th, 2008

I need to telnet to a sunser via 9023 port.
Normally, I can telnet directly as below:
telnet 10.2.4.9 9023
Trying 10.2.4.9...
Connected to 10.2.4.9.
Escape character is '^]'.
900 Login required.
login username passwd
login username passwd
102 2 records.
Lucent NavisRadius Radius Server, Version 4.5.8 (Build 551-060)
Copyright (c) 1998-2005 Lucent Technologies Inc. All Rights Reserved.
==>

I tried to use Net::Telnet with the code following:

#!/usr/bin/perl -w
use Net::Telnet ();
$t = new Net::Telnet (Timeout => 10,Port => 9023,Prompt => '/==> $/');
$t->open("10.2.4.9");
$t->waitfor("900 Login required. $/i");
$t->print("login username passwd");


But it's not successful. This is the error message:

missing opening delimiter of match operator in argument "900 Login required.i" given to Net::Telnet::waitfor() at ./try line 5

How can i fix it ?

PuneetKhurana   

Posted at 5:55am on Monday, April 21st, 2008

Really cool stuff

Peter   

Posted at 8:01am on Tuesday, May 13th, 2008

I that the right modul for me ?

I want to see what happens in my telnet window !
My goal is to open 4 telnet windows on diffent maschines to do a tcpdump ! With net::telnet I get a telnet not visible telnet session.

I that possible ? (input_log is not enough)

Peter

YaKuZa_B   

Posted at 4:00am on Thursday, May 15th, 2008

it'd error and not complete.

Rajesh   

Posted at 2:40am on Tuesday, May 27th, 2008

Hi,
I tried ur method I am getting below errors. What should I do for this.


Can't locate Net/Telnet.pm in @INC (@INC contains: /usr/lib/perl5/5.8.5/i386-linux-thread-multi /usr/lib/perl5/5.8.5 /usr/lib/perl5/site_perl/5.8.5/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.4/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.3/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.2/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.1/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.0/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.5 /usr/lib/perl5/site_perl/5.8.4 /usr/lib/perl5/site_perl/5.8.3 /usr/lib/perl5/site_perl/5.8.2 /usr/lib/perl5/site_perl/5.8.1 /usr/lib/perl5/site_perl/5.8.0 /usr/lib/perl5/site_perl /usr/lib/perl5/vendor_perl/5.8.5/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.4/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.3/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.2/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.1/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.0/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.5 /usr/lib/perl5/vendor_perl/5.8.4 /usr/lib/perl5/vendor_perl/5.8.3 /usr/lib/perl5/vendor_perl/5.8.2 /usr/lib/perl5/vendor_perl/5.8.1 /usr/lib/perl5/vendor_perl/5.8.0 /usr/lib/perl5/vendor_perl .) at tel.pl line 1.
BEGIN failed--compilation aborted at tel.pl line 1.

Jamal   

Posted at 7:59am on Wednesday, May 28th, 2008

I got following error.Can somebody help to fix this issue.?

Can't locate Net/Telnet.pm in @INC at c:test.pl line 3.

Begin failed --- compilation aborted at C:test.pl line 3.

Rajasekhar   

Posted at 2:59am on Wednesday, June 11th, 2008

my script is :
==============
#!/usr/bin/perl -w
use Net::Telnet(Timeout=>10,Errmode=>'die');

$telnet->open("135.132.144.1");
$telnet->waitfor("/login: /i");
$telnet->print("raja");
$telnet->waitfor("/Password:/i");

But it is throwing follwing error:-
====================================
perl -w tnet
Can't locate Net/Telnet.pm in @INC (@INC contains: /usr/perl5/5.00503/sun4-solaris /usr/perl5/5.00503 /usr/perl5/site_perl/5.005/sun4-solaris /usr/perl5/site_perl/5.005 .) at tnet line 2.
BEGIN failed--compilation aborted at tnet line 2.

Anonymous   

Posted at 6:21am on Wednesday, June 11th, 2008

Grep in google for Telnet.pm and make this module in your host pc

Anonymous   

Posted at 5:30am on Friday, July 11th, 2008

cpan install Net::Telnet

Jeff   

Posted at 12:26pm on Friday, July 18th, 2008

Hello, Quick question. I am using a .bat to call this perl script and run in it my cmd window. The telnet functionality works per say, but I ultimately do not establish a telnet connection. Is this a limitation? Or do I need to check my connection port? Thanks

Jeff   

Posted at 12:36pm on Friday, July 18th, 2008

Another way to ask this question, after the scrip runs the window terminates and I am left with no telnet connectivity. What do I need to do?

Jeff   

Posted at 12:39pm on Friday, July 18th, 2008

#!/usr/bin/perl

use Net::Telnet;

print"Connectingn";
$telnet = new Net::Telnet ( Timeout=>10, Errmode=>'die');

print"openningn";
$telnet->open("BOXNAME");

$telnet->waitfor('/login: $/i');
$telnet->print("USERNAME");

$telnet->waitfor('/Password: $/i');

$telnet->print('PASSWORD');

Akif Irshad   

Posted at 3:21am on Friday, July 25th, 2008

For..Jamal
Please install Telnet package for your perl or copy Telnet.pm in your "net" directory available in perl installation forlder , then all telnet commands wil work for you in perl scripts

Akif Irshad   

Posted at 3:22am on Friday, July 25th, 2008

For..Jamal
Please install Telnet package for your perl or copy Telnet.pm in your "net" directory available in perl installation forlder , then all telnet commands wil work for you in perl scripts
Akif Irshad(Pakistan)

Venkat   

Posted at 4:41pm on Friday, August 1st, 2008

Hi,

When I'm trying to telnet my Linux machine from my Windows XP machine through the following script:

#! /usr/bin/perl -w

use strict;
use warnings;
use Net::Telnet;

my $telnet = new Net::Telnet ( Timeout => 10, Errmode => 'die');
$telnet->open('192.168.50.101');
#print "Connectedn";
$telnet->waitfor('/login: /');
$telnet -> print ("datn");
$telnet -> waitfor('/password: $/i');
$telnet -> print ("sai123n");
$telnet -> waitfor('/W$ $/i');
$telnet -> print ("ls -ln");
$telnet -> waitfor('/W$ $/i');


and when I'm running in debug wiht warning mode I was getting the following:

C:>perl -dW telnet.pl
Useless use of a variable in void context at C:/Perl/lib/perl5db.pl line 1007.
Use of uninitialized value $DB::stack_depth in addition (+) at C:/Perl/lib/perl5db.pl line 3650.
Use of uninitialized value $DB::deep in numeric eq (==) at C:/Perl/lib/perl5db.pl line 3663.
Use of uninitialized value $DB::frame in bitwise and (&) at C:/Perl/lib/perl5db.pl line 3747.
Use of uninitialized value $DB::doret in string eq at C:/Perl/lib/perl5db.pl line 3759.
Use of uninitialized value $DB::frame in bitwise and (&) at C:/Perl/lib/perl5db.pl line 3759.
Useless use of a variable in void context at C:/Perl/lib/perl5db.pl line 6400.
Use of uninitialized value $DB::filename in concatenation (.) or string at C:/Perl/lib/perl5db.pl line 1049.
Use of uninitialized value $DB::filename in concatenation (.) or string at C:/Perl/lib/perl5db.pl line 1049.
Use of uninitialized value $DB::filename in concatenation (.) or string at C:/Perl/lib/perl5db.pl line 1049.
Use of uninitialized value $DB::filename in concatenation (.) or string at C:/Perl/lib/perl5db.pl line 1049.
Use of uninitialized value $DB::filename in concatenation (.) or string at C:/Perl/lib/perl5db.pl line 1049.
Use of uninitialized value $DB::filename in concatenation (.) or string at C:/Perl/lib/perl5db.pl line 1049.
Use of uninitialized value $DB::filename in concatenation (.) or string at C:/Perl/lib/perl5db.pl line 1049.
Use of uninitialized value $DB::filename in concatenation (.) or string at C:/Perl/lib/perl5db.pl line 1049.
Use of uninitialized value $DB::filename in concatenation (.) or string at C:/Perl/lib/perl5db.pl line 1049.
Use of uninitialized value $DB::filename in concatenation (.) or string at C:/Perl/lib/perl5db.pl line 1049.
No such signal: SIGBUS at C:/Perl/lib/perl5db.pl line 7799.
at C:/Perl/lib/perl5db.pl line 7799
DB::signalLevel(1) called at C:/Perl/lib/perl5db.pl line 1238
require perl5db.pl called at telnet.pl line 0
main::BEGIN() called at C:/Perl/lib/perl5db.pl line 0
eval {...} called at C:/Perl/lib/perl5db.pl line 0
Subroutine Cwd::fastcwd redefined at C:/Perl/lib/Cwd.pm line 732.
at C:/Perl/lib/Cwd.pm line 732
require Cwd.pm called at C:/Perl/lib/ActiveState/Path.pm line 13
ActiveState::Path::BEGIN() called at C:/Perl/lib/Cwd.pm line 0
eval {...} called at C:/Perl/lib/Cwd.pm line 0
require ActiveState/Path.pm called at C:/Perl/lib/ActivePerl/Config.pm line 46
ActivePerl::Config::override('pager', undef) called at C:/Perl/lib/Config_heavy.pl line 1131
Config::fetch_string('Config=HASH(0x19f15ec)', 'pager') called at C:/Perl/lib/Config.pm line 57
Config::FETCH('Config=HASH(0x19f15ec)', 'pager') called at C:/Perl/lib/perl5db.pl line 1257
require perl5db.pl called at telnet.pl line 0
main::BEGIN() called at C:/Perl/lib/Cwd.pm line 0
eval {...} called at C:/Perl/lib/Cwd.pm line 0
Subroutine Cwd::getcwd redefined at C:/Perl/lib/Cwd.pm line 732.
at C:/Perl/lib/Cwd.pm line 732
require Cwd.pm called at C:/Perl/lib/ActiveState/Path.pm line 13
ActiveState::Path::BEGIN() called at C:/Perl/lib/Cwd.pm line 0
eval {...} called at C:/Perl/lib/Cwd.pm line 0
require ActiveState/Path.pm called at C:/Perl/lib/ActivePerl/Config.pm line 46
ActivePerl::Config::override('pager', undef) called at C:/Perl/lib/Config_heavy.pl line 1131
Config::fetch_string('Config=HASH(0x19f15ec)', 'pager') called at C:/Perl/lib/Config.pm line 57
Config::FETCH('Config=HASH(0x19f15ec)', 'pager') called at C:/Perl/lib/perl5db.pl line 1257
require perl5db.pl called at telnet.pl line 0
main::BEGIN() called at C:/Perl/lib/Cwd.pm line 0
eval {...} called at C:/Perl/lib/Cwd.pm line 0
Subroutine Cwd::abs_path redefined at C:/Perl/lib/Cwd.pm line 732.
at C:/Perl/lib/Cwd.pm line 732
require Cwd.pm called at C:/Perl/lib/ActiveState/Path.pm line 13
ActiveState::Path::BEGIN() called at C:/Perl/lib/Cwd.pm line 0
eval {...} called at C:/Perl/lib/Cwd.pm line 0
require ActiveState/Path.pm called at C:/Perl/lib/ActivePerl/Config.pm line 46
ActivePerl::Config::override('pager', undef) called at C:/Perl/lib/Config_heavy.pl line 1131
Config::fetch_string('Config=HASH(0x19f15ec)', 'pager') called at C:/Perl/lib/Config.pm line 57
Config::FETCH('Config=HASH(0x19f15ec)', 'pager') called at C:/Perl/lib/perl5db.pl line 1257
require perl5db.pl called at telnet.pl line 0
main::BEGIN() called at C:/Perl/lib/Cwd.pm line 0
eval {...} called at C:/Perl/lib/Cwd.pm line 0

Loading DB routines from perl5db.pl version 1.3
Editor support available.

Enter h or `h h' for help, or `perldoc perldebug' for more help.

main::(telnet.pl:7): my $telnet = new Net::Telnet ( Timeout => 10, Errmode => 'die');
No such signal: SIGWINCH at C:/Perl/lib/Term/ReadLine/readline.pm line 427.
at C:/Perl/lib/Term/ReadLine/readline.pm line 427
readline::get_window_size called at C:/Perl/lib/Term/ReadLine/readline.pm line 1113
readline::init called at C:/Perl/lib/Term/ReadLine/readline.pm line 208
require Term/ReadLine/readline.pm called at C:/Perl/lib/Term/ReadLine/Perl.pm line 64
eval {...} called at C:/Perl/lib/Term/ReadLine/Perl.pm line 64
Term::ReadLine::Perl::new('Term::ReadLine', 'perldb', 'GLOB(0x18dd424)', 'GLOB(0x187fc44)') called at C:/Perl/lib/perl5db.pl line 5998
DB::setterm called at C:/Perl/lib/perl5db.pl line 2227
DB::DB called at telnet.pl line 7
Use of uninitialized value $tid in concatenation (.) or string at C:/Perl/lib/perl5db.pl line 2227.
at C:/Perl/lib/perl5db.pl line 2227
DB::DB called at telnet.pl line 7

DB n
main::(telnet.pl:8): $telnet->open('192.168.50.101');
Use of uninitialized value $tid in concatenation (.) or string at C:/Perl/lib/perl5db.pl line 2227.
at C:/Perl/lib/perl5db.pl line 2227
DB::DB called at telnet.pl line 8

DB n
Argument "" isn't numeric in numeric ge (>=) at C:/Perl/lib/Net/Telnet.pm line 2837.
at C:/Perl/lib/Net/Telnet.pm line 2837
Net::Telnet::_optimal_blksize('') called at C:/Perl/lib/Net/Telnet.pm line 1332
Net::Telnet::open('Net::Telnet=GLOB(0x1f16644)', 192.168.50.101) called at telnet.pl line 8
main::(telnet.pl:10): $telnet->waitfor('/login: /');
Use of uninitialized value $tid in concatenation (.) or string at C:/Perl/lib/perl5db.pl line 2227.
at C:/Perl/lib/perl5db.pl line 2227
DB::DB called at telnet.pl line 10
DB n
main::(telnet.pl:11): $telnet -> print ("datn");
Use of uninitialized value $tid in concatenation (.) or string at C:/Perl/lib/perl5db.pl line 2227.
at C:/Perl/lib/perl5db.pl line 2227
DB::DB called at telnet.pl line 11
DB n
main::(telnet.pl:12): $telnet -> waitfor('/password: $/i');
Use of uninitialized value $tid in concatenation (.) or string at C:/Perl/lib/perl5db.pl line 2227.
at C:/Perl/lib/perl5db.pl line 2227
DB::DB called at telnet.pl line 12
DB n
pattern match timed-out at telnet.pl line 12
at C:/Perl/lib/Net/Telnet.pm line 2036
Net::Telnet::_croak('Net::Telnet=GLOB(0x1f16644)', 'pattern match timed-out') called at C:/Perl/lib/Net/Telnet.pm line 539
Net::Telnet::error('Net::Telnet=GLOB(0x1f16644)', 'pattern match timed-out') called at C:/Perl/lib/Net/Telnet.pm line 1995
Net::Telnet::waitfor('Net::Telnet=GLOB(0x1f16644)', '/password: $/i') called at telnet.pl line 12
Debugged program terminated. Use q to quit or R to restart,
use o inhibit_exit to avoid stopping after program termination,
h q, h R or h o to get additional info.
Use of uninitialized value $tid in concatenation (.) or string at C:/Perl/lib/perl5db.pl line 2227.
at C:/Perl/lib/perl5db.pl line 2227
DB::DB called at C:/Perl/lib/perl5db.pl line 9451
DB::fake::at_exit() called at C:/Perl/lib/perl5db.pl line 9022
DB::END() called at C:/Perl/lib/Net/Telnet.pm line 2036
eval {...} called at C:/Perl/lib/Net/Telnet.pm line 2036
DB q

can any body look into the same and do the needful, please.

san   

Posted at 8:33am on Wednesday, October 1st, 2008

i need to telnet to a host machine and from tat machine i need to ftp to another server... can anyone post the code for this..

arp   

Posted at 7:13am on Wednesday, October 15th, 2008

I am using telnet with ruby
My code works fine on Windows platform but gives error when i try to do the same thin using Linux, it does not creates the object only, when i call Net::Telnet.new with all the parameters .
so can anyone help?
is there any changes that are need to be made

topepsi2   

Posted at 9:39am on Friday, November 7th, 2008

Pepsi, you're experienceing ANSI terminal codes that format the data for your screen. I'm having the same problem accessing an Avaya PBX system that uses AT&T513 terminal codes. I'm wondering if there's any easy way to convert them to the formatting that would result if I was actually using a terminal emulator.

Dhiraj   

Posted at 3:11am on Thursday, January 8th, 2009

hi all,
Can any one tell me how we use Net::Telnet;
for windows machine.
i mean for windows to windows telnet connection can i do this. If is it so then plz send me the real time code snippet

thanks in advance.
Dhiraj

Mike   

Posted at 12:09pm on Wednesday, January 21st, 2009

use Net::SSH, telnet is not secure.

Thil   

Posted at 10:19pm on Monday, February 2nd, 2009

Hi,

Ran into this error while executing Net::Telnet on a solaris server

Can't locate Net/Telnet.pm in @INC (@INC contains: /usr/perl5/5.8.4/lib/sun4-sol
aris-64int /usr/perl5/5.8.4/lib /usr/perl5/site_perl/5.8.4/sun4-solaris-64int /u
sr/perl5/site_perl/5.8.4 /usr/perl5/site_perl /usr/perl5/vendor_perl/5.8.4/sun4-
solaris-64int /usr/perl5/vendor_perl/5.8.4 /usr/perl5/vendor_perl .) at ./test
.pl line 3.
BEGIN failed--compilation aborted at ./test.pl line 3.

Could someone tell me where I can get Net/Telnet.pm module?

Thanks,
Thil

John   

Posted at 12:15am on Wednesday, March 18th, 2009

when I use @lines=>$t->cmd("who"); I get Command timed out error, But input.txt contains the actual response of the command. How will I get it printed. Should I check for any prompt match? Please help me with the syntax.

sami   

Posted at 10:03pm on Saturday, March 28th, 2009

I just want to know, how to integrate Net::Telnet Perl /Net::Telnet:Cisco modules in to main Perl program which is running on LINUX environment.

lora   

Posted at 9:48pm on Sunday, April 19th, 2009

I get Syntax Error near "'die' Prompt" :(

Shamim   

Posted at 8:52pm on Monday, April 27th, 2009

I had a requirement of stopping my output capture upon seeing a particular string. Your waitfor explanation has helped me with a solution.

ABHISHEK.R   

Posted at 10:15am on Monday, June 22nd, 2009

I am trying to connect to my QNX m/c from the host PC, which haswindows xp.I want to kill two processes which run on my QNX PC from my host PC.For this purpose I am using a perl script which uses Net::Telnet....Although the perl script seems to be proper and according to the guidelines given by ppl below, my perl script is not entering the login name.It opens the command prompt and stops there.Which version of perl supports Net::Telnet .Please help me out...

Kevin   

Posted at 3:23pm on Monday, July 20th, 2009

Hi, what is the pattern for root@hostname:/> ? I tried few variations and its failing..can you help? Thanks.

Sachin Sharan   

Posted at 10:11pm on Thursday, August 6th, 2009

when i type into CMD> "telnet 192.168.1.1". it asked for login= admin
password=
but i dont know the password. tell me this

Strom   

Posted at 5:02pm on Friday, September 4th, 2009

Excuse me. There are several good protections against temptations, but the surest is cowardice. Help me! There is an urgent need for sites: Treatment for thinning hair. I found only this - african american women thinning hair. They receive that a ideal infancy of the scissors head is required for aga to provide. Trackingfirst, mechanical chapters are known around the peach in similar products. :-) Thanks in advance. Strom from Serbia.

Jelena   

Posted at 5:16am on Sunday, September 6th, 2009

Give please. I will love the light for it shows me the way, yet I will endure the darkness for it shows me the stars.
I am from Republic and learning to read in English, please tell me right I wrote the following sentence: "Copenhagen has the two oldest slot changes in the world, easyhome prepaid mastercard."

Thank you so much for your future answers 8-). Jelena.

kewell   

Posted at 3:30pm on Saturday, January 9th, 2010

what about ssh? is that possibel to do the same thing with ssh authentication??

XL   

Posted at 8:41pm on Monday, January 18th, 2010

hi, i using XAMPP -apache as my web server. This application includes Apache, MySQL, Perl and PHP. so after i have download my Net::Telnet, where am i supposed to place this Net::Telnet folder into which folder of XAMPP?

Anonymous   

Posted at 6:40am on Thursday, February 25th, 2010

how do I save the output to a file ?

Shreyas Parikh   

Posted at 2:33am on Thursday, April 1st, 2010

nice tips good and easy to understandable

t   

Posted at 1:53pm on Thursday, April 1st, 2010

Hello,
What's the prompt for the following?
I'm trying
$t = new Net::Telnet(Timeout => 5,
Prompt => '/dm350 login:$/');
But it isn't working.

Thaks.
----------------------

Remote Session RidgeRun SDK
Embedded Linux Solutions

For further information see:
http://www.ridgerun.com

dm350 login:
---------

tim   

Posted at 1:54pm on Thursday, April 1st, 2010

Hello,
What's the prompt for the following?
I'm trying
$t = new Net::Telnet(Timeout => 5,
Prompt => '/dm350 login:$/');
But it isn't working.

Thanks
tim
----------------------

Remote Session RidgeRun SDK
Embedded Linux Solutions

For further information see:
http://www.ridgerun.com

dm350 login:
---------

ShanAJ   

Posted at 10:38pm on Monday, April 5th, 2010

Though I tried to compile the above perl script I got the error message "Pattern match time-out at Telnet.pl line 12"

My code is,
#!/usr/bin/perl

print "content-type: text/html";

use Net::Telnet;
$telnet = new Net::Telnet ( Timeout=>10,Errmode=>'die');
$telnet->open('192.168.1.1');
$telnet->waitfor('/login: $/i');
$telnet->print('root');
$telnet->waitfor('/password: $/i');
$telnet->print('123456');
$telnet->waitfor('/> $/i');
$telnet->print('version');
$output = $telnet->waitfor('/$ $/i');
print $output;

Samar Pradhan   

Posted at 3:23am on Wednesday, April 7th, 2010

Remove Prompt => '/$ $/i'); from the script if you are logged in as root..I have tried the following code and it WORKS....

#!C:Perlbin
use Net::Telnet;
$telnet = new Net::Telnet ( Timeout=>10, Errmode=>'die');
$telnet->open('10.77.66.142');
$telnet->login('root','Agnt2tst');
print $telnet->cmd('who');

Anonymous   

Posted at 5:18am on Thursday, April 15th, 2010

what prompt we require for match huawei router as in net::telnet.

Anonymous   

Posted at 1:47pm on Tuesday, May 18th, 2010

thanks for your input and hope you have a good day

SC   

Posted at 12:41pm on Monday, December 6th, 2010

how can we achieve this result:
file.txt contains 10 devices. a couple of device on telnet prompt for "press any key to continue", and when you hit any key then Username, password prompts.
where as other devices directly prompts for username, password.

How can we program it? so that if any device do not prompts for 'press any key' then it should go to username,password

$t = Net::Telnet->new(Host => $host)
$t->open($host);

$t->waitfor('/Username:s*/');
$t->print($username);
$t->waitfor('/Password:s*/');
$t->print($pass);

$t->print('enable');
$t->waitfor('/Username:s*/');
$t->print($user);
$t->waitfor('/Password:s*/');
$t->print($ena);

SC

SC   

Posted at 12:49pm on Monday, December 6th, 2010

how can we achieve this result:
file.txt contains 10 devices. a couple of device on telnet prompt for "press any key to continue", and when you hit any key then Username, password prompts.
where as other devices directly prompts for username, password.

How can we program it? so that if any device do not prompts for 'press any key' then it should go to username,password.

The below code only works devices which prompt's for 'press any key to continue'. Can any one fix it to apply condition to skip it if it is not prompted.




$t = Net::Telnet->new(Host => $host)
$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($pass);

$t->print('enable');
$t->waitfor('/Username:s*/');
$t->print($user);
$t->waitfor('/Password:s*/');
$t->print($ena);

ranjan   

Posted at 11:51am on Wednesday, December 22nd, 2010

hi friends,i connected 2 routers and 2 host usig static routing... script is

use Net::Telnet::Cisco;
my $session = Net::Telnet::Cisco->new(Host => '172.17.0.2');

$session->login('cisco', 'cisco');

if ($session->enable(`cisco`))
{
@output = $session->cmd(`conf t`);
@output = $session->cmd(`no ip source-route`);

print `Policy1: DISALLOW ALL SOURCE ROUTINGn`;
}
else
{
warn `Canít enable:`. $session -> errmsg;
}
$session -> close;




got Error : pattern match read eof at policy1.pl line 4.

tel how i can solve the prob and how i can conect that router using perl module....
please help me asp to my mail who all are expert in those area....
mail- ranjanxp@hotmail.com
thx advance... plase help me sooon....

Sankar   

Posted at 10:44pm on Monday, January 10th, 2011

Nice

Sankar   

Posted at 10:45pm on Monday, January 10th, 2011

Nice

Jenny   

Posted at 12:12pm on Wednesday, February 2nd, 2011

Try this testport.pl script:


======================================
#!/usr/bin/perl

use IO::Socket;

@servers = ("server1", "server2");

foreach $iserver (@servers)
{
print "Current server===>$iservern";

for ($port=6020; $port $iserver,
PeerPort => $port,
Proto => 'tcp', );

if (!$sock)
{
print " tNOT connected to $iserver:$portn";
next;
}

##die "Could not create socket : $!n" unless $sock;

$client = $sock->accept();
$host = $sock->sockhost();
print " tConnected to $iserver:$portn";
close($sock);
}
}

Jenny   

Posted at 12:12pm on Wednesday, February 2nd, 2011

Try this testport.pl script:


======================================
#!/usr/bin/perl

use IO::Socket;

@servers = ("server1", "server2");

foreach $iserver (@servers)
{
print "Current server===>$iservern";

for ($port=6020; $port $iserver,
PeerPort => $port,
Proto => 'tcp', );

if (!$sock)
{
print " tNOT connected to $iserver:$portn";
next;
}

##die "Could not create socket : $!n" unless $sock;

$client = $sock->accept();
$host = $sock->sockhost();
print " tConnected to $iserver:$portn";
close($sock);
}
}

Deepak   

Posted at 8:24am on Wednesday, March 23rd, 2011

Hi Guys,
I'm using windows cmd prompt. My prompt look like,
C:perl_projects>
What shud be the wait for command that i have to use?

Please help me.

Regards,
Deepak

fox95   

Posted at 8:10am on Sunday, April 10th, 2011

great doc!!!

Bighna Raj Mallik,402-998-7828   

Posted at 6:08am on Wednesday, July 6th, 2011

Hi,
This hpccp010#[/home/mes] is my log in prmopt.When I logged into this server it always takes me to "hpccp010[/home/mes]"
Can you please tell me what should be the Prompt() for this.

Below is my code ...
#!C:perlbinperl.exe
use net::telnet;

$remote = new Net::Telnet (Timeout => 20, Errmode=>'die');
$remote->open('hpccp010');
#$remote->waitfor('/login as: $/i');
#$remote->print('mesop');
#$remote->waitfor('/Password: $/i');
#$remote->print('mesop!1');
$remote->login('mesop', 'mesop!1');
print $remote->waitfor('/]$/i');
print $remote->cmd('who');
$remote->close;

Please help me as I got stucked somewhere in the mod of tool devloppment.

rahul   

Posted at 9:58pm on Wednesday, July 27th, 2011

hmmmmmmmmm.......

MarquezMamie   

Posted at 1:50am on Thursday, September 22nd, 2011

According to my own monitoring, thousands of people on our planet receive the loans at various creditors. Therefore, there is a good chance to get a credit loan in all countries.

DougM   

Posted at 7:44pm on Thursday, September 22nd, 2011

I started with this module this morning. We are running it on HPUX & our Perl is at 5.8.8 - the module works very well.

I had some trouble understanding what some of the code did but I just read through all the posts and was able to resolve all my problems by reading them first.

Below is my working program that Telnets to a Controller (router) then connects to an attached device managed by this router (The Router is a Telindus Orchid1035 card & the attached devices are Telindus Aster5 Modems, we use the Modems to take in dial up orders ($6 Billion PA).

The Aster5 modems allow us to access them via the Orchid card to check their status & even reboot them dynamically when a hang up occurs (happens enough that we need to do it).

The prompt char from the Telindus router is '>'

I hope seeing this program helps others solve their problems.

# Perl script to telnet to Orchid Router then access a modem & reboot it
# Uses a CPAN module called NET::Telnet 3.03.

# http://www.perlfect.com/articles/telnet.shtml
# http://search.cpan.org/~jrogers/Net-Telnet-3.03/lib/Net/Telnet.pm

my $host=@ARGV[0];
my $modem=@ARGV[1];
my @resp;

use Net::Telnet;
print "modem.boot.aster5.pl: HOST=$host MODEMNAME=$modemn";

# $telnet = new Net::Telnet ( Timeout=>10, Errmode=>'die' );
$telnet = new Net::Telnet ( Timeout=>10, Errmode=>'die', Prompt => '/>$/i' );

$telnet->open($host); print "Opened host...n";

# $telnet->waitfor('/Username: $/i');
$telnet->waitfor('/Username:/'); $telnet->print('n'); # send login (currently blank)
print "Entered Orchid username ok ...n";


$telnet->waitfor('/Password:/'); $telnet->print('n'); # send password (currently blank)
print "Entered Orchid password ok ...n";


@resp=$telnet->waitfor('//telindus1035:"Edit Configuration"/');
print "@respn";


@resp=$telnet->print('get sysName'); # Ask the host for its config name
@resp=$telnet->waitfor('//telindus1035:"Edit Configuration"/');
print "@respn";


@resp=$telnet->print('connect '.$modem);
$telnet->waitfor('/Username:/'); $telnet->print('n'); # send login (currently blank)
print "Entered Modem username ok ...n";


$telnet->waitfor('/Password:/'); $telnet->print('n'); # send password (currently blank)
print "Entered Modem password ok ...n";


@resp=$telnet->waitfor('//aster5:"Edit Configuration"/');
print "@respn";


@resp=$telnet->print('get sysName');
@resp=$telnet->waitfor('//aster5:"Edit Configuration"/');
print "@respn";


$telnet->print('action "Cold Boot"'); # send reboot
@resp=$telnet->waitfor('/OK/');
print "@respn";


$telnet->print('action "Cold Boot"'); # send reboot
@resp=$telnet->waitfor('/OK/');
print "@respn";
@resp=$telnet->waitfor('//aster5:"Edit Configuration"/');
print "@respn";


@resp=$telnet->print("disconnect");
@resp=$telnet->waitfor('/closing session .../');
print "@respn";
print "Disconnectedn";

exit 0;

DougM   

Posted at 7:54pm on Thursday, September 22nd, 2011

added comments to the code I submitted above - I notice that in some lines where I needed backslahes that putting the code in the above post removed those back slashes so some lines are not what I coded.

e.g. in the waitfor commands there are 2 lines with ...
/telindus1035:"Edit Configuration" and /aster5:"Edit Configuration"

Each of those lines is meant to be between two slashes

i.e. waitfor('/....../') where the dots are the above 2 lines. But in my original code I had a backslash before the /aster and /telindus but posting it here stripped them out.

So if some code looks funny ask yourself if there is a missing backslash.

DougM

VinsonRUBY   

Posted at 5:19pm on Sunday, October 9th, 2011

According to my analysis, billions of persons in the world receive the home loans at different banks. Thence, there's a good possibility to find a consolidation loan in all countries.

BENTLEY21Alicia   

Posted at 4:26pm on Monday, October 10th, 2011

I received my first loans when I was 25 and this aided me a lot. However, I need the financial loan as well.

CaseSofia28   

Posted at 7:18am on Sunday, October 16th, 2011

Don't you know that it's high time to receive the loan, which can help you.

HouseKendra   

Posted at 8:50am on Monday, November 14th, 2011

I had got a desire to begin my business, but I did not have got enough amount of cash to do that. Thank God my fellow said to take the business loans. Hence I received the financial loan and realized my old dream.

vishal   

Posted at 1:24am on Monday, November 28th, 2011

i ' m engenius telnet bat error could not open connection to the host,on port 23 connect failed what problem with solution

HarrisonMelisa18   

Posted at 5:12pm on Saturday, December 10th, 2011

If you are in the corner and have no money to move out from that point, you would have to receive the loan. Because it would help you for sure. I take financial loan every year and feel great because of this.

pravin pawar   

Posted at 4:46am on Wednesday, January 18th, 2012

good and easy to start a telnet in perl excelent

hna   

Posted at 7:17am on Monday, February 6th, 2012

very good

Ganesh   

Posted at 6:16am on Wednesday, March 14th, 2012

Hi experts,

I am facing Thommy's issue. Can anyone help me on this??
thx

Ravi   

Posted at 8:40am on Tuesday, August 21st, 2012

Just about all I can state is, I am not sure what to comment! Except neesleds to say, for the great tips that are shared within this blog. I could think of a million fun ways to read the articles on this site. I do think I will at last take a step with your tips on that matter I could never have been able to take care of alone. You are so careful to allow me to be one of those to benefit from your valuable information. Please know how significantly I enjoy the whole thing.

fnjbdd   

Posted at 9:42pm on Tuesday, August 21st, 2012

gpbpBo voeiobbejknw

pcqdojepn   

Posted at 4:12am on Friday, August 24th, 2012

e6LNOq fucjyeeokueu

Attila   

Posted at 3:43am on Thursday, November 29th, 2012

Great one!

Is it possible to set the buffer larger? Full BGP routing table would not fit the default size!

c-h-k.org   

Posted at 3:35am on Wednesday, December 19th, 2012

if you get: pattern match timed-out at ...

try to:
$output = $telnet->waitfor(String => '>',Timeout => 5);

docs here:

http://www.softpanorama.info/Admin/Monitoring/telnet_simulation_in_perl.shtml

asd   

Posted at 11:17am on Friday, July 19th, 2013

asd

asd   

Posted at 11:17am on Friday, July 19th, 2013

asd

HEYA   

Posted at 11:19am on Friday, July 19th, 2013

alert('HELLO THERE!')

HEYA   

Posted at 11:19am on Friday, July 19th, 2013

alert('HELLO THERE!')

oxhulctafn   

Posted at 11:57am on Sunday, January 19th, 2014

fgofbqfsmgfdu, etekkzhahx

zukyfosdmu   

Posted at 2:42am on Wednesday, January 22nd, 2014

mvgbvqfsmgfdu, lfsbhplbzi

ejphnfvgoq   

Posted at 5:45am on Saturday, January 25th, 2014

khameqfsmgfdu, http://www.wqzeumarvp.com/ ugxlqnjzmp

jamie   

Posted at 10:33pm on Wednesday, February 5th, 2014

Hi all,
I am getting the below error Can't parse prompt: '/Proceed with reload? [confirm]$/i'

code

my @out=$session->print('reload');
print @out;
$session->waitfor('/Proceed with reload? [confirm]$/i') ;
#my $output = $session->waitfor('/$ $/i');
$session->send('yes');

pls help

qxaqliqugp   

Posted at 2:35am on Saturday, March 22nd, 2014

wmlgqqfsmgfdu, ututkzzafy

hfducpyjho   

Posted at 11:36am on Monday, March 24th, 2014

gssilqfsmgfdu, http://www.dqeqnbrbcw.com/ nfgtyxomlp

qmueqfctkm   

Posted at 7:42am on Thursday, March 27th, 2014

ccdxoqfsmgfdu, http://www.bnffgdcvqs.com/ yncoqowmrt

hkaaojkgwz   

Posted at 6:48pm on Monday, April 14th, 2014

lbdfsqfsmgfdu, xkcgxlrtut

Comments to date: 133.

Like it? Share it!