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: Advanced:
Script to remove features of a subscriber line.

 



cuboidgraphix
User

Jan 6, 2009, 7:08 AM

Post #1 of 16 (5752 views)
Script to remove features of a subscriber line. Can't Post

Hi guys,
I'm back .... and as promised, this is one of my scripts. Which has been very useful to me.
What this script basically does is, log into a 'switch' and removes roaming features off a subscriber line. The subscriber line is located in the roamers.txt . This is very useful when you got thousands of subscriber lines that you need to remove a feature on. This can be modified to not only remove a roaming or other feature, but also to add a feature to a subscriber line.

It's not a very professional script, but it got the job done.
The script is as follows.


Code
#!/usr/bin/perl 
$user="USER";
$pass="PASS";
$server="##.##.##.##";

use Net::Telnet;
$t = new Net::Telnet;
$t->open("$server");
$t->waitfor('/Enter User Name/');
$t->print("$user");
$t->waitfor('/Enter Password/');
$t->print("$pass");
$t->waitfor('/>/');
$t->print("servord");

open(LIST,"roamers.txt");
while(my $number = <LIST>){
for($number =~ /^6/){
$t->waitfor('/>/');
$t->print("deo");
$t->waitfor('/>/');
$t->print("");
$t->waitfor('/>/');
$t->print("$number");
print "The number is:\n $number";
$t->waitfor('/>/');
$t->print("cdw");
$t->waitfor('/>/');
$t->print("arw");
$t->waitfor('/>/');
$t->print("\$");
$t->waitfor('/>/');
$t->print("Y");
$t->waitfor('/>/');
$t->print("");
$t->waitfor('/>/');

($output) = $t->waitfor('/>/');
open(OUT,">output.txt");
print "The output is:\n $output";
print OUT "$output";
close(OUT);

open(MYIN, "output.txt");
open(MYOUT,">temp.txt");
while(my $line = <MYIN>){
$line =~ s/\n//g;
print MYOUT $line;
}
close(MYIN);
close(MYOUT);

open(READ,"temp.txt");
while(my $line = <READ>){
if($line =~ /NNNNNNN|INVALID FOR THIS OFFICE/){
print "Reading:\n $line\n\n";
print "The number does not exist in the switch!\n";
open(WRITE,">>xnum.txt");
print WRITE $number;
close(WRITE);
$t->print("N");
}
if($line =~ /JOURNAL FILE RECORD ID/){
print "Reading:\n $line\n\n";
print "Successfully removed roaming feature!\n";
$t->print("");
}
if($line =~ /does not exist for subscriber/){
print "Reading:\n $line\n\n";
print "The number did not have the roaming feature!\n";
$t->print("N");
}
}
close(READ);
print "-------------------------------------------------------------\n";
}
}
close(LIST);

$t->waitfor('/>/');
$t->print("quit all;logout");
print "The Session has Ended!\n";



Maybe one of you guys... FishMonger or KevinR can clean it up or something.

Thanks for helping me before, guys.


FishMonger
Veteran / Moderator

Jan 6, 2009, 7:33 AM

Post #2 of 16 (5750 views)
Re: [cuboidgraphix] Script to remove features of a subscriber line. [In reply to] Can't Post

I'm not going to clean it up for you, but I will give you some advice/recommendations.

EVERY Perl script you write should include the warnings and strict pragmas, which means that you'll also need to declare your vars.

The use statements should come before assigning the global vars.

You should ALWAYS check the return code of an open call and take proper action if it fails. It's much better/preferred to use a lexical var for the filehandle instead of a bareword.

You have way too much I/O going on i.e., there is absolutely no reason open those files in the while loop. That data should be stored in memory (i.e. a var) not temp files.


Code
for($number =~ /^6/){

Why are you using a for loop?
I presume what you really want to use is an if block.

Code
if($number =~ /^6/){



shawnhcorey
Enthusiast


Jan 6, 2009, 7:33 AM

Post #3 of 16 (5749 views)
Re: [cuboidgraphix] Script to remove features of a subscriber line. [In reply to] Can't Post

Here's a template system http://www.magma.ca/~shawnhcorey/200812/1230047183.html

It does things like create a POD for the script. Create a new file and inset your code into it.

__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


cuboidgraphix
User

Jan 6, 2009, 7:52 AM

Post #4 of 16 (5747 views)
Re: [FishMonger] Script to remove features of a subscriber line. [In reply to] Can't Post

I'm using the for loop because the roamers.txt file has thousands for numbers.. each line has a number. and 6 is the start of each line. an example of numbers would be..
6701050
6701055
6701074 ... and so on.

I know the script needs the warnings and strict pragmas, I know that now... I was simply posting one of my scripts which I promised I would in my other thread where I needed help, where by you said..

"Don't be too concerned about that at this point; it takes time to learn. When I first started out helping on the Perl forums, my code was functional, but pretty poor quality."


My older thread was (FTP to multiple directories and finding specific files.).

Anyways.. I was just trying to give something back to the forum for all the help I've gotten. Maybe this script can be useful to someone with some modifications and good perl practices.


As for having way too much I/O going on... I couldn't think of any other way of doing it. What I needed was a way to capture the output and verify the output. And my IF conditions would take the next step depending on the Match.

I wanted to write it simpler, but couldn't and honestly, the script does work perfect so far. I ran it for 65,000 + subscriber lines and they all were successfully modified.

I know the script needs a lot of work ... cleaning it up and such.. but I just hope my contribution would help someone with a similar problem I had before I wrote this script.

Anyways.. thanks again for everything. As for cleaning it up.. I'll leave it to the poor soul who wants to use it. :)

God Bless!!!


FishMonger
Veteran / Moderator

Jan 7, 2009, 11:35 AM

Post #5 of 16 (5725 views)
Re: [cuboidgraphix] Script to remove features of a subscriber line. [In reply to] Can't Post


In Reply To
I know the script needs the warnings and strict pragmas, I know that now... I was simply posting one of my scripts which I promised I would in my other thread where I needed help, where by you said..

"Don't be too concerned about that at this point; it takes time to learn. When I first started out helping on the Perl forums, my code was functional, but pretty poor quality."


I probably should have expanded on that comment of mine that you quoted.

You should always strive to write the best quality code you can and when someone more experienced points out problems and better ways of doing something, you should welcome it and build on it.


In Reply To
As for cleaning it up.. I'll leave it to the poor soul who wants to use it

That's a very disapointing comment. Frown

Perpetuating/promoting code after it was pointed to have some major issues does a big disservice to the Perl community.


cuboidgraphix
User

Jan 8, 2009, 9:19 AM

Post #6 of 16 (5717 views)
Re: [FishMonger] Script to remove features of a subscriber line. [In reply to] Can't Post

Hi FishMonger,
I'm sorry to disappoint you, my intention was not to say that I didn't appreciate your comments. You are right. If I'm to contribute something to the community, I should contribute something worth contributing.

Well ... I'm actually working on a similar script, and I am practicing good Perl Standards ... but I still can't get around the I/O of the text files. That's honestly the only way I know how to do it. I have tried the approaches suggested, but I am stumped.

Let me give you a brief description of what my purpose is.

I want to log into the switch and run a query. That output on the screen I want capture into a text file (or some other method) and check the file to see if it has a certain string of text.

My approach involves the following:

Open a text file and print the output to it and save. Close it.
Open the text file again, remove the \n and make it into one single line and save.. close it.
Open the text file again to read and find a match of a string of text. After that I start with my IF statements. Then close it.

Honestly, I know it's not a really good way of doing it.. I've tried incorporating everything into one and it doesn't work for me.

If you could give me another approach I am all ears and would really try to go that route.

Anyways, I apologize to you and the Perl community for seeming like a ungrateful bastard.


cuboidgraphix
User

Jan 8, 2009, 10:56 AM

Post #7 of 16 (5712 views)
Re: [cuboidgraphix] Script to remove features of a subscriber line. [In reply to] Can't Post

This is what I have so far for the new 'similar' script. I tried incorporating memory as a variable.. but it doesn't work with the while loop.



Code
#!/usr/bin/perl 
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,"list.txt")
or die "Can't open text file!";
while(my $number = <LIST>){
if($number =~ /^6/){
$t->waitfor('/>/');
$t->print("qhlr $number");
print "The number is:", $number, "\n";
$t->waitfor('/>/');
$t->print("");
(my $output) = $t->waitfor('/>/');

open(MEMORY, '>', \my $var)
or die "Can't open memory file: $!";
print MEMORY $output;
print "The output is:", $output, "\n";
while(my $line = <MEMORY>){
$line =~ s/\n//g;
print MEMORY $line;
if($line =~ /CSO:|QOSPI:/){
print "The number has Data Service.\n";
open(WRITE,">>data_numbers.txt");
print WRITE $number;
close(WRITE);
$t->print("N");
} else{
print "The number does NOT have Data Service.\n";
}
}
close(MEMORY);

print "----------------------------\n";
}
}

$t->print("quit all;logout");
print "The Session has Ended!\n";
$t->close;


The Open(LIST) is a text file that has thousands of lines, each line is a phone number.

The Open(WRITE) is a text file where I want to save all that numbers that DO have the data service.

the script times out at line 29. What am I doing wrong here? Am I using too many operations in the Open(MEMORY)?

Please help.


(This post was edited by cuboidgraphix on Jan 8, 2009, 11:00 AM)


FishMonger
Veteran / Moderator

Jan 8, 2009, 11:32 AM

Post #8 of 16 (5706 views)
Re: [cuboidgraphix] Script to remove features of a subscriber line. [In reply to] Can't Post

I can't test this and it still needs a few more tweaks, but based on your working script, these adjustments should work.


Code
#!/usr/bin/perl  
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,'<', "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/){
$t->waitfor('/>/');
$t->print("qhlr $number");
print "The number is:$number\n";
$t->waitfor('/>/');
$t->print("");
my ($output) = $t->waitfor('/>/');
print "The output is:$output\n";
$output =~ s/\n//g;

if($output =~ /CSO:|QOSPI:/){
print "The number has Data Service.\n";
print WRITE $number;
$t->print("N");
} else{
print "The number does NOT have Data Service.\n";
}

print "----------------------------\n";
}
}

close LIST;
close WRITE;

$t->print("quit all;logout");
print "The Session has Ended!\n";
$t->close;



cuboidgraphix
User

Jan 8, 2009, 11:35 AM

Post #9 of 16 (5704 views)
Re: [FishMonger] Script to remove features of a subscriber line. [In reply to] Can't Post

Thanks FishMonger. I'll give your tweaks a try and report back.


Update:

WOW!!!! Purely Genius!!!!! Damn FishMonger.... you're really ... I mean reeeeeeeeeeeeeeeeaaaaaally good man.

I'm stumped ... is that what you meant when you said the output to a variable? I was reading and reading all yesterday and today.. and I saw writing into memory as a variable.. OH well.. I see how simple it was ... yet how complicated it all seemed to me at first.

Thanks again FishMonger!!!!

YOU DA MAN!!!!


(This post was edited by cuboidgraphix on Jan 8, 2009, 11:47 AM)


cuboidgraphix
User

Jan 8, 2009, 11:55 AM

Post #10 of 16 (5701 views)
Re: [FishMonger] Script to remove features of a subscriber line. [In reply to] Can't Post


In Reply To
I can't test this and it still needs a few more tweaks, but based on your working script, these adjustments should work.


What were the few more tweaks you had in mind Fish?


FishMonger
Veteran / Moderator

Jan 8, 2009, 12:36 PM

Post #11 of 16 (5698 views)
Re: [cuboidgraphix] Script to remove features of a subscriber line. [In reply to] Can't Post


In Reply To

In Reply To
I can't test this and it still needs a few more tweaks, but based on your working script, these adjustments should work.


What were the few more tweaks you had in mind Fish?


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:

Code
if($number =~ /^6/){


I'd probably do this:

Code
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.


cuboidgraphix
User

Jan 8, 2009, 1:07 PM

Post #12 of 16 (5696 views)
Re: [FishMonger] Script to remove features of a subscriber line. [In reply to] Can't Post


In Reply To
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:

Code
if($number =~ /^6/){


I'd probably do this:

Code
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.



I don't follow with the next unless.. you pointed out.. if you could show me here.. I'd appreciate it. Below I did the other stuff you advised on.


Code
#!/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;



(This post was edited by cuboidgraphix on Jan 8, 2009, 1:10 PM)


KevinR
Veteran


Jan 8, 2009, 1:21 PM

Post #13 of 16 (5693 views)
Re: [cuboidgraphix] Script to remove features of a subscriber line. [In reply to] Can't Post


In Reply To
WOW!!!! Purely Genius!!!!! Damn FishMonger.... you're really ... I mean reeeeeeeeeeeeeeeeaaaaaally good man.

Thanks again FishMonger!!!!

YOU DA MAN!!!!


I taught him everything he knows........ Angelic Cool
-------------------------------------------------


cuboidgraphix
User

Jan 8, 2009, 1:29 PM

Post #14 of 16 (5691 views)
Re: [FishMonger] Script to remove features of a subscriber line. [In reply to] Can't Post

One slight modification guys... I had to move the OPEN WRITE into the IF statement, because I noticed that the numbers weren't getting written into the data_numbers.txt.

I still don't know what you mean by 'next unless ...' FishMonger, but thanks for everything so far bro.


This is the current WORKING script:


Code
#!/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 in list.txt.

open(LIST, "list.txt")
or die "Can't open 'list.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";


# Ouput 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 it 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";

# Open WRITE, data_numbers.txt to write in the subcriber number that has the data service.

open(WRITE, ">>data_numbers.txt")
or die "Can't open 'data_numbers.txt' $!";
print WRITE $number;
close WRITE;
} else{
print "The number does NOT have Data Service.\n";
}

print "-------------------------------------------\n";
}
}

close LIST;

# 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;



(This post was edited by cuboidgraphix on Jan 8, 2009, 1:42 PM)


FishMonger
Veteran / Moderator

Jan 8, 2009, 1:56 PM

Post #15 of 16 (5681 views)
Re: [cuboidgraphix] Script to remove features of a subscriber line. [In reply to] Can't Post


Code
while( my $number = <LIST> ) { 

next unless $number =~ /^6\d+$/;
print "The number is: $number\n";

$t->waitfor('/>/');
$t->print("qhlr $number");
$t->waitfor('/>/');
$t->print("");

# Capturing the multi-liine output of the qhlr command
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;


# Check the $output if it contains CSO: or QOPSI:.
# If match is found, then output it to data_numbers.txt.
# If not found, output a status message.
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 a reord separator then go to the next number.
print "-------------------------------------------\n";
}



cuboidgraphix
User

Jan 8, 2009, 2:58 PM

Post #16 of 16 (5675 views)
Re: [FishMonger] Script to remove features of a subscriber line. [In reply to] Can't Post

cool.. I follow now.. instead of the whole if you just say next line unless the line starts with 6. Cool man.. Thanks for everything Fish.. I really appreciate 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