Jump to content


Photo
- - - - -

Perl help


  • Please log in to reply
8 replies to this topic

#1 dual

dual

    BinRev veteran

  • Agents of the Revolution
  • 1,196 posts
  • Gender:Male

Posted 03 October 2002 - 12:45 AM

This is mainly for nick...

I'm teaching myself perl and the book I'm using mentions the implicit variable, $_, but does not explain it? What is it?

Btw, here's my first script. It pulls all of the SSIDs out of cs_waps.txt, found at oldskoolphreak.com.

#!/usr/bin/perl

# ssid.pl - by dual_parallel
# Bonus perl script that pulls out SSIDs

system("/usr/bin/clear");

print "SSIDs\n";
print "-----\n";

$data = "/home/luser/cs_waps.txt";
open(WAPFILE, $data);

while ( ) {
if (/SSID/) {
print;
}
}

print "\nhttp://www.oldskoolphreak.com\n\n";

#2 nick84

nick84

    Member

  • Agents of the Revolution
  • 1,680 posts
  • Gender:Male

Posted 03 October 2002 - 03:14 AM

I’m only a relative beginner also, in Perl. Here’s some info I found copied/pasted about $_. (the links also includes a couple of examples of how you would use $_ in programs etc)

Substitution and translation

As well as identifying regular expressions Perl can make substitutions based on those matches. The way to do this is to use the s function which is designed to mimic the way substitution is done in the vi text editor. Once again the match operator is used, and once again if it is omitted then the substitution is assumed to take place with the $_ variable.
To replace an occurrence of london by London in the string $sentence we use the expression
$sentence =~ s/london/London/

and to do the same thing with the $_ variable just
s/london/London/

Notice that the two regular expressions (london and London) are surrounded by a total of three slashes. The result of this expression is the number of substitutions made, so it is either 0 (false) or 1 (true) in this case.
(Copied from one of the pages listed below)


http://ehlers.gps.ca...ubroutines.html
http://www.comp.leed...erl/sandtr.html


The program looks great, (well what there is of it), I assume you haven’t quite finished the book yet, as if you had it should tell you to close the file after you have opened it :) e.g.
$data_file="data.cgi";
open(DAT, $data_file) || die("Could not open file!");
@raw_data=<DAT>;
close(DAT); ### As you can see this line closes the file
( http://www.pageresou...irec/ptut14.htm )

I’ve found this site to be very good in the basics of Perl http://www.pageresou...irec/index2.htm

#3 nick84

nick84

    Member

  • Agents of the Revolution
  • 1,680 posts
  • Gender:Male

Posted 03 October 2002 - 03:16 AM

Also if you cant understand what I posted above / need any more help just ask :)

#4 dual

dual

    BinRev veteran

  • Agents of the Revolution
  • 1,196 posts
  • Gender:Male

Posted 03 October 2002 - 08:05 AM

Beautiful - thanks for the help.

# ssid.pl - by dual_parallel
# Bonus perl script that pulls out SSIDs
#!/usr/bin/perl

system("/usr/bin/clear");
print "SSIDs\n";
print "-----\n";

$data = "/home/luser/cs_waps.txt";
open(WAPFILE, $data) || die(">>> cannot open file");

while ( ) {
if (/SSID/) {
print;
}
}

close(WAPFILE);

#5 nick84

nick84

    Member

  • Agents of the Revolution
  • 1,680 posts
  • Gender:Male

Posted 03 October 2002 - 10:19 AM

I assume you got it running, its just when I tried it, the script hung on the while statement. (I was running it off Perl on windows and see you were using it on a Unix system or something - not that it should make a difference though)

Anyway, I just coded up my own version, (complete with comments on what each line does) that:

1. Gets the SSID’s from your file
2. Sorts them into alphabetical order (well actually ASCII code order)
3. Removes any duplicate id’s.
4. Writes the SSID only list back to a file.
5. Prints the list on screen

:P B)

So feel free to take a look through it any try and add the functions to your version if you like.

#!/usr/bin/perl

#######Set Variables#################
$data_file="cs_waps.txt"; #
$data_out_file="cs_waps_ssids.txt"; #
#####################################

system("/usr/bin/clear"); # Clear the screen on Unix
#system("cls"); # Clear the screen on Windows

###Read date from file
open(DAT, $data_file) || die("Error # Open file in read only mode
opening data file \"$data_file\"");
@raw_data=; # Read stuff into an array

close(DAT); # Close file

@sortedarray=sort(@raw_data); # Sort all the file contents simply by
# the ASCII value of the characters

foreach $line (@sortedarray) # For every line in the file
{
chop($line); # Take the carrige return off the end

($is_ssid,$ssid)=split(/\ /,$line); # Split the line into sections that are
# Separated by a space character

if ($is_ssid eq "SSID:") { # If the first part of the line is SSID:

$all_ssids =~ s/$ssid\n//; # Check to see if the list allready has
# the ssid (if so take it out)

$all_ssids= $all_ssids . $ssid . "\n"; # Add the ssid to the list in the string
# And separate them with a new line (\n)

} # End the SSID if statement

} # End the "foreach $line" loop

###Write the ssid's to a file
open(DAT,">$data_out_file") || die("Error # Open file in write only mode
opening data file \"$data_out_file\"");
print DAT "SSIDs\n"; # What we want in the file
print DAT "-----\n\n"; # What we want in the file
print DAT "$all_ssids"; # What we want in the file
close(DAT); # Close file

###Display the info on screen
print "\nSSIDs\n"; # Print out a fancy heading
print "-----\n\n"; # Underane our fancy heading

print "$all_ssids"; # Print out all the ssid's


:angry: I just checked through the code, and the board messed it up, as it thinks im using HTML tags, so if you want the proper code that will work, download it from here.

#6 StankDawg

StankDawg

    same old Dawg, no new tricks

  • Moderating Team
  • 8,073 posts
  • Country:
  • Gender:Male

Posted 03 October 2002 - 03:06 PM

Nick, to post code use the CODE code...errr... I mean, you know what I mean. It works just like a quote. Use the same form with CODE to start it and /CODE to end it. (They are inside straight brackets, like the quote). that prevent the code from being executed.

#7 nick84

nick84

    Member

  • Agents of the Revolution
  • 1,680 posts
  • Gender:Male

Posted 03 October 2002 - 05:08 PM

:blink: - Yes I know that, but I had a carefully planned reason not for using it.

You see my code had comments in it, which were all carefully aligned (as you can see in the code as above) however when I tried using the code tags it messed up the formatting (visually) so it all looked out of line (because the code tags don’t use a font that has the same width for all characters). Next I tried getting the pre tags outside the code tags but the board weren’t having none of it, so there was no way I could get both the comments aligned and the code not execute as HTML. (I also tried the file attachment option but it didnt work either for some reason)

If I had turned off HTML for the post that would have meant I couldn’t have used the pre tags to make all the comments line up - So I decided to get most of the code in the forum for people to see, with comments, then have a link to download the full version :)

#8 StankDawg

StankDawg

    same old Dawg, no new tricks

  • Moderating Team
  • 8,073 posts
  • Country:
  • Gender:Male

Posted 03 October 2002 - 06:54 PM

right on. Maybe I can add a FIXEDSYS font somehow.

#9 dual

dual

    BinRev veteran

  • Agents of the Revolution
  • 1,196 posts
  • Gender:Male

Posted 22 October 2002 - 12:11 AM

Thanks, nick. Looking at your code helped a lot.

#!/usr/bin/perl

# Clear the screen
system("/usr/bin/clear");

# Open file and read in data
$data = "/home/luser/cs_waps.txt";
open(WAPFILE, $data) || die "there is a problem";
chomp(@list=);
close(WAPFILE);

# Sort every line in the file
@sorted_list=sort(@list);

#Print header
print "SSIDs\n";
print "-----\n";

# Check for SSID string and print the split line
foreach $line(@sorted_list){
if ( $line =~ /SSID/ ){
($junk, $ssid) = split(/SSID: /, $line);
print "$ssid\n";
}
}




BinRev is hosted by the great people at Lunarpages!