The creation of the $t telnet object should include error handling just in-case it fails.
It's better to use lexical vars for the filehandles instead of the barewords
This is a minor style issue, but instead of:
I'd probably do this:
next unless $number =~ /^6\d+$/;
which has the effect of reducing the indentation by 1 level and ensures that your $number var actually holds an integer and not something like this:
63fg7.
I'd move
print "The number is:$number\n"; up a couple lines so that it's not in the middle of the telnet calls.
I'd add a few comments on exactly what the code is supposed to be doing and expected results.
I'd add a little vertical white space (i.e, blank lines) to improve readability.
#!/usr/bin/perl
# This is a script that will telnet into a telcom switch,
# query a subscriber's number (sn) and check if the sn has
# data service in the account.
use strict;
use warnings;
use Net::Telnet;
my $user="user";
my $pass="pass";
my $server="host";
my $t = Net::Telnet->new($server);
$t->waitfor('/Enter User Name/');
$t->print("$user");
$t->waitfor('/Enter Password/');
$t->print("$pass");
# Open LIST to read the subscriber numbers from list.txt.
# Open WRITE, a blank file to write in the subcriber numbers that have the data service.
open(LIST,'<', "list.txt")
or die "Can't open 'list.txt' $!";
open(WRITE,'>>', "data_numbers.txt")
or die "Can't open 'data_numbers.txt' $!";
while(my $number = <LIST>){
if($number =~ /^6|^2/){
print "The number is: $number\n";
$t->waitfor('/>/');
$t->print("qhlr $number");
$t->waitfor('/>/');
$t->print("");
# Capturing the output.
my ($output) = $t->waitfor('/>/');
print "The output is: $output\n";
# Output is striped of \n and converted into a single line.
$output =~ s/\n//g;
# Checking the single line if it contains CSO: or QOPSI:.
# If match is found, then the number is written into data_numbers.txt.
# If not found, while loop prints a spacer of --- then it goes to the next number.
if($output =~ /CSO:|QOSPI:/){
print "The number has Data Service.\n";
print WRITE $number;
} else{
print "The number does NOT have Data Service.\n";
}
print "-------------------------------------------\n";
}
}
close LIST;
close WRITE;
# After all numbers in the loop have been queried, the session is closed.
$t->print("quit all;logout");
print "The Session has Ended!\n";
$t->close;