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: Intermediate:
Trying to connect using Net::Telnet

 



tprizler
Novice

Jul 23, 2008, 5:20 AM

Post #1 of 5 (893 views)
Trying to connect using Net::Telnet Can't Post

Hi all,

I need help with this script.

I am trying to connect to remote machines using telnet.

I tried it in 2 diffrent ways, but none of them worked.

First try:


Code
 use Net::Telnetmy $telnet = new Net::Telnet ( Timeout=>10,Port => 2034,Errmode=>'die'); 
$telnet->open('10.56.49.98');
print "1\n";
$telnet->waitfor('/login: $/i');
print "2\n";

$telnet->print('root');
print "3\n";
$telnet->waitfor('/password: $/i');
print "4\n";
$telnet->print('admin');
$telnet->waitfor('/\# $/i');





Second try :


Code
 my $telnet = new Net::Telnet ( Timeout=>10,Errmode=>'die',Port => 2034 ,Prompt => '/login: $/i'); 
$telnet->open('10.56.49.98');
$telnet->login('root', 'admin');
print $telnet->cmd('who')



The script on both examples fails at the step of the login(), and i don't know why..

The machine login prompt looks like this :






|-----------------------------------------------------------------|
| This system is for the use of authorized users only. |
| Individuals using this computer system without authority, or in |
| excess of their authority, are subject to having all of their |
| activities on this system monitored and recorded by system |
| personnel. |
| |
| In the course of monitoring individuals improperly using this |
| system, or in the course of system maintenance, the activities |
| of authorized users may also be monitored. |
| |
| Anyone using this system expressly consents to such monitoring |
| and is advised that if such monitoring reveals possible |
| evidence of criminal activity, system personnel may provide the |
| evidence of such monitoring to law enforcement officials. |
|-----------------------------------------------------------------|

sh-nv210-1773 console login:








What i am doing wrong???



Thanks!!!!!


travs69
Novice

Jul 23, 2008, 9:51 AM

Post #2 of 5 (883 views)
Re: [tprizler] Trying to connect using Net::Telnet [In reply to] Can't Post

Look up the input log and the dump log. After you turn those on you can read the files and see what's up.


tprizler
Novice

Jul 30, 2008, 4:18 AM

Post #3 of 5 (844 views)
Re: [travs69] Trying to connect using Net::Telnet [In reply to] Can't Post

Hi ,



First thanks for the tip.

Second sorry for the delay!

Third!

This is what i did, i defined 2 files for debugging , executed the script agian and still i am stuck!


Code
   

my $Dump = "/Dump.txt";
my $Input = "/Input.txt";

my $telnet = new Net::Telnet ( Timeout=>10,Port => 2034 ,
Dump_Log => $Dump,
Input_log => $Input,
Prompt => '/login: $/i');
$telnet->open('10.56.49.98');
$telnet->waitfor('/login:.*$/');




When i am trying to see what te logs file gave me, the Input.txt is empty.

And the Dump.txt give me something i can't read!




[root@Host /]# cat Dump.txt
< 0x00000: ff fb 01 ff fb 03 ff fd 18 ff fd 1f ....

> 0x00000: ff fd 01 ff fd 03 ff fc 18 ff fc 1f ....

< 0x00000: ff fe 18 .

< 0x00000: ff fe 1f .




Am I doing any thing wrong??( most changes that I do somthing wrong:) but what?? )



Thanks!!!


tprizler
Novice

Jul 30, 2008, 6:25 AM

Post #4 of 5 (836 views)
Re: [tprizler] Trying to connect using Net::Telnet [In reply to] Can't Post


In Reply To
Hi, Found the problem!

Code
   

my $Dump = "/Dump.txt";
my $Input = "/Input.txt";

my $telnet = new Net::Telnet ( Timeout=>10,Port => 2034 ,
Dump_Log => $Dump,
Input_log => $Input,
Prompt => '/login: $/i'); <--------------- This line was unnecessary.
$telnet->open('10.56.49.98');
$telnet->waitfor('/login:.*$/');


Second, i found out that in order to initialize a connection i must use the "Errmode=>""" The thing is that from some reason when I write Errmode=>'die' the connaction fails, and when I write it as "Errmode=>'return'" , the connecion works and I can axecute commands as i wish! Now I have the problem to distinguish between to kinds of prompts: 1. login: 2. [~]# What I worte is :

Code
   
my $telnet = new Net::Telnet ( Timeout=>10,
Port => 2034 ,
Errmode=>'return');
$telnet->open('10.56.49.98');

if ($telnet->waitfor('/login:.*$/')) {
$telnet->waitfor('/login:.*$/');
$telnet->waitfor('/authorized/i');
$telnet->print('root');
$telnet->waitfor('/password: $/i');
$telnet->print('admin');
$telnet->waitfor('/\# $/i');
my @lines = $telnet->cmd('who');
print @lines;
}

elsif ($telnet->waitfor('/\# $/i')) {
my @lines = $telnet->cmd('who');
print @lines;

}


From debugging it a little I dicovered that The If fails.. Is there a right way to do this distinction??? Thanks!!!!


(This post was edited by tprizler on Jul 30, 2008, 6:27 AM)


tprizler
Novice

Jul 31, 2008, 7:24 AM

Post #5 of 5 (812 views)
Re: [tprizler] Trying to connect using Net::Telnet [In reply to] Can't Post

Hi,

I solved this issue so if anyone is interstead so her eis the explanation:



First of all What i wrote before is not correct.

Errmode=>'return' didn't fixed my problem it just ignored it, so it's very important to use Errmode=>'die', just when Errmode define to die when there is an error you will know if your script worked or not.



Second,

In order to distinguish between to kind of prompts i used anouther option of NET::TELNET,

my ($prematch, $match) , watch the code.


Code
 my $matchlogin = '/login: $/i';  
my $matchprompt = '/# $/i';
my ($prematch, $match) = $telnet->waitfor(Match => $matchlogin,
Match => $matchprompt);if (defined $match) {
print $match;
} else {
print "failed\n";
exit 1;}





This is it.

 
 


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

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