Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
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
Use 'use' in foreach
5 direct replies — Read more / Contribute
by zidi
on Jul 19, 2017 at 06:41
    Hey, I am currently storing some non core perl modules in an array like this:
    my @NotInstalledModules = ( 'Foo::Bar', 'Bar::Foo', )
    And I am able to install these modules (install, if eval("use $_") not possible...). The only problem is that I can't 'use' them afterwards. I tried:
    foreach (@Dependencies){ use $_; }
    which is not working. Any help is appreciated Best regards
Running script only within a specific domain/network
3 direct replies — Read more / Contribute
by Anonymous Monk
on Jul 19, 2017 at 06:38

    Dear monks

    I want to allow a script to run only if it is physically inside a specific domain. Let's say that the script should work only if it is on a computer which is inside the network of the University of Wien (www.univie.ac.at). In order to do so, the script needs to read the network/domain in which the computer is (As you can see from the terms I am using, I am really a non-expert in all these IP & co. matters, but I am fairly okay in Perl programming ;). At the moment I can read the IP with the following:

    use warnings; use Net::Address::IP::Local; print my $address = Net::Address::IP::Local->public;

    Is the ip adress I am getting here stable enough to allow me to check if the computer is inside the university network over a longer period of time? If not, what is the best way to check it? Thanks

Issue with Net::SSH::Perl - can't establish connection
1 direct reply — Read more / Contribute
by dayton
on Jul 18, 2017 at 16:46
    Using Net::SSH::Perl and I can't establish a connection even though my key is accepted... what am I missing?
    .ssh/config:
    
    	Host remote.ssh.server
        	ControlMaster auto
        	ControlPath ~/.ssh/master-%r@%h:%p
        	User myusername
        	IdentityFile id_rsa
    

    The Code:

    $ssh = Net::SSH::Perl->new($host,debug=>true,options => ["ConnectT +imeout=5","StrictHostKeyChecking=no"]); $ssh_conf="$ENV{'HOME'}/.ssh/config"; $cfg = Net::SSH::Perl::Config->new($host); $cfg->read_config($ssh_conf); if (! $USERNAME){ $USER=$cfg->get('user'); } eval{$ssh->login($USER);}; if ($@){ print "Unable to connect to $host\n$@\n"; } eval{($out, $err, $exit)=$ssh->cmd($exec_cmd);};print "Unable to e +xecute: $exec_cmd\n" if $@; ### print "\n\nmade it this far\n\n"; ### if ($out){ print $out; } else { print "$err\n$exit"; }

    result:

    local.host: Reading configuration data /home/myusername/.ssh/config
    local.host: Reading configuration data /etc/ssh_config
    local.host: Connecting to remote.ssh.server, port 22.
    local.host: Remote version string: SSH-2.0-OpenSSH_7.2
    local.host: Remote protocol version 2.0, remote software version OpenSSH_7.2
    local.host: Net::SSH::Perl Version 2.09, protocol version 2.0.
    local.host: No compat match: OpenSSH_7.2.
    local.host: Connection established.
    local.host: Sent key-exchange init (KEXINIT), waiting for response.
    local.host: Using curve25519-sha256@libssh.org for key exchange
    local.host: Host key algorithm: ssh-ed25519
    local.host: Algorithms, c->s: chacha20-poly1305@openssh.com <implicit> none
    local.host: Algorithms, s->c: chacha20-poly1305@openssh.com <implicit> none
    local.host: Generating ephemeral key pair.
    local.host: Entering Curve 25519 Key Exchange.
    local.host: Sent client public key, waiting for reply.
    local.host: Received host key, type 'ssh-ed25519'.
    local.host: Host 'remote.ssh.server' is known and matches the host key.
    local.host: Verifying server signature.
    local.host: Send NEWKEYS.
    local.host: Waiting for NEWKEYS message.
    local.host: Enabling encryption/MAC/compression.
    local.host: Sending request for user-authentication service.
    local.host: SSH2_MSG_EXT_INFO received
    local.host: SSH Extension activated: server-sig-algs=rsa-sha2-256,rsa-sha2-512
    local.host: Service accepted: ssh-userauth.
    local.host: Trying empty user-authentication request.
    local.host: Authentication methods that can continue: publickey,gssapi-keyex,gssapi-with-mic,password.
    local.host: Next method to try is publickey.
    local.host: Publickey: testing agent key '/home/myusername/.ssh/id_rsa'
    local.host: Public key is accepted, signing data.
    local.host: Key fingerprint: SHA256:rScQOC1zgitfa/IiDb7CYwPkwK/k16ViRuAfzv4ITcQ=
    Unable to connect to remote.ssh.server
    Error reading response length from auth socket. at /usr/local/lib64/perl5/Net/SSH/Perl/Auth/PublicKey.pm line 104.
    
    local.host: channel 0: new client-session
    local.host: Requesting channel_open for channel 0.
    local.host: Entering interactive session.
    
    
    made it this far
    ]$
    

    just using "ssh -v":

    ]$ ssh -v remote.ssh.server "uptime"
    OpenSSH_7.5p1, OpenSSL 1.1.0f-fips  25 May 2017
    debug1: Reading configuration data /home/myusername/.ssh/config
    debug1: /home/myusername/.ssh/config line 8: Applying options for remote.ssh.server
    debug1: Reading configuration data /etc/ssh/ssh_config
    debug1: Reading configuration data /etc/ssh/ssh_config.d/05-redhat.conf
    debug1: Reading configuration data /etc/crypto-policies/back-ends/openssh.config
    debug1: /etc/ssh/ssh_config.d/05-redhat.conf line 8: Applying options for *
    debug1: auto-mux: Trying existing master
    debug1: Control socket "/home/myusername/.ssh/master-myusername@remote.ssh.server:22" does not exist
    debug1: Connecting to remote.ssh.server xxx.xxx.xxx.xxx port 22.
    debug1: Connection established.
    debug1: key_load_public: No such file or directory
    debug1: identity file id_rsa type -1
    debug1: key_load_public: No such file or directory
    debug1: identity file id_rsa-cert type -1
    debug1: Enabling compatibility mode for protocol 2.0
    debug1: Local version string SSH-2.0-OpenSSH_7.5
    debug1: Remote protocol version 2.0, remote software version OpenSSH_7.2
    debug1: match: OpenSSH_7.2 pat OpenSSH* compat 0x04000000
    debug1: Authenticating to remote.ssh.server:22 as 'myusername'
    debug1: SSH2_MSG_KEXINIT sent
    debug1: SSH2_MSG_KEXINIT received
    debug1: kex: algorithm: curve25519-sha256@libssh.org
    debug1: kex: host key algorithm: ecdsa-sha2-nistp256
    debug1: kex: server->client cipher: aes256-gcm@openssh.com MAC: <implicit> compression: none
    debug1: kex: client->server cipher: aes256-gcm@openssh.com MAC: <implicit> compression: none
    debug1: kex: curve25519-sha256@libssh.org need=32 dh_need=32
    debug1: kex: curve25519-sha256@libssh.org need=32 dh_need=32
    debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
    debug1: Server host key: ecdsa-sha2-nistp256 SHA256:b0EOVxSO46RlNAPAeDQg66CnCsKupSxhB1Kl5ihJ9Ok
    debug1: Host 'remote.ssh.server' is known and matches the ECDSA host key.
    debug1: Found key in /home/myusername/.ssh/known_hosts:3
    debug1: rekey after 4294967296 blocks
    debug1: SSH2_MSG_NEWKEYS sent
    debug1: expecting SSH2_MSG_NEWKEYS
    debug1: SSH2_MSG_NEWKEYS received
    debug1: rekey after 4294967296 blocks
    debug1: SSH2_MSG_EXT_INFO received
    debug1: kex_input_ext_info: server-sig-algs=<rsa-sha2-256,rsa-sha2-512>
    debug1: SSH2_MSG_SERVICE_ACCEPT received
    debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,password
    debug1: Next authentication method: gssapi-keyex
    debug1: No valid Key exchange context
    debug1: Next authentication method: gssapi-with-mic
    debug1: Unspecified GSS failure.  Minor code may provide more information
    No Kerberos credentials available (default cache: KEYRING:persistent:1000)
    
    debug1: Unspecified GSS failure.  Minor code may provide more information
    No Kerberos credentials available (default cache: KEYRING:persistent:1000)
    
    debug1: Next authentication method: publickey
    debug1: Offering RSA public key: /home/myusername/.ssh/id_rsa
    debug1: Server accepts key: pkalg rsa-sha2-512 blen 279
    debug1: Authentication succeeded (publickey).
    Authenticated to remote.ssh.server (xxx.xxx.xxx.xxx:22).
    debug1: setting up multiplex master socket
    debug1: channel 4: new /home/myusername/.ssh/master-myusername@remote.ssh.server:22
    debug1: channel 5: new client-session
    debug1: Entering interactive session.
    debug1: pledge: id
    debug1: client_input_global_request: rtype hostkeys-00@openssh.com want_reply 0
    debug1: Requesting X11 forwarding with authentication spoofing.
    debug1: Requesting authentication agent forwarding.
    debug1: Sending environment.
    debug1: Sending env LANG = en_US.utf8
    debug1: Sending env XMODIFIERS = @im=none
    debug1: Sending command: uptime
    debug1: client_input_channel_req: channel 5 rtype exit-status reply 0
    debug1: client_input_channel_req: channel 5 rtype eow@openssh.com reply 0
     15:23:01 up 50 days, 19:36,  1 user,  load average: 0.45, 0.25, 0.24
    debug1: channel 5: free: client-session, nchannels 6
    debug1: channel 0: free: port listener, nchannels 5
    debug1: channel 1: free: port listener, nchannels 4
    debug1: channel 2: free: port listener, nchannels 3
    debug1: channel 3: free: port listener, nchannels 2
    debug1: channel 4: free: /home/myusername/.ssh/master-myusername@remote.ssh.server:22, nchannels 1
    Transferred: sent 2744, received 2564 bytes, in 0.1 seconds
    Bytes per second: sent 27909.5, received 26078.7
    debug1: Exit status 0

Remove return on multiple lines between 2 characters
5 direct replies — Read more / Contribute
by AnewSmurf
on Jul 18, 2017 at 16:41
    Hi All, Here is a sample of variable
    $lines = "The kernel packages contain the Linux kernel, the core of any Linux
    operating system.
    
    Security fixes:
    
    * The maximum file offset handling for ext4 file systems could allow a
    local, unprivileged user to cause a denial of service. (CVE-2011-2695,
    Important)
    
    * IPv6 fragment identification value generation could allow a remote
    attacker to disrupt a target system's networking, preventing legitimate
    users from accessing its services. (CVE-2011-2699, Important)
    
    * A malicious CIFS (Common Internet File System) server could send a
    specially-crafted response to a directory read request that would result in
    a denial of service or privilege escalation on a system that has a CIFS
    share mounted. (CVE-2011-3191, Important)
    ";
    
    I would like to extract content between * and (CVE-.*) and removing return between line. For example:
    A malicious CIFS (Common Internet File System) server could send a specially-crafted response to a directory read request that would result in a denial of service or privilege escalation on a system that has a CIFS share mounted. (CVE-2011-3191, Important)
    
    I've the code to extract the text between * and (CVE..) when there are no \n.
search a string in a non-regular file
6 direct replies — Read more / Contribute
by ytjPerl
on Jul 18, 2017 at 15:41

    Hi guys, I am new babie in perl. I happened to come across an issue of finding a string in 'non-regular' log file. I know how to use like if(/*/) {code} to find a string "*" in .txt file, but I could not use this to find a string in the file generated by command 'tmboot -y'. The string actually is there and I am able to use notepad++ search function to find it. so the only reason I could think of is that the file is not the regular txt file. any input? thanks!

Making use of a hash of an array...
3 direct replies — Read more / Contribute
by Peter Keystrokes
on Jul 18, 2017 at 11:46

    ...which is populated through a loop and if statements

    So I have some data:
    >hsa_circ_0075116|chr5:175956288-175956388-|NM_014901|RNF44 FORWARD -4.6 12 .. 35 xxxxGTGTGTGGTCT GC TTCAGTGACTTCGAGG +CGCG GC AGCTGCTCCGAGTCC -5.5 11 .. 36 xxxxxGTGTGTGGTC TGC TTCAGTGACTTCGAGG +CGCG GCA GCTGCTCCGAGTCCT -7.8 10 .. 37 xxxxxxGTGTGTGGT CTGC TTCAGTGACTTCGAGG +CGCG GCAG CTGCTCCGAGTCCTC -4.3 9 .. 38 xxxxxxxGTGTGTGG TCTGC TTCAGTGACTTCGAGG +CGCG GCAGC TGCTCCGAGTCCTCC -4.6 31 .. 41 CAGTGACTTCGAGGC GCGG CAG CTG +C TCCGAGTCCTCCCCT -5.7 28 .. 44 CTTCAGTGACTTCGA GGCGCGG CAG CTG +CTCC GAGTCCTCCCCTGCA -5.1 20 .. 49 GTGGTCTGCTTCAGT GACTT CGAGGCGCGGCAGCTG +CTCC GAGTC CTCCCCTGCAACCAT -4.3 27 .. 56 GCTTCAGTGACTTCG AGGCG CGGCAGCTGCTCCGAG +TCCT CCCCT GCAACCATGAGTTCC -5.6 31 .. 58 CAGTGACTTCGAGGC GCGG CAGCTGCTCCGAGTCC +TCCC CTGC AACCATGAGTTCCAC -5.4 72 .. 82 GCAACCATGAGTTCC ACAC CAA GTG +T GTTGACAAGTGGTTG -7.7 71 .. 83 TGCAACCATGAGTTC CACAC CAA GTG +TG TTGACAAGTGGTTGA -4.2 70 .. 84 CTGCAACCATGAGTT CCACAC CAA GTG +TGT TGACAAGTGGTTGAA >hsa_circ_0014931|chr1:160293220-160293320-|NM_001098398|COPA FORWARD -5.5 11 .. 36 xxxxxGGTCACGATC GTG GAGTAAACTGGGCTGC +CTTC CAC CCCACTATGCCCCTT -4.5 22 .. 40 GATCGTGGAGTAAAC TGGGCTG CCTTC CA +CCCCA CTATGCCCCTTATTG -4.1 11 .. 41 xxxxxGGTCACGATC GTGGAG TAAACTGGGCTGCCTT +CCAC C-CCAC TATGCCCCTTATTGT

    And I want to create a hash which has the name of the sequence 'hsa_circ...' as the key and the hash itself will be an array, containing a series of 'starts' and 'ends' which are chosen after going through some conditional if statements.

    For example with respect to hsa_circ_0075116|chr5:175956288-175956388-|NM_014901|RNF44 FORWARD I want to capture this as the hash key. But then as I read the file in line by line I want to ask if statements regarding the start and end position values and then push them into the array of their respective hash.

    So my problem is twofold:

    (1) I want to be able to create a hash of arrays that is added to as the file is read

    (2) I want to be able to read in line by line asking if statements of whether the start value and end value is within a positional vicinity of 6 such that it is essentially 1 hairpin and in which case I push into my array the start and end values which have the greatest range.

    For example in:
    -4.6 <b>12 .. 35</b> xxxxGTGTGTGGTCT GC TTCAGTGAC +TTCGAGGCGCG GC AGCTGCTCCGAGTCC -5.5 <b>11 .. 36</b> xxxxxGTGTGTGGTC TGC TTCAGTGAC +TTCGAGGCGCG GCA GCTGCTCCGAGTCCT -7.8 <b>10 .. 37</b> xxxxxxGTGTGTGGT CTGC TTCAGTGAC +TTCGAGGCGCG GCAG CTGCTCCGAGTCCTC -4.3 <b>9 .. 38</b> xxxxxxxGTGTGTGG TCTGC TTCAGTGAC +TTCGAGGCGCG GCAGC TGCTCCGAGTCCTCC -4.6 31 .. 41 CAGTGACTTCGAGGC GCGG CAG CTG +C TCCGAGTCCTCCCCT
    The start positions and end positions surrounded by bold tags are essentially the same hairpin, so all except 9 and 38 will be ignored since it is this one that captures the hairpin best.

    I am struggling to programmatically articulate these if statements though. Essentially I am trying to read each line capturing the values and assessing whether the next line bears values which are $start-1 and $end+1 and if they are, then they become the $start and $end and this is repeated until -/+6 of the orginal $start and $end value is reached

    Trying to populate the hash of arrays:
    #!/usr/bin/perl use strict; use warnings; open my $hairpin_file, '<', "test.hairpin", or die $!; my %HoA_sequences; while (my $line = <$hairpin_file>){ if ($line =~ /^>hsa/){ $HoA_sequences{$line} = ## Do I provide the key for the hash as soon as I read in the line bea +ring the sequence name? ## And can I create a hash with a key but no hash? I'm guessing not. : +/ } }
    Trying to develop the if statements:
    if ($line =~ /$RE{num}{real}\s+($RE{num}{real})\s..+($RE{num}{real})+/ +){ my $start = $1 && my $end = $2; ## Once captured how do I continue to the next line to query if the ## value is - or + of the start and end values }
JSPL vs JavaScript ...and other questions
1 direct reply — Read more / Contribute
by ptizoom
on Jul 18, 2017 at 10:18

    A few decades ago, I wanted to perl-swig the javascripting for vague hording applications. but then failed miserably... We did not have all this help : < https://developer.mozilla.org/en-US/docs/Mozilla/Projects/SpiderMonkey/JSAPI_User_Guide > at hand !

    now I have time to mess with that. but then, trying the joys of JavaScript-1.16+mozjs185, I encounter too many bugs and nasties, and an ultimate frustration when rewarded by SIGVEC messages and showers of core dumps while objects instantiating.

    but then JSPL-1.08_1 prooved more docile and compliant to my deeds. I even started pinching some code from it, and stuffing if back to JavaScript. but hey...stop. tingle and come questions... Shouldn't JavaScript be more robust?

    somewhat both packages are old, and quietly abandoned on CPAN. unless ActiveState has commercial interests to keep it so, my guts tells me that JSPL is the package to back up.

    so, back to the monks, here are my questions:

    are they out there, new trendy marriages with SpiderMonkey and Perl ?

    if not, would it be worth evolving JSPL rather than JavaScript (sorry) ?

    or forget perl and try python (just teasing, like perl too much to do that)?

    thanks.
Breaking issue
2 direct replies — Read more / Contribute
by jayvillach
on Jul 18, 2017 at 08:47

    Hey Monks Hope you doing fine. My issue is, I have Barcode scanner, I have text to detect by the scanner. This project I am doing for tracking our library books when it is been lend to someone when they need. In order to track to whom I have lent it, I have made the program to scan Book ID. When we scan the book ID then a time loop starts, this time loop has been created so that if the person has multiple books then we scan the second, then again the time loop starts, then the third book, then when the time loop finish automatically it should update the details in our list. But it does not. It does update but we have to scan something after when the time loop finishes, I mean scan anything, any Barcode, then it happens. But I dont understand why it does not update automatically soon after I finish the flow and time loop ends. Any help would be appreciated. Thanks you in advance.

Removing empty elements from multi-dimensional array
4 direct replies — Read more / Contribute
by OldMonkRum
on Jul 18, 2017 at 08:28

    Hi Monks,
    I am using
    push(@array,['X1','X2','X3','X4' ]);
    Subsequently I am doing:
    push(@array,['',$key,'','']);
    Because X2 elements are coming from a certain hash and so on.
    push(@array,[$key,'','','']);
    Then I am using HTML::QuickTable
    When I render: $qt->render(\@array);
    The table has first column empty values till it hits an actual value. Any way, I can print only when its not empty i.e. not waste rows in the table?
    Thanks a lot!

HMAC Sha256 Help!
3 direct replies — Read more / Contribute
by ribble
on Jul 18, 2017 at 05:13

    Hello all. I am after some assistance regarding the creation of a basic authentication header.

    I am been asked to code against a restAPI that requires the creation of the authentication header using hmac sha256. I am running into issues getting the hash values to match against the restAPI yet when I check using an online hmac generator my values are correct

    This leads me to believe I am doing something wrong when using the app key as part of the hash

    The documentation states that the key is comprised of 32 two-character base-16 hexadecimal values. I guess this where my issue is

    I don't know how I should be handling the key other than just parsing it as a string.

    my simple code so far

    #set the strings $method="GET"; $appid="d6468df6c1e8419fb5ec50f62be9a28b"; $appkey="15c7a6e1a5bd73500db29dffd0e19b6c6228b044c64348a6f5d6d470c54fc +208"; $path="/xxxxx/xxxx/api/v1/users/xxxx/xxxxx"; #build the string to hash $step1="$method\\n $datestring\\n $appid\\n $path"; #hmac sha256hash the string $step2=hmac_sha256_hex($step1, $appkey);

    I am using hmac_sha256_hex as the app key string is in hex. I am clearly doing something wrong but no idea what?

    Do i need to perform some conversion of the app key before using it? I am lost!

    thanks for looking


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 all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others making s'mores by the fire in the courtyard of the Monastery: (4)
    As of 2017-07-22 20:12 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      I came, I saw, I ...
























      Results (341 votes). Check out past polls.