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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I need a perl script to telnet on port 25 to an IP address

Status
Not open for further replies.

MHThomas

Technical User
Nov 25, 2002
26
GB
Sorry, This really is a begging letter.
I am very new to perl (though reasonably okay with shell scripting). I need a complete hand-holding script to telnet on port 25 to a specified IP address and measure the time before it issues the ehlo (and then quit) statements.
I'm basically asking for somebody to do my homework for me; I need to write this script to fix a production problem, but I'm way to new to perl, I'm spending way to much time reading books and messing about with basic array handling and sorting scripts.
I know nothing about OO programming, but know that I'll need to download the Net::Telnet module from cpan.

Cheers,

Mark T.
 
Once you download Net::Telnet and install it type :

perldoc Net::Telnet

there is example code there for you to use. Basically copy/paste!

 
Thanks Siberian, I'll give it a shot.

Cheers,

Mark T.
 
Some script I build once upon a time (few months ago) maybe you can use it:

# This telnet script asks for three variables from
# the user, namelijk the IP address, username
# and the password. It will then telnet into the
# device specified

# Features:
# 1. Input from User
# 2. Telnet to device
# 3. Input filters hardcoded in program
# 4. Implement filters and commit


# use strict;

use warnings;
use strict;
use Net::Telnet();


# Data inputted by the user of the script

my $ip;
my $username;
my $password;

print "Please input the IP adress:";
chomp ( $ip = <STDIN>);

print &quot;Please input the username:&quot;;
chomp ( $username = <STDIN>);

print &quot;Please input password:&quot;;
chomp ( $password = <STDIN>);


# Define and open the Telnet connection

my $con = new Net::Telnet();

# U should add reference to port 25!
# [Port => $port,]


$con->input_log(&quot;log.txt&quot;);
$con->open($ip) or die(&quot;error, please help me!&quot;);
$con->login($username, $password);

&PAUSE();

# Gettimenow() or something?
# Implement waiting for Ehlo
# GettimeWhenEhlo() or something?
# Substract times to get waiting time

#close connection

$con->close;


sub PAUSE {
print &quot;Press <enter> to continue...\n&quot;;
my $continue = <STDIN>;
}

Hope it helps some...

-- Raenius

&quot;Free will...is an illusion&quot;
 
Thanks Raenius,
Your script was very useful, but...
I still can't get a telnet session to start. If I try to telnet manually (ie, telnet 195.183.191.230 from the command line) I get connected immediately (sub-second), but when I try to run from a perl script I get the following timeout error timed-out waiting for command prompt at ./telnet.pl line 34 , even when I set Timeout to 1000! I think this is something to do with the Prompt statement. The prompt on our machines is a &quot;#&quot;, so I assumed that the default /[\$%#>] $/ should have picked it up, but my understanding of regular expressions is only slightly greater than my understanding of perl.

Thanks for all your advice so far,

Mark T.
 
I've hit a brick wall again (but thanks for all the support).
My simplified test script (using Korn shell on AIX) looks like this;

#!/usr/bin/perl
use Net::Telnet ();
$t = new Net::Telnet (Timeout => 10,
Prompt => '/[\$%#>] $/');
$t->open(&quot;195.183.191.234&quot;);
$t->login(thomasm, hendr1x);

@lines = $t->cmd(&quot;who&quot;);
print @lines;

Which times out with the following;

root@smtpgw1[/sd]#./tnet2.pl
timed-out waiting for command prompt at ./tnet2.pl line 6

but just issuing the telnet command from the command lines completes instantly. As my earlier reply sugests, I thought this was a problem with the prompt statement, but now I'm not so sure. Any sugestions (BTW, I have just completed reading the Net::Telnet docs, but am non the wiser).

Cheers,

Mark T.
 
This should help, from the Net::Telnet docs:

Debugging

The typical usage bug causes a time-out error because
you've made incorrect assumptions about what the remote
side actually sends. The easiest way to reconcile what
the remote side sends with your expectations is to use
input_log() or dump_log().

dump_log() allows you to see the data being sent from the
remote side before any translation is done, while
input_log() shows you the results after translation. The
translation includes converting end of line characters,
removing and responding to TELNET protocol commands in the
data stream.
 
Thanks Siberian,
That helped a lot, the problem was my use of regular expressions, the input_log() showed me exactly where I was going wrong.

Cheers,

Mark T.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top