By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,848 Members | 1,363 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,848 IT Pros & Developers. It's quick & easy.

some usage doubts on net::telnet

100+
P: 170
hi guys,
a question on net::telnet
what does this module do?
cos i have a telnet client that will be able to communicate with a database
so is it true if i enter my ip address, username, and password and using print later again to execute some commands
i will get some results on some screen??



Expand|Select|Wrap|Line Numbers
  1. use Net::Telnet;
  2.  
  3. $telnet3 = new Net::Telnet;
  4. $telnet3->open('IP_ADDRESS'); # Substituted fake IP
  5. $telnet3->waitfor('/Enter login: $/i');
  6. $telnet3->print('USERNAME'); # Substituted fake User
  7. $telnet3->waitfor('/Enter password: $/i');
  8. $telnet3->print('PASSWORD'); # Substituted fake PW
  9.  
  10. print "Logged in to device\n";
  11.  
Apr 16 '08 #1
Share this Question
Share on Google+
21 Replies


numberwhun
Expert Mod 2.5K+
P: 3,503
hi guys,
a question on net::telnet
what does this module do?
cos i have a telnet client that will be able to communicate with a database
so is it true if i enter my ip address, username, and password and using print later again to execute some commands
i will get some results on some screen??



Expand|Select|Wrap|Line Numbers
  1. use Net::Telnet;
  2.  
  3. $telnet3 = new Net::Telnet;
  4. $telnet3->open('IP_ADDRESS'); # Substituted fake IP
  5. $telnet3->waitfor('/Enter login: $/i');
  6. $telnet3->print('USERNAME'); # Substituted fake User
  7. $telnet3->waitfor('/Enter password: $/i');
  8. $telnet3->print('PASSWORD'); # Substituted fake PW
  9.  
  10. print "Logged in to device\n";
  11.  
If you read the page, you will find that the print() function of that module will write what you put in there, specifically after the connection is made. What you want is the login() function to log in. The CPAN page shows you how to use this module.

Regards,

Jeff
Apr 16 '08 #2

100+
P: 170
hi guys,
alright i tried to see if my connection works using the following code
but when i use the program it apparents open the perl command line interpreter that just waits for some time before closing the interpreter

i dunno what was happening but apparently my guess it timeout cos there's some errors

is there any way to check if i'm connected to the IP i stated?

Expand|Select|Wrap|Line Numbers
  1. use Net::Telnet;
  2.  
  3. $telnet3 = new Net::Telnet(timeout => 10,
  4.                 Errmode => 'die');
  5.  
  6. $telnet3->open('IP_ADDRESS'); 
  7. $telnet3->login(username, password);
  8. @lines = $telnet3->cmd("who");
  9.  
  10.  
  11. open(FILE, ">file.txt");
  12.  
  13.     print FILE ("@lines\n");
  14.  
Apr 17 '08 #3

nithinpes
Expert 100+
P: 410
You can catch the errors as below:

Expand|Select|Wrap|Line Numbers
  1. $telnet3->open('IP_ADDRESS') or die "failed to connect:$!"; 
  2. $telnet3->login(username, password) or die "login failed:$!";
  3.  
Apr 17 '08 #4

100+
P: 170
hm..tried that
still the same
like a the perl interpreter screen comes up
and after the default timeout of 10
the screen closes
no errors caught and i still got no idea if connection is made
pls help
Apr 17 '08 #5

nithinpes
Expert 100+
P: 410
hm..tried that
still the same
like a the perl interpreter screen comes up
and after the default timeout of 10
the screen closes
no errors caught and i still got no idea if connection is made
pls help
Before trying to catch the error, you need to change Errmode in the line where you are creating new instance of object.

Expand|Select|Wrap|Line Numbers
  1. $telnet3 = new Net::Telnet(timeout => 10,
  2.                 Errmode => 'return');
  3.  
  4.  
Apr 17 '08 #6

100+
P: 170
right..i've actually done what u have said to try and catch the error
but i think there's a problem
the screen display the error and then closes it in 1 sec
it's almost impossible to see the error msg
any one experience this kind of problem before?
i'm using activestate perl 5.10 on windows XP
Apr 17 '08 #7

KevinADC
Expert 2.5K+
P: 4,059
check your Windows firewall ( or any other one you may have running) and see if it is blocking telnat access. If you don't know how to check consult your Windows XP help files.

Also, don't double-click on your perl file to run it. Open a DOS window and type in:

Expand|Select|Wrap|Line Numbers
  1. c:\>perl yourscript.pl
provide full paths if necessary

Expand|Select|Wrap|Line Numbers
  1. c:\>Perl\bin\perl.exe path\to\yourscript.pl
then press enter and the windows will stay open.
Apr 17 '08 #8

100+
P: 170
thanks for the help
ok
right now the error msg is i can't login with the username and password
i got no idea why
it isn't blocked by any firewalls
and when i telnet using the command prompt using the same password and username it is able to log into my telnet application
any idea why i can't log in when i use net::telnet??
Apr 17 '08 #9

100+
P: 170
alright here's the warning message
i dunno if it helps

the warning mesage is

"global symbol $telnet3 requires explicit package name"
Apr 17 '08 #10

KevinADC
Expert 2.5K+
P: 4,059
instead of :

Expand|Select|Wrap|Line Numbers
  1. $telnet3 = 'foobar';
you need to declare it with "my":

Expand|Select|Wrap|Line Numbers
  1. my $telnet3 = 'foobar';
Thats what the error message you are getting means.
Apr 17 '08 #11

100+
P: 170
OK
everything's fine but i guess the login has some problem
it alwayss time out at the login line
i read that i need to input some kind of prompt like bash
but i know that's for linux
does this prompt makes the differences on windows XP such that it can't read my login?
i really dunno why it's not logging in
pls help look into this? thank you
Apr 17 '08 #12

numberwhun
Expert Mod 2.5K+
P: 3,503
OK
everything's fine but i guess the login has some problem
it alwayss time out at the login line
i read that i need to input some kind of prompt like bash
but i know that's for linux
does this prompt makes the differences on windows XP such that it can't read my login?
i really dunno why it's not logging in
pls help look into this? thank you
My suggestion then, is to just do a telnet from the command line and see if it works. If not, then you need an account on that other machine. Also, make sure the other machine is allowing telnet as well.

Regards,

Jeff
Apr 17 '08 #13

100+
P: 170
yup
it's allowing me to enter into the switch
i'm telnetting into a switch basically
from command line it can be done
how ever from the module net::telnet
i can't seem to log it in...i got no idea why

u might ask why i'm doing this rather from command line..
basically i'm creating a script to communicate one data base to another proprietary database
and the proprietary database can only be logged in by telnetting into the swtich
so the i still can't establishing a connection using net::telnet...
so any clues?
:)
Apr 18 '08 #14

numberwhun
Expert Mod 2.5K+
P: 3,503
yup
it's allowing me to enter into the switch
i'm telnetting into a switch basically
from command line it can be done
how ever from the module net::telnet
i can't seem to log it in...i got no idea why

u might ask why i'm doing this rather from command line..
basically i'm creating a script to communicate one data base to another proprietary database
and the proprietary database can only be logged in by telnetting into the swtich
so the i still can't establishing a connection using net::telnet...
so any clues?
:)
Not having done much of anything (at all) with this module, I am not sure why you aren't able to log in. I would re-read the module documentation and see if there is anything you may have missed.

Regards,

Jeff
Apr 18 '08 #15

100+
P: 170
ok thanks
hm..i've already done some verifying

Expand|Select|Wrap|Line Numbers
  1.     my ($t);
  2.  
  3.     use Net::Telnet ();
  4.  
  5.     $t = new Net::Telnet;
  6.  
  7.      $t->open("IP_ADDRESS");
  8.      $t->waitfor( -match => qr{ogin});
  9.      $t->print("USERNAME");
  10.      $t->waitfor( -match => qr{assword});
  11.      $t->print("PASSWORD");    
  12.     @lines = $t -> getlines (timeout=>20);
  13.     print @lines;
  14.  
to print out what happens when i log in
so when i "telnet IP_ADDSRESS"
it brings me to a page with ENTER USERNAME <
i manage to get the lines out when i print this
but when i started testing it with my login and password
nothing came out
cos it's suppose to like print "WELCOME TO DIALOGUE SESSION"

but nothing came out...
so i'm just wondering it could be the prompt matching that's causing the problem
the password and username aren't caught by the program
so see if anyone of u can detect the problem
Apr 18 '08 #16

nithinpes
Expert 100+
P: 410
Not sure if prompt matching is the cause. But if it is, try to use prompt matching as below:

Expand|Select|Wrap|Line Numbers
  1.     $t->waitfor('/login:/i');
  2.     $t->print($user);
  3.  
  4.     $t->waitfor('/password: $/i');
  5.     $t->print($passwd);
  6.  
Apr 18 '08 #17

100+
P: 170
hi nithinpes
alright i've got it to log in with yr code
perhaps u can have a look below:

Expand|Select|Wrap|Line Numbers
  1. #!perl\bin\perl
  2. use strict;
  3. use warnings;
  4.  
  5.   my $t; 
  6.   my $string;
  7.   my $data;
  8.  
  9.     use Net::Telnet(); 
  10.     $t = new Net::Telnet;
  11.  
  12.      $t->open("password"); #replace with yr own ip
  13.      $t->waitfor('/ENTER USERNAME < /i');
  14.      $t->print("username"); #replace with yr own username
  15.      $t->waitfor('/ENTER PASSWORD < /i');
  16.      $t->print("password"); #replace with yr own password
  17.      @lines = $obj->getlines (timeout=1500);
  18.      print @lines;
  19.  
so i use the code getlines to print out what i got on the screen so as to verify i got logged in
hm..
but the problem is i needed to use 1500 secs before i can it printed out
i think that's gonna pose a problem since it's taking such a long time
u all know why it's taking such a long long time to get the results printed out?
Apr 21 '08 #18

KevinADC
Expert 2.5K+
P: 4,059
Judging by your code I am confused if you are reading the Net::Telnet documentation.

Question: Where in it do you see this:

Expand|Select|Wrap|Line Numbers
  1. timeout=nnn
where nnn is any number.

Answer: You don't. Read the documentation and pay careful attention to the syntax:

Expand|Select|Wrap|Line Numbers
  1. Timeout => nnn
The code you posted should not even compile as you have not declared all of your variables properly.
Apr 21 '08 #19

100+
P: 170
opps i forgot to amend the mistakes before posting
it was the previous code

alright here's the right one
Expand|Select|Wrap|Line Numbers
  1. #!perl\bin\perl
  2. use strict;
  3. use warnings;
  4.  
  5.   my $t; 
  6.   my @lines;
  7.  
  8.  
  9.     use Net::Telnet(); 
  10.     $t = new Net::Telnet;
  11.  
  12.      $t->open("ip_Address");
  13.      $t->waitfor('/ENTER USERNAME < /i');
  14.      $t->print("username");
  15.      $t->waitfor('/ENTER PASSWORD < /i');
  16.      $t->print("password");
  17.      @lines=$t->getlines(timeout=>1500);
  18.      print @lines;
  19.  
hm..ok this should be right
yeap so my question will still be
why does it take like 1500 secs to return me the results, lesser than that
it times out

cos usually when i telnet using command prompt, they ask for username and password, then i enter into my switch control
is it cos the @lines started reading line the moment i log into my switch? that's why it's taking a long time to read all the lines when i log into my switch
Apr 21 '08 #20

KevinADC
Expert 2.5K+
P: 4,059
so my question will still be
why does it take like 1500 secs to return me the results, lesser than that
it times out

cos usually when i telnet using command prompt, they ask for username and password, then i enter into my switch control
is it cos the @lines started reading line the moment i log into my switch? that's why it's taking a long time to read all the lines when i log into my switch
I have no idea why it takes that long.
Apr 21 '08 #21

100+
P: 170
hm..ok thanks
but if anyone let me know
i guess for now i'll just do some testing and seee if it affects my development
i'm doing connections with mysql and telnet now
after which i will merge them up
iso if anyone knows just respond
many thanks :)
Apr 21 '08 #22

Post your reply

Sign in to post your reply or Sign up for a free account.