CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Net::Telnet help needed

 



Trunkenstein
Novice

Dec 29, 2011, 1:38 AM

Post #1 of 14 (1738 views)
Net::Telnet help needed Can't Post

Hello guys and girls,

I'm very new to programming with Perl, already learned a bit C++ and Java trough school but never Perl.

And now I need to start learning it with a, for a beginner, difficult task.

Problem:

I need to connect to some devices trough telnet, I need to give a username and password, send 1-2 commands and need to get a reply in form of a file of the device reply to the command.

I think it's more easy than I think but I'm stuck.

I already have figured out how to connect to a "normal" telnet server on my local machine (KpyM Telnet) and login but than, after inserting my passwort, the script just crashes or disconnects, no errors or anything.

If I use telnet by hand, after entering user/pass I get a normal cmd line in my "userfolder" of Windows so I thought, ok, I can send a

Code
$telnet->print('dir')

but after the login it seems that my script just disconnects.


The whole script looks like this:


Code
 
#! /usr/bin/perl
use strict;
use warnings;
use Net::Telnet;
my $telnet = new Net::Telnet ( timeout=>10, Errmode=>'die');



my $host = '10.X.X.X';
my $benutzer = 'user';
my $password = 'pass';
my $aa = $telnet->input_log('C:\_PerlTest\in_log.txt');
my $bb = $telnet->output_log('C:\_PerlTest\out_log.txt');



$telnet->open($host);
$telnet->waitfor('/Press any key to continue/');
$telnet->print(''); #cause a key needs to be pressed
$telnet->waitfor('/login:/');
$telnet->print($benutzer);
$telnet->waitfor('/password:/');
$telnet->print($password);
$telnet->print('dir'); #doesn't work, doesn't show up in the logs

print $telnet->lastline;


can anyone help me with this problem?
Gonna be the best (In other words: Hope it will work :D )


rovf
Veteran

Dec 29, 2011, 1:59 AM

Post #2 of 14 (1736 views)
Re: [Trunkenstein] Net::Telnet help needed [In reply to] Can't Post

First, I wonder why you do login manually. Net::Telnet has a login() method, which I would try first.

Second, if you really want to do it by yourself, I would put a sleep(2) before sending the password.


Trunkenstein
Novice

Dec 29, 2011, 2:33 AM

Post #3 of 14 (1735 views)
Re: [rovf] Net::Telnet help needed [In reply to] Can't Post

Cause later on, the devices I wan't to connect to, doesn't give out the correct patterns which can be evaluated by the login method
Gonna be the best (In other words: Hope it will work :D )


rovf
Veteran

Dec 29, 2011, 4:04 AM

Post #4 of 14 (1729 views)
Re: [Trunkenstein] Net::Telnet help needed [In reply to] Can't Post

I don't think so. You are expecting during the login process the patterns /login:/ and /password:/, and reading the documentation shows that this is exactly the default.

BTW, I now also see that you don't wait for a prompt, after the login has succeeded. This means that if you try the login() function, and that system's prompt does not correspond to the default prompt, this won't work unless you explicitly define the correct prompt (which is a good idea anyway).

In your version, where you send the "dir" command immediately after the password, it might be that you are too fast, and the command gets lost. You should either wait a few seconds, or do a waitfor() in between.

Another issue to consider is the correct line ending. Currently, your "print" always appends the current output_record_separator. I don't remember whether this works with sending the password too, because during password entry, the terminal might be turned into raw mode. I suggest that, even if you believe that the standard login() routine is not suited for your case, you at least check the code of login() to see how they deal with sending the password.

BTW, both systems (client and server) are Unix/Linux?


Trunkenstein
Novice

Dec 29, 2011, 4:22 AM

Post #5 of 14 (1727 views)
Re: [rovf] Net::Telnet help needed [In reply to] Can't Post

Both machines are currently the same Microsoft Windows XP machine, later on the client will be MS Windows Server 200X and the server will be a proprietary OS (based on UNIX I think) made by Nokia (telecommunication backbone equipment).

At this moment I expect the patterns /login:/ and /password:/, afterwards, with the correct system (currently I'm just testing with a system where I can also get the log's of the telnet server AND which is not critical if something happens) the patterns will change drastically.

Login works correct as the logs are implying but after that the script shuts down the connection / cuts it I think.

Currently I just want to connect to a telnet server on my local machine FROM my local machine, get to the cmd, send the "dir" command and get back the list it shows, maybe move a file or something for testing purpose.

Maybe there are also problems because theres a $-sign in my password but the telnet server says "login accepted: [ user ]".
Gonna be the best (In other words: Hope it will work :D )


rovf
Veteran

Dec 29, 2011, 5:30 AM

Post #6 of 14 (1721 views)
Re: [Trunkenstein] Net::Telnet help needed [In reply to] Can't Post

For the safe side, I would still try to use the login() method, just to verify (though, after your description, I guess that it would have the same problem). Don't forget to setup the current prompt!

How did you verify, that the server shuts down the connection, instead of executing the 'dir' command? You didn't show any logs, and your code doesn't seem to do much error handling. Do you get any error message on stderr? What does your input/output logs *exactly* say?

As for timeout, did you notice the following sentence in the docs?


Quote
Timing-out while making a connection is disabled for machines that don't support the alarm() function. Most notably these include MS-Windows machines.


Then, if you talk to a Windows Server, maybe it is necessary to set binmode(1); I have never used Net::Telnet on Windows, so I'm unsure about this effect.

Finally, make sure that you read the short chapter

Connecting to a Remote MS-Windows Machine

in the Net::Telnet docs.


Trunkenstein
Novice

Dec 30, 2011, 12:59 AM

Post #7 of 14 (1711 views)
Re: [rovf] Net::Telnet help needed [In reply to] Can't Post

As I'm very new to programming (mostly did this in school without doing anything like a "real" program) I don't know exactly how to lookup "stderr", but I will try to figure out how to do it.

It's not exactly a original Microsoft Telnet Server I'm using, its a third party telnet server which I installed on my client for testing purposes.

For the logs, here they are, I just changed username and password as I'm using my company computer login:

in_log.txt

Quote


 =================================================================
KpyM Telnet/SSH Server - fully functional unregistered version.
Order registration key at http://www.kpym.com/
The registered version does not display this notice.
=================================================================

 waiting... 54321Press any key to continue...================================================================ KpyM TELNET/SSH Server v1.19c  http://www.kpym.com/  Copyright (c) 2002-2011, Kroum Grigorov  All rights reserved.  ================================================================ 
login:user
password:


out_log.txt

Quote

user
password
dir


Telnet Server Log

Quote
1636 : 3604 2011-12-29 13:20:40 938 : 0: KTS connected to 10.5.10.47:2902

3972 : 1572 2011-12-29 13:20:41 141 : 0: session.exe started

3972 : 1572 2011-12-29 13:20:41 219 : 0: connected to 10.5.10.47:2902

3972 : 3732 2011-12-29 13:20:49 829 : 0: login accepted: [ user ]

3972 : 1572 2011-12-29 13:20:52 782 : 0: disconnecting

3972 : 1572 2011-12-29 13:20:52 782 : 0: shutdown session

3972 : 2252 2011-12-29 13:20:52 782 : 0: session.exe end

3212 : 2528 2011-12-29 13:20:53 16 : 0: disconnected

Gonna be the best (In other words: Hope it will work :D )


rovf
Veteran

Dec 30, 2011, 1:30 AM

Post #8 of 14 (1709 views)
Re: [Trunkenstein] Net::Telnet help needed [In reply to] Can't Post

I guess you already tried the other suggestions I sent? (binmode, sleep, etc.)?

Do you get the same effect if you send, for example, the command

cmd /c dir

instead of just

dir

?

If all of this is of no help, I suggest that you repost your question (together with your findings so far) at the kpym forum. Maybe it is an issue related to this particular Telnet server.


Trunkenstein
Novice

Dec 30, 2011, 2:19 AM

Post #9 of 14 (1708 views)
Re: [rovf] Net::Telnet help needed [In reply to] Can't Post

If I set binmode to "1" the program won't send the return key, after a command, anymore...
I tried

Code
$telnet->print(\n)

and

Code
print '\n'

, both doesn't work so I can't get to the

Code
$telnet->waitfor('/password:/')


if I try "cmd /c dir" I also get a timeout, it appears to me, that after senden the password, the console didn't change into "directory mode"

I think at first I will try some other telnet servers at first.
Gonna be the best (In other words: Hope it will work :D )


rovf
Veteran

Dec 30, 2011, 3:29 AM

Post #10 of 14 (1705 views)
Re: [Trunkenstein] Net::Telnet help needed [In reply to] Can't Post


Quote
I think at first I will try some other telnet servers at first.


I would, in addition, post a message at kpym. After all, your setup is not so unusual, so maybe someone stumbled over it. It could, for instance, also be related to the configuration of the telnet server.


Trunkenstein
Novice

Jan 3, 2012, 8:02 AM

Post #11 of 14 (1688 views)
Re: [rovf] Net::Telnet help needed [In reply to] Can't Post

Hello again, I'm now trying in our live network, and I just stumbled over a new problem.

I need to catch what the console is giving back to define to what type of device it is connecting.

The first phrase is: "ENTER USERNAME < "

My only idea till now was to extract the info trough the $telnet->get; command which also works but gets me alot of "spaces" before my phrase.

Is there an other possibility to get only the last line which is presented in the console?


Code
#! /usr/bin/perl 
use strict;
use warnings;
use Net::Telnet;

$| = 0;

my $telnet = new Net::Telnet ( timeout=>20, Errmode=>'return', Dump_Log => 'C:\_PerlTest\MML_TEST.log');

my ($line, $host, $benutzer, $password, $devType, $x25, $directConnect);
$host = '10.XX.XX.XX';
$benutzer = 'XXXOP1';
$password = 'XXXANE';
$x25 = '';
$directConnect = "ENTER USERNAME < ";

$telnet->input_log('C:\_PerlTest\in_log.txt');
$telnet->output_log('C:\_PerlTest\out_log.txt');

$telnet->open($host);

$devType = $telnet->get;
if ($devType eq $directConnect) {
#$telnet->waitfor('/ENTER USERNAME < /');
$telnet->print($benutzer); # Doppelte Anfürhungszeichen = Text und Sonderzeichen {Return, del, etc}, Einfacher Anführungszeichen = nur Text;

$telnet->waitfor('/ENTER PASSWORD < /');
$telnet->cmd($password);

#$telnet->cmd('ZXXX:TRAXXX,XX_XXX;');
#$telnet->cmd('Z;');
$telnet->cmd('Z;');
}
else
{print ('Iwas funktioniert noch nicht richtig')};


I only get to my "else {print ....." command cause of many "spaces" or "breaks" (26 and in line 27 theres my phrase) I also see in the "in_log.txt"
Gonna be the best (In other words: Hope it will work :D )


rovf
Veteran

Jan 3, 2012, 8:10 AM

Post #12 of 14 (1686 views)
Re: [Trunkenstein] Net::Telnet help needed [In reply to] Can't Post

I would not test the reply with eq, but matching via a regular expression.

Alternatively, if you have a multiline response and know that the last line is exactly what you need, you can just extract the last line. Use the split function (perldoc -f split) to splityour multiline-string into an array of lines, and use index [-1] to get the last element of the array.

Hope that helps.


Trunkenstein
Novice

Jan 9, 2012, 8:22 AM

Post #13 of 14 (1645 views)
Re: [rovf] Net::Telnet help needed [In reply to] Can't Post

Yes, I get a multiline response, most times indeed.

Later on I need to get the whole response after my last command was send but now I just need to get the last line of the first response after connecting.

Connecting looks like this:


Quote


























ENTER USERNAME <  SYSOP1

ENTER PASSWORD <  ******


What do you mean by "matching via a regular expression." ?
I just want to compare two strings via eq.

Sry if my questions are a bit, lets say, stupid, but I'm still new into the whole world of Perl :/
Gonna be the best (In other words: Hope it will work :D )


Trunkenstein
Novice

Jan 11, 2012, 9:05 AM

Post #14 of 14 (1613 views)
Re: [Trunkenstein] Net::Telnet help needed [In reply to] Can't Post

Thread can be closed, I found a solution to my problem
Gonna be the best (In other words: Hope it will work :D )

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives