Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

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
Perl binary file reading
3 direct replies — Read more / Contribute
by kepler
on May 02, 2016 at 15:22
    Hi, I'm having some troubles reading a binary file. That file has several records of 36 bytes, with about 4 fields with different lengths. First I've opened the file, set the binmode, and tryed to retrieve all the data in a variable. Then, I've tryed to isolate the fields with substr, and switched the \x00 characters into space... The problem is that the file is about 15 MB - and I only can read about 2Kb. So some caracheter - wich might be needed (some ascii values correspond to a integer number) - is messing things up....Any sugestion? Regards, Kepler
Telnet to multiple IPs and tftp individual config files
1 direct reply — Read more / Contribute
by maclaren_role
on May 02, 2016 at 11:39
    So I have a couple of hundred DSL routers that I need to telnet too and change to DHCP. I have a simple perl script that I already plagiarized to change the DNS on these routers but that was the same command on every box. What I want to do now is run a script that will telnet to each ip (same username/pass for all) and then run the tftp command to upload the new DHCP based config for each router. PLEASE NOTE - I'm very new to Perl and learning so I would very much appreciate your time and patience :-) The script I have refers to an ip.list txt file containing all the IPs to be connected to. Can I somehow add the name of the individual config files, or check for them against the ip and run the tftp command to upload it? Hope this makes sense. J.
Remove double bracket and singe quotes
4 direct replies — Read more / Contribute
by lobs
on May 02, 2016 at 10:50
    So I am trying to remove double brackets and singe quotes. Here is example text
    'C-3PO' or 'See-Threepio' is a humanoid robot character from the [[Sta +r Wars]] universe who appears in the original ''Star Wars'' films, th +e prequel trilogy and the sequel trilogy.
    What I have done is
    $doc =~ s/\[\[//g; $doc =~ s/\]\]//g; $doc =~ s/\'//g;
    Does not work at all. Please help.
find a substring using regex and then replace it
2 direct replies — Read more / Contribute
by swissknife
on May 02, 2016 at 10:15

    Dear Monks,

    I want to achieve following. I have a string called bge1 and I want to convert it to bge:1. to make it more specific, search the substring in the end of string and if it is number replace it with :number. I am trying following

    my $Interface = bge1; $Interface =~ s/[0-9]\$/:\$&/;

    but still result is bge1

    what is wrong with it?

Grab 4 numbers from url
2 direct replies — Read more / Contribute
by htmanning
on May 01, 2016 at 16:27
    Hi Monks, I need to grab four numbers from a long url like: /test-text-ad-1234.html The URL gets set to a variable, but then I need to grab the number. It is always on the end just before .html. How do I do this and set it to a variable?
What am I not understanding about $,
2 direct replies — Read more / Contribute
by Anonymous Monk
on May 01, 2016 at 01:55
    Easy way around this, but I want to understand why it is not working as I expect.
    #!/usr/bin/perl -w use strict; my @array = ( 'abc', 'def', 'ghi', 'jkl', 'mno', 'pqr', 'stu', 'vwx', 'yz ' ); print "@array[2,4]\n";
    ghi mno
    I want to get rid of the space, so I added:
    local $, = undef;
    But there is still a space in the output. Why? And is there some clever way of getting rid of the space without the obvious method of combining the two elements in a temporary variable and printing that?
Device::SerialPorDevice::SerialPort;t Problem
2 direct replies — Read more / Contribute
by kurta
on Apr 30, 2016 at 14:25

    This is a weird one. I'm reading data from a GPS module. The following code gives the expected response:

    #! /opt/local/bin/perl -w use strict; require 5.000; use lib "/opt/local/lib/perl5/"; use Data::Dump qw(dump); use Device::SerialPort; my $port = Device::SerialPort->new("/dev/tty.usbserial"); $port->are_match("\r\n"); $port->baudrate(9600); $port->databits(8); $port->parity("none"); $port->stopbits(1); while (1) { my $s = $port->lookfor(); next if $s eq ''; print $s,"\n"; if ($s=~/.*GPGGA.*/){ my @a=$s=~/(\d+\.\d{2}),(\d+\.\d+),(N|S),(\d+\.\d+),(E|W)/; print "==",dump(@a); } }
    But if I comment out the "print $s,"\n";" statement, I get nothing. Here's some sample output:

    ==("108746.88", "2048.37808", "N", "80113.44007", "W")$GPGSA,A,3,16,26,22,31,23,27,51,80,89,,,,1.78,1.82,1.36*83

    I suspect there's something strange going on with buffering, but don't understand why printing a variable should make a difference. Any perl-ly wisdom would be appreciated

    === ARGH! Just added a "/n" to the print "==",dump(@a); line and now it works. Why doesn't the buffer flush on print? Is Device::SerialPort resetting something?
Increment frequency of attempts based on IP and login details combination
3 direct replies — Read more / Contribute
by firepro20
on Apr 30, 2016 at 04:36

    I have the following logfile

    2016-04-29 15:56:48+0000 [cowrie.ssh.transport.HoneyPotSSHFactory] New + connection: ( [session: 3b8d22b +5] 2016-04-29 15:56:49+0000 [SSHService ssh-userauth on HoneyPotTransport +,28,] login attempt [root/root] succeeded 2016-04-29 16:11:14+0000 [cowrie.ssh.transport.HoneyPotSSHFactory] New + connection: ( [session: a6c0fac1] 2016-04-29 16:17:42+0000 [cowrie.ssh.transport.HoneyPotSSHFactory] New + connection: ( [session: d33e1566] 2016-04-29 19:07:10+0000 [cowrie.ssh.transport.HoneyPotSSHFactory] New + connection: ( [session: fafec37 +d] 2016-04-29 19:07:10+0000 [SSHService ssh-userauth on HoneyPotTransport +,0,] login attempt [root/root] succeeded 2016-04-29 19:42:58+0000 [cowrie.ssh.transport.HoneyPotSSHFactory] New + connection: ( [session: 539960a +3] 2016-04-29 19:42:58+0000 [SSHService ssh-userauth on HoneyPotTransport +,1,] login attempt [root/root] succeeded 2016-04-29 20:39:03+0000 [cowrie.ssh.transport.HoneyPotSSHFactory] New + connection: ( [session: b9f550d +f] 2016-04-29 20:39:03+0000 [SSHService ssh-userauth on HoneyPotTransport +,2,] login attempt [root/root] succeeded 2016-04-29 21:13:41+0000 [cowrie.ssh.transport.HoneyPotSSHFactory] New + connection: ( [session: e696835c] 2016-04-29 21:13:59+0000 [SSHService ssh-userauth on HoneyPotTransport +,3,] login attempt [user1/test123] failed 2016-04-29 21:14:10+0000 [SSHService ssh-userauth on HoneyPotTransport +,3,] login attempt [user1/test1234] failed 2016-04-29 21:14:13+0000 [SSHService ssh-userauth on HoneyPotTransport +,3,] login attempt [user1/test123] failed

    I want to increment a value called frequency whenever the IP is found and the same username and password are used to login. For that I want to output the SourcePort, the statusfailed/succedded and the total number of occurrences of the IP, and user/pass combination. So for instance, taking the last three entries, the result would be

    Port,Status,Occurrences 64400,failed,2 64400,failed,1

    This is the code I have so far

    $ip = ""; $port = ""; $usr = ""; $pass = ""; $status = ""; $frequency = 0; #Given this is a user/pass attempt honeypot logger, I will use + a wide character to reduce the possibility of stopping #the WEKA CSV loader from functioning by using smileyface as s +eperators. while(my $lines = <LOG2>){ if($lines =~ /New connection/){ ($ip, $port) = (split /[\[\]\s:()]+/, $lines)[7,8]; } if($lines =~ /login attempt/){#and the ip of the new c +onnection if($lines =~ /$ip/){ ($usr, $pass, $status) = (split /[\s:\[\]\/]+/, $lines +)[-3,-2,-1]; if($lines =~/$ip/ && $lines =~/$usr/ && $lines + =~ /$pass/){ $frequency++ } if($ip && $port && $usr && $pass && $status ne + ""){ print FILE2 join "$seperator",($port, +$status, $frequency, $end); print FILE2 "\n"; } } }

    This was also cross posted on Stack Overflow

Bioinformatics - Scoring DNA mutations
2 direct replies — Read more / Contribute
by allsop_5
on Apr 29, 2016 at 21:28

    Hi all, I'm doing an assignment that requires I shuffle a DNA sequence, then score the "mutations" according to a set of rules. I have come up with the below code and it is obviously not working (gives really odd results, like -2-4-400-200...etc) I'm hoping I just did something silly. My intent was to create a foreach loop that would use the subroutine I have that has a hash that creates numeric values based on evaluating the keys (hopefully that makes sense). Here is the code:

    my $score; foreach(0 .. length($string) - 1){ my $s = substr($string, $_, 1); my $m = substr($shuf_seq, $_, 1); $score = scoring($s, $m); $score += $score; print $score; } print "\nThis is the total mutation score: $score \n"; #sub to calculate the scoring of mutations #If purine --> purine: -1 #If pyrimidine --> pyrimidine: -1 #If purine --> pyrimidine (or vice versa): -2 #If no change: 0 sub scoring{ my ($a, $b) = sort @_; my %scores; $scores{'A'}{'G'} = -1; $scores{'A'}{'T'} = -2; $scores{'A'}{'C'} = -2; $scores{'G'}{'T'} = -2; $scores{'C'}{'G'} = -2; $scores{'C'}{'T'} = -1; $scores{'A'}{'A'} = +0; $scores{'T'}{'T'} = +0; $scores{'C'}{'C'} = +0; $scores{'G'}{'G'} = +0; return $scores{$a}{$b}; }
DBIx::Class Looping thru resultset
3 direct replies — Read more / Contribute
by phildeman
on Apr 29, 2016 at 16:04

    Hi All,

    Can variables be used in place of column names when looping through DBIx::Class array of objects.
    For example:

    my @db_colnames = ('lastname','firstname','middlename'); my @rows = $schema->resultset( 'myTable' )->search({}, {order_by => [qw +/ lastname firstname middlename /] } ); foreach my $row (@rows) { foreach my $col (@db_colnames) { print $row->$col ."\t"; } print "\n"; }

    No value printed. However, when I used the actual column name to retrieve the data, it worked fine. For example:

    . . print $row->lastname . "\t" . $row->firstname . "\t" . $row->middlenam +e . "\t"; print "\n"; . .

    Thanks for your help.

Add your question
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?

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

    How do I use this? | Other CB clients
    Other Users?
    Others taking refuge in the Monastery: (5)
    As of 2016-05-02 21:19 GMT
    Find Nodes?
      Voting Booth?