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

Perl+Telnet+Grep

Status
Not open for further replies.

treyhcnc

Programmer
Oct 27, 2006
6
0
0
US
Hello,

I have been trying to write a script, for days now, that will telnet into a text based antenna unit, enter numeric commands, and then grep the results into a log file or spit them out on the screen.

I'll explain how the antenna unit works. If you were going to access it, this is exactly whatyou would type:

1. telnet 10.5.0.14 (enter)
2. 3 (which automatically brings a prompt up for password, option 3 is for admin)
3. test (enter. test is the password)
4. 1 (the option for unit info)
5. 1 (the option to display the mac address)

Here is the script that I have. I know for sure thats it's logging into the antenna unit, and also hitting option 1, option1, because thats the screen I see on the unit when perl scripts run.

I am sure that the error is in the grep section.

#!/usr/bin/perl

use Net::Telnet::Cisco;

use Net::Telnet;

$router = "10.5.0.14";
my $session = Net::Telnet->new(Host => $router);
$session->print("3test");
@broadcast = $session->print("11");
open LOG, "> log.txt";
select LOG;
print @broadcast;
close LOG;
$session->close;

 
>> I am sure that the error is in the grep section.

Where is the "grep" section?

- Kevin, perl coder unexceptional!
 
Thats the problem, I don't know where to put grep!

The above script is also not logging. If it would just log, then I could grep it from there. Once the option 1, option 1 is chosen, it spits out a screen worth of info, incuding the 1st line to be the mac address.

Do i have the format correct for logging?

Also, where could I possibly put grep?
 
Hello,

Yes I had read a lot of the documentation.

What I ended up doing instead of using the login+cmd method, I used the print+waitfor method and now have a working script.


==================================
#!/usr/bin/perl

use Net::Telnet;

my ($host, $login, $options, $mac, $t);

$host = '10.5.0.22';
$login = 'password';
$options = '11';

use Net::Telnet ();
$t = new Net::Telnet;
$t->open($host);

## Login
$t->print($login);

## Type in options for unit status
$t->print($options);

## Read and print the first page of forecast.
#($mac) = $t->waitfor('/[ \t]+press return to continue/i');
($mac) = $t->waitfor('/Ethernet.*$/');
print $mac;

exit;
==================================

This echos back everything, including logging in and etc.

The desired results are displayed at the bottom and stops on the line with the word "Ethernet"

==================================
login results = extra, I cut out
choosing option 1 , 1 = extra, I cut out

Unit MAC Address : 00-**-**-**-**-**

Now all that is left to do is limit the results to only the Mac Address Line. I'll post my final script when I get grep working.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top