Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Seekers of Perl Wisdom

( #479=superdoc: print w/ replies, xml ) Need Help??

If you have a question on how to do something in Perl, or you need a Perl solution to an actual real-life problem, or you're unsure why something you've tried just isn't working... then this section is the place to ask. Post a new question!

However, you might consider asking in the chatterbox first (if you're a registered user). The response time tends to be quicker, and if it turns out that the problem/solutions are too much for the cb to handle, the kind monks will be sure to direct you here.

User Questions
How to parse a file with emails and IP's and output a sorted list with a total count
2 direct replies — Read more / Contribute
by mitzyc
on Jul 28, 2016 at 18:39

    I really need help. I've been trying to code this myself for over 3 hours now, and nothing I do works.

    I have a vpopmail file like this:

    Jul 28 13:42:27 mail vpopmail[47985]: vchkpw-smtp: (PLAIN) login success joe@example.com:192.168.250.251
    Jul 28 13:42:28 mail vpopmail[47992]: vchkpw-smtp: (PLAIN) login success joe@example.com:192.168.5.23
    Jul 28 13:42:29 mail vpopmail[47994]: vchkpw-smtp: (PLAIN) login success sally@example.com:192.168.10.28
    Jul 28 13:42:27 mail vpopmail[47985]: vchkpw-smtp: (PLAIN) login success fred@example.com:192.168.8.8
    Jul 28 13:42:28 mail vpopmail[47992]: vchkpw-smtp: (PLAIN) login success joe@example.com:192.168.5.23
    Jul 28 13:42:29 mail vpopmail[47994]: vchkpw-smtp: (PLAIN) login success harry@example.com:192.168.10.5
    

    I'm trying to write a routine that reads in this file, sorts the list, and gives a count:

    joe@example.com
    => 192.168.250.251 (1)
    => 192.168.5.23 (2)
    joe total: 2 unique IP's
    
    sally@example.com
    => 192.168.10.28 (1)
    sally total: 1 unique IP's
    

    I know basic perl. But object oriented programming is something I'm trying to learn, but I just can't seem to get it.

    If I had a working example written like this.. I could understand it better, and then be able to finally get how it works when I see the code.

    The output format doesn't have to be exact.

    I'm hunting to see how many unique IP addresses a person is successfully logging in from and output the counts

    I tried playing with XML::Simple, and gave up when I got this:

    Can't call method "address" on unblessed reference

    #!/usr/bin/perl use XML::Simple; my $xml = new XML::Simple; my $sender = { 'address' => 'joe@example.com', 'ips' => ["12.52","13.53","14.54.55"], }; # add another IP to an existing user $sender->address->'joe@example.com' {; ->ips = "16.70.71"; };

    I know I'm screwing up syntax, and all the examples I've been trying to incorporate never seem to work. I'd have a longer code sample if I had it, but I kept erasing my code and trying again, and again, and again.

    This led me to Perl Monks. Please help if you can. :)

    - Mitzy

Crypt() usage
2 direct replies — Read more / Contribute
by fionbarr
on Jul 28, 2016 at 12:04
    Is the 'crypt' function identical on Windows and *nix platforms?
hashes & threads
4 direct replies — Read more / Contribute
by gravid
on Jul 28, 2016 at 09:02

    Hi,

    I have this code:

    my %hash; foreach my $dir (@dirs) { if (-e "$dir/syn.log") { proccess_1_file("$dir/syn.log"); } } sub proccess_1_file { my $file = $_[0]; open(my $fh, "$file") or die "cannot open $file $!"; while (<$fh>) { chomp; my $line =$_; if ($line =~ /^area=(.*)/) { $hash{"$dir"}{"area"} = $1; }

    I want to parallelise the files processing.<\p>

    something like having a thread for each $dir, while all thread update the $hash.

    How can I do that?

    Thx

    Guy

Trying to understand Perl :-)
4 direct replies — Read more / Contribute
by harangzsolt33
on Jul 28, 2016 at 01:13
    I am learning Perl, and I am trying to see what happens when I write some weird code. And I have some questions...
    ################################## sub AA{6} sub BB{{{3}}} sub L{AA+BB} # Now, here Perl thinks that I am # calling AA(+BB) so, instead of # adding 6+3, it simply returns 6 print "\nL=".L."\n"; print "\nL=".((AA) + BB) ."\n"; # Now I got it right. # Okay, I understand this. So far everything is clear. ################################## sub UNIQUE::print{print 'Hello World!'} UNIQUE->print(333); sub UNIQUE::print{ print "\nMuhahaha! @_ \n"; } UNIQUE->print(65.65.65.65); # Okay this is weird. # First of all, how does 65 become letter 'A' without using chr(65) ? # Secondly, Perl allows me to redefine my UNIQUE::print and now # this second definition overwrites the first one. # What happened to the first one??? # Why is my 'Hello World' never showing up at all? ################################## print "What on earth is this? >>> " . sub {5}; my $D = sub { print "What the Heck!"; }; # What am I doing here? # And why is this allowed in Perl? What's the point of # creating a noname sub? Okay, so if I wanted to, how # would I run my noname sub using the handle $D ? ################################## sub Red { sub LittleRed{'red'}; # sub within a sub LittleRed; # returns 'red' } print "\nPerl is getting confused here..." . Red; print "\n\n"; print LittleRed; # Wait. This is not supposed to work! # If I declare a sub within a sub, then the inner sub should # not be accessible from the outside. Is this right??? # I mean what's the point of declaring a sub within a sub # if it's the same as declaring it outside the sub? ################################### sub X{10} print "\nYES>".X; # This prints 10. Fine. sub Y{my$i=0;$i while($i++<10);}print"\nYES>".Y; # Now, why is this not printing 10?? # When the while loop ends, $i equals 10, so if I just # say "$i" then it should use that as a return value for # the sub. No? ########################### # Another question: Is there a way to # access previous return values? # Example: sub DDD { 0; 1; 2; 3; 4; 5; } print DDD; # Is it possible to do something like this # without using the = assignment operator?: # # sub DDD { # 4+4; # 5+(previous lines' result); # return previous line's result; # } # # So, DDD would return 13.
$_[OBJECT] 's example
1 direct reply — Read more / Contribute
by liuweichuan
on Jul 27, 2016 at 23:30
    I have do some example about POE; but about this point on $_OBJECT parameter, i can't know clear. i make a baozi.pm, contains a object:
    package baozi; sub new{ my $class = shift; my $self = bless {}, $class; } sub fun1{ my $self = shift; print "fun1......baozi \n" } 1
    then try the $_OBJECT in other pl,
    use POE; use baozi; POE::Session->create( inline_states => { _start => sub { $_[KERNEL]->yield('next1')}, next1 => sub { print "tick ... \n"; $_[KERNEL]->delay(next2 => 1); }, next2 => \&next2, next3 =>\&baozi, }, ); POE::Kernel->run(); sub next2{ print "tick 0 ... \n"; $_[KERNEL]->yield('next3'); } sub baozi{ my $self = $_[OBJECT]; print $self; $self->baozi::fun1(); }
    I think 'next2' call 'next3' handler, and run sub baozi.$self->baozi::fun1(); can call package baozi's fun1. but I know my thinking is wrong, plz tell me more right, and help me understand $_OBJECT, thank you.
Net::SSH::Perl push variable into remote file
2 direct replies — Read more / Contribute
by rtech0
on Jul 27, 2016 at 23:14
    Hi,

    I am trying to grab the contents of ~/username/.ssh/id_rsa.pub into a variable and copy it so a user on the remote machine in /home/remoteusername/.ssh/authorized_keys. I have the file already in a variable via:

    my $fnpubcert = "/home/localusername/.ssh/id_rsa.pub"; open my $fhpubcert, '<', $fnpubcert or die "error opening $fnpubcert $ +!"; my $mypubcert = do { local $/; <$fhpubcert> };

    If I try this is creates the files but empty (I am sure you know why). Please advise:

    my $cmd = "echo $mypubcert > /home/remoteusername/.ssh/authorized_keys +"; my ($stdout,$stderr,$exit) = $ssh->cmd("$cmd"); if ($stdout || $stderr) { print "STDOUT: $stdout STDERROR: $stderr\n"; + } my $cmd = 'chown remoteusername.remoteusername /home/remoteusername/.s +sh/authorized_keys; chmod 600 /home/remoteusername/.ssh/authorized_ke +ys'; my ($stdout,$stderr,$exit) = $ssh->cmd("$cmd"); if ($stdout || $stderr) { print "STDOUT: $stdout STDERROR: $stderr\n"; + }

    ps I thought it might be this but doesn't work:

    my $cmd = "cat > /home/vmsys/.ssh/authorized_keys"; print $mypubcert | my ($stdout,$stderr,$exit) = $ssh->cmd("$cmd");

    Thank y'all in advance!

No tools? Use Perl?!
2 direct replies — Read more / Contribute
by Boyd.Ako
on Jul 27, 2016 at 22:20

    So, I have an "isolated system" (not internet connected) under "configuration management" with perl on it. I have an XML file that I'd like parse the extract and format specific information from it.

    "Normally" I'd install some XML perl module. But being "isolated" I can't download it. And even if I was able to "configuration management" says no new software without a shit ton of red tape just for a few little packages. Scripts that an admin writes in an exception.

    The XML file is a Nessus scanner results file. The section that I want to extract are between <RemoteHost> and </RemoteHost> for several servers in the file. For each RemoteHost section I'd like to only pull sections like the hostname, CVEs (list), and synopsis (multi line).

    I've tried using the flip-flop operator but it just gives me everything. If there's a way to pick and choose what parts I want; I don't know how to do it.

    Any suggestions?

    #! /usr/bin/perl use strict; use warnings; use FileHandle; my $file; if (-e $ARGV[0]){ $file = $ARGV[0]; hostreport(); } else { die; } sub hostreport { open(my $fh, "<file"); while (<$fh>){ print if (/^\<ReportHost/../^\<\ReportHost>/); } } print $file;
How do I make a UDP request to a torrent tracker?
2 direct replies — Read more / Contribute
by Cody Fendant
on Jul 27, 2016 at 17:21

    This page contains the details of a well-known torrent tracker:

    http://coppersurfer.tk/

    Which advises us to use UDP and access udp://tracker.coppersurfer.tk:6969/announce -- how would I do that with Perl?

    This node shows how to send something to a UDP host (no path is given) but I want to do something more like a HTTP get() request and get back a stream of data.

    How do I do that?

    question based on a misunderstanding

Simple comparison of 2 files
5 direct replies — Read more / Contribute
by Q.and
on Jul 27, 2016 at 14:47

    I know this is an elementary problem, so if this is a repeated question, please kindly flag, but I have yet to find something that works in the way I'm thinking about the problem. If the code seems overcomplicated for the task at hand, it's in part because the real script and files are more complex and because I'm new, so any suggested solutions that stay as close as possible to the code given here will be most appreciated.

    Say I have two files, FILE1 contains:
    A  1_1
    A  1_2
    B  1_3
    C  1_4

    and FILE2 is:
    A  2_1
    B  2_2

    I would like to compare both files and have it print:

    A from FILE1 with number 1_1 and A from FILE2 with number 2_1 match
    A from FILE1 with number 1_1 and B from FILE2 with number 2_2 DO NOT match
    A from FILE1 with number 1_2 and A from FILE2 with number 2_1 match
    A from FILE1 with number 1_2 and B from FILE2 with number 2_2 DO NOT match
    B from FILE1 with number 1_3 and A from FILE2 with number 2_1 DO NOT match
    B from FILE1 with number 1_3 and B from FILE2 with number 2_2 match
    C from FILE1 with number 1_4 and A from FILE2 with number 2_1 DO NOT match
    C from FILE1 with number 1_4 and B from FILE2 with number 2_2 DO NOT match

    The way I've been constructing the code so far however, obviously does not loop over the files in the way that I'm aiming for. That code is below, along with example output.

    #!perl open (FILE1, $ARGV[0]); open (FILE2, $ARGV[1]); while ($_ = <FILE1>) { chomp; @FILE1 = split; ($FILE1letter, $FILE1number) = @FILE1; @FILE2 = split(' ',<FILE2>); ($FILE2letter, $FILE2number) = @FILE2; # print "$FILE1letter from FILE1 with number $FILE1number and $FILE +2letter from FILE2 with $FILE2number match\n"; #prints the same as below if ($FILE1letter == $FILE2letter) { print "$FILE1letter from FILE1 with number $FILE1number and $F +ILE2letter from FILE2 with number $FILE2number match\n"; } else { print "$FILE1letter from FILE1 with number $FILE1number and $F +ILE2letter from FILE2 with number $FILE2number DO NOT match\n"; }

    Output from above code:
    A from FILE1 with number 1_1 and A from FILE2 with number 2_1 match
    B from FILE1 with number 1_2 and C from FILE2 with number 2_2 match
    C from FILE1 with number 1_3 and from FILE2 with number match

    I would appreciate any direction, brief explanation of what about my current code is not feasible for producing my desired output, and/or suggestions for better constructing the script. Thanks in advance.

    Clarification: The goal of the real script is really not to just print matches and mismatches, but to do many things within each loop. However, I realized that my if statement was not evaluating correctly, as it was printing even when there was no match and I think the larger problem is the looping structure of the entire code. In the real script, I would like it to evaluate lines from the two files ONLY when $FILE1letter is equal to $FILE2letter, but have it simplified to just a difference detection problem above in order to try to help myself learn how to solve smaller problems within larger ones.

Revising Input Method
3 direct replies — Read more / Contribute
by PinePerl
on Jul 27, 2016 at 13:15

    The following code is modified from the Perl Cookbook and is just a demonstration of what I intend, not the actual code being implemented...

    use Term::ReadLine; $term = Term::ReadLine->new("APP DESCRIPTION"); $OUT = $term->OUT || *STDOUT; $term->addhistory($fake_line); $line = $term->readline(PROMPT); print $line

    I am trying to make improvements to a program created in early 2000's. The built-in terminal interface works similar to the recipe above, yet the interface prevents the user from revising input once the terminal text wraps.

    PROMPTThis is a test. This is being written from a terminal. As I writ +e, I get closer to the edge. The prompt continues on the line below w +ithout me hitting the return key. When I press up with an arrow key, +I get these characters ^[[A^[[A

    Could someone please advise me on how to do either of the two: 1) allow the word wrap and modify all revised text similar to emacs, or 2) get rid of the word wrap and in turn allow revision of the paragraph before submitting it as input.

    If you are aware of a different tool that provides these features, please let me know as I'm unaware of all available Perl modules and have been learning so much this past month.


Add your question
Title:
Your question:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":


  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others drinking their drinks and smoking their pipes about the Monastery: (3)
    As of 2016-07-28 23:36 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      What is your favorite alternate name for a (specific) keyboard key?


















      Results (259 votes). Check out past polls.