Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Perl Telnet

Status
Not open for further replies.

jr8rdt

IS-IT--Management
Feb 9, 2006
59
US
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?

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;
----------
 
@output instead of $output
but you should really use the cmd option instead of print and waitfor.. it will make your life a billion time easier.



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
That's not your real IP address, userid, and password I hope? [smile]

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
If it is click on the Inappropriate Post link so it can be deleted.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
stevexff - of course it's not real ip and passwd. anyway the box is on private network.

thanks travs69 - I use the cmd option now and works fine for linux box.

How can make it work for windows? in addition to login and password, windows telnet ask for "Mode"?

 
if it does anything other than login/password you have to do print/waitfor.
After a waitfor you can do cmd's again.
For mode you want to use "Simple".
I guess you could always say that "Mode: " is a prompt.. but that could cause problems.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
I am stuck. Help...

I am creating perl script for telneting windows

this is the output for regular cmd line telnet
This copy of the Ataman TCP Remote Logon Services is registered as licensed to:
xxyy America

Account Name: xxyy
Password:
Mode:

once login this is the prompt

[<XDCCVFGG-c:\]


this is my code


use Net::Telnet;
my $t = new Net::Telnet (Timeout => 7200);
$t->open("22.22.22.22");
$t->waitfor('/Account.Name:.*$/');
$t->print('xxyy');

$t->waitfor('/Password:.$/');
$t->print('xxyypassword');

$t->waitfor('/Mode:.*$/');
$t->print('a');

my @lines=$t->cmd("dir");
print @lines;

is there an easier way, using login but with Mode. Mode is the culprit
 
Here is code I have going to ataman telnet
Code:
$telnet = new Net::Telnet (Timeout => 30, errmode => "return");
	$telnet->open($server) or print "can't open $server\t";
	$telnet->waitfor('/:/');
	$telnet->print("$login");
	$telnet->waitfor('/:/');
	$telnet->print("$pass");
	$telnet->waitfor('/:/');
	$telnet->print("Simple");
	$telnet->waitfor('/>/');
	@data = $telnet->cmd("dir");	
	$telnet->close;

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
You can change that last waitfor from a > to a ] to match your prompt.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Also there are 2 dump logs that Net::Telnet will create a dump log and a input log. When trouble shooting how far you are getting and what you are sending to the server it is good to turn these on and look at the output.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
jr8rdt

My comment about security wasn't intended as a slight. It's just that sometimes people are under pressure to solve a problem and they just cut & paste their code and hit 'submit' without thinking...

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
travs69, thanks again for your help.
it works now. I can see the ouput from Input_Log.
However I still can't print the output to the screen.
afterI run the program it's just give me a prompt.
Please note that the program is on SUSE10.2 and the telnet target is on Winserver2003.

@$data = $telnet->cmd($get_something);
print @data;

I have looked at the Net::Telnet doc and can't find any ref about this. is there additional param I need to use?

in addition when I check the win box task manager. the "cmd" process doesn't die after perl telnet finish running. though I have

$telnet->buffer_empty;
$telnet->close;

 
@$data = $telnet->cmd($get_something);
should be
@data = $telnet->cmd($get_something);


I actually left off a line of my telnet script because I didn't know why it was there.
$telnet->cmd("exit");


apparently I was having the same problem (telnet's not exiting).

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top