Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

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
Q: Cannot get the capture from connection with Net::Openssh?
1 direct reply — Read more / Contribute
by ryder
on May 05, 2015 at 03:57

    Heelo Perl Monks,

    I try to connect to a network appliance using Net::Openssh. Simple commands like ls /tmp or cat /etc/hosts are working.

    But I try to capture the output from a proprietary command, called dbedit. The appliance have a standard Openssh server installed, version OpenSSH_4.3p2, OpenSSL 0.9.8b 04 May 2006 The server cannot be updated. here my test programm:

    #!/usr/bin/perl use Net::OpenSSH; my $host = ""; my $user = "admin"; my $pass = "my_pw"; #$Net::OpenSSH::debug=-1; my $ssh = Net::OpenSSH->new(host=>$host, user=>$user, password=>$pass, master_opts => [-v, -o => "StrictHostKeyCh +ecking=no"], ); $ssh->error and die "Couldn't establish SSH connection: ". $ssh->error +; my $xxx = "source /opt/CPshrd-R77/tmp/ ; /opt/CPsuite-R7 +7/fw1/bin/dbedit --h"; #my (@ls,$errput) = $ssh->capture2({tty => 1},"/opt/CPsuite-R77/fw1/ +bin/dbedit --h"); my (@ls,$errput) = $ssh->capture2({tty => 1},$xxx); $ssh->error and die "remote ls command failed: " . $ssh->error; print"LS:@ls\n"; print"ERR:$errput\n";

    As you can see by the code. I already tried some hacks to solve the problem. But any additional option I integrated into the code. Did not solve the problem. Interesting ist that the Debug-Output is showing the output from the command, but it seems this output did not get back through the tunnel to the ssh client. Please finde following the debug output from a connection establishment test:

    Is there anybody out there who can help me to capture the output from this command.

    ciao ryder

Problem installing Win32::ToolHelp with Strawbeery Perl Win64
1 direct reply — Read more / Contribute
by tangobaer
on May 05, 2015 at 03:52
    Hello community,
    this is my first question - please be kind.
    I try to install Win32::ToolHelp with x64 Strawberry Perl on Win7 64.
    When I use cpanm I get an dmake error: Build.log:
    cpanm (App::cpanminus) 1.7033 on perl 5.020002 built for MSWin32-x64-m +ulti-thread Work directory is C:\Users\Eurocom/.cpanm/work/1430810127.6240 You have make C:\Strawberry\c\bin\dmake.exe You have LWP 6.13 Falling back to Archive::Tar 2.04 Searching Win32::ToolHelp () on cpanmetadb ... --> Working on Win32::ToolHelp Fetching +tar.gz -> OK Unpacking Win32-ToolHelp-0.32.tar.gz Entering Win32-ToolHelp-0.32 Checking configure dependencies from META.yml Checking if you have ExtUtils::MakeMaker 6.58 ... Yes (7.04) Configuring Win32-ToolHelp-0.32 Running Makefile.PL Checking if your kit is complete... Looks good MakeMaker (v7.04) Warning (non-fatal): Target 'dynamic' depends on targets in skipped se +ction 'dynamic_lib' Warning (non-fatal): Target 'static' depends on targets in skipped sec +tion 'static_lib' Generating a dmake-style Makefile Writing Makefile for Win32::ToolHelp::Impl Writing MYMETA.yml and MYMETA.json Generating a dmake-style Makefile Writing Makefile for Win32::ToolHelp Writing MYMETA.yml and MYMETA.json -> OK Checking dependencies from MYMETA.json ... Checking if you have ExtUtils::MakeMaker 0 ... Yes (7.04) Building and testing Win32-ToolHelp-0.32 cp blib\lib\Win32\ AutoSplitting blib\lib\Win32\ (blib\lib\auto\Win32\ToolHelp +) cd Impl && dmake gcc -c -s -O2 -DWIN32 -DWIN64 -DCONSERVATIVE -DPERL_TEXTMODE_SCR +IPTS -DPERL_IMPLICIT_CONTEXT -DPERL_IMPLICIT_SYS -DUSE_PERLIO -fwrapv + -fno-strict-aliasing -mms-bitfields -s -O2 -DVERSION=\"\" +-DXS_VERSION=\"\" "-IC:\STRAWB~1\perl\lib\CORE" Impl.c ar /out:Impl.a Impl.o ar: invalid option -- / Usage: ar [emulation options] [-]{dmpqrstx}[abcDfilMNoPsSTuvV] [member +-name] [count] archive-file file... ar -M [<mri-script] commands: d - delete file(s) from the archive m[ab] - move file(s) in the archive p - print file(s) found in the archive q[f] - quick append file(s) to the archive r[ab][f][u] - replace existing or insert new file(s) into the archi +ve s - act as ranlib t - display contents of archive x[o] - extract file(s) from the archive command specific modifiers: [a] - put file(s) after [member-name] [b] - put file(s) before [member-name] (same as [i]) [D] - use zero for timestamps and uids/gids [U] - use actual timestamps and uids/gids (default) [N] - use instance [count] of name [f] - truncate inserted file names [P] - use full path names when matching [o] - preserve original dates [u] - only replace files that are newer than current archiv +e contents generic modifiers: [c] - do not warn if the library had to be created [s] - create an archive index (cf. ranlib) [S] - do not build a symbol table [T] - make a thin archive [v] - be verbose [V] - display the version number @<file> - read options from <file> --target=BFDNAME - specify the target object format as BFDNAME emulation options: No emulation specific options ar: supported targets: pe-x86-64 pei-x86-64 elf64-x86-64 elf64-l1om el +f64-k1om pe-i386 pei-i386 elf32-i386 elf64-little elf64-big elf32-lit +tle elf32-big srec symbolsrec verilog tekhex binary ihex dmake: Error code 129, while making 'Impl.a' dmake.exe: Error code 255, while making 'Impl\Impl.a' -> FAIL Installing Win32::ToolHelp failed. See C:\Users\Eurocom\.cpanm +\work\1430810127.6240\build.log for details. Retry with --force to fo +rce install it.
    Please could someone explain the problem for me.
    It is the first time I try to install a Win32 Module from CPAN with Strawberry Perl.
    Thanks for your help !!
What is this ? $^T
2 direct replies — Read more / Contribute
by Noloz
on May 05, 2015 at 03:37

    I recently started learning Perl. Looking at a script I have the following expression

    my $rand = $^T;

    and I do not know what $^T means or stands for. The code continues and it generates an R file open(Out1, >$rand".".R"); print Out1 "source('$Bin/varNumber2Rel.R')\n"; print Out1 "varNumber2Rel('".$options{'o'}.".varNumber'".", ".$options{'P0Cutoff'}.", ".$options{'p'}.")"; close(Out1); I hope you can help me! Thank you!
Print string next to pattern matching?
1 direct reply — Read more / Contribute
by sumathigokul
on May 05, 2015 at 02:29

    Hi all, i have a text file contains following lines...

    2 INT_NET Net : c_c 2 INT_NET Net : b_c 2 INT_NET Net : a_c
    Here, i want to extract string next to colon ':' i.e c_c, b_c and a_c and save them in another text file. How to print next to pattern matching?

    I also want your comments on my approach of this problem. I have two files, one is .vhd file (which contains nets names as a_c, b_c and c_c)and another is designer.log file which lists high fanout nets of the design. i have to extract these high fanout nets from designer.log file and accordingly i have to modify those nets in .vhd file. For this problem, i wrote perl script which extract high fanout nets with its corresponding statements also as shown above. From this statements i want to extract those nets name alone??

    Thank you all.

Perl script to find particular string and save those lines in another file?
3 direct replies — Read more / Contribute
by sumathigokul
on May 05, 2015 at 01:13

    Hi all, i want to search in a text file for '2' and copy the whole statement in another text file. Here is the code. It prints those matched statements and it also generated new.txt file, but the file is empty.

    use strict; my $find = "2"; open (FILE, "<high_fanout.txt") or die "could not open:$!"; my @line = <FILE>; for (@line) { if ($_ =~ /$find/) { print "$_\n"; print NEW "@line\n"; } } open (NEW, ">new.txt" ) or die "could not open:$!"; close (FILE); close (NEW);

    can anyone help me out from this mistake? Thank you all.

Can't cpanm install DBD::Pg
2 direct replies — Read more / Contribute
by Thai Heng
on May 05, 2015 at 00:57
    the error output file as follow:
    --> Working on DBD::Pg Fetching +.gz -> OK Unpacking DBD-Pg-3.5.1.tar.gz Entering DBD-Pg-3.5.1 Checking configure dependencies from META.yml Checking if you have DBI 1.614 ... Yes (1.632) Checking if you have version 0 ... Yes (0.9902) Configuring DBD-Pg-3.5.1 Running Makefile.PL Configuring DBD::Pg 3.5.1 You need to install postgresql-server-dev-X.Y for building a server-si +de extension or libpq-dev for building a client-side application. Enter a valid PostgreSQL postgres major version number Enter a valid PostgreSQL postgres minor version number Enter a valid PostgreSQL postgres patch version number You need to install postgresql-server-dev-X.Y for building a server-si +de extension or libpq-dev for building a client-side application. You need to install postgresql-server-dev-X.Y for building a server-si +de extension or libpq-dev for building a client-side application. Enter a valid PostgreSQL postgres bin dir You need to install postgresql-server-dev-X.Y for building a server-si +de extension or libpq-dev for building a client-side application. Enter a valid PostgreSQL postgres include dir No POSTGRES_HOME defined, cannot find automatically -> N/A -> FAIL Configure failed for DBD-Pg-3.5.1. See /home/hengaini/.cpanm/w +ork/1430800299.6291/build.log for details.

    I don't know how to define $POSTGRES_HOME.

    I install postgresql 9.4, and /usr/lib/postgresq have two directory 9.3 and 9.4.

    /usr/lib/postgresql/9.4/lib have some *.so files, such as

    I set $POSTGRES_HOME in ~/.bashrc as follow:

    # add by hengaini 20150113 POSTGRES_HOME='/usr/lib/postgresql/9.4'

    What is the problem ?

Screen Scraping from Juniper Switch using - Multiple Commands
No replies — Read more | Post response
by julio-johnson
on May 04, 2015 at 22:10

    Hello, I'm trying to do some telnet/ssh (start with telnet for simplicity) screen scraping from a Juniper switch (netconf/snmp aren't available) using the perl module.

    My goal is to be able to shove the output of a few commands into an array for each command. For example:

    "show version" goes to @show_version and "show interfaces" goes to @show_interfaces

    I've consulted

    I am failing utterly at this.

    I can login to the switch and do a basic command and then wait for the session to timeout.

    I've tried various combinations but I've so far failed at:

    1. Understanding the flow control logic of the Expect module. So to figure out I'm logged in, I need to look for a CLI prompt. But how do I tell the difference between the initial login vs. subsequent prompts (after the successful completion of a CLI command)?

    2. How do I piggy back several show commands together without logging out each time? I see that as part of the module's FAQ (How to expect on multiple spawned commands) where it mentions the -i parameter but I just don't get it.

    #!/usr/bin/perl use strict; use warnings; use Expect; my $cmd1 = "show version | no-more "; my $access_protocol = 'telnet'; my $router = ''; my $expect_log_file = 'expect-log-file.txt'; my $timeout = 15; my $username = 'username1'; my $password = 'asdfasdfasdf'; #Uncomment to hide stdout: #$Expect::Log_Stdout = 0; my $exp = Expect->spawn("$access_protocol $router") or die "Can't conn +ect to $router: $!\n"; $exp->expect ($timeout, ['(yes/no)',sub{my $fh = shift; $fh->send("yes\n"); exp_continue;}], ['(sername: )|(ogin: )$',sub{my $fh = shift; $fh->send("$username\n" +); exp_continue;}], ['(assword:)$',sub{my $fh = shift; $fh->send("$password\n"); exp_con +tinue;}], ['timeout',sub{&login_error();}], '-re', '> $', #wait for router prompt, then exit expect + ); $exp->log_file($expect_log_file); + $exp->expect ($timeout, ['',sub{my $fh = shift; $fh->send("$cmd1 \n");}] + ); $exp->expect ($timeout, ['More',sub{my $fh = shift; $fh->send(" "); exp_continue}], '-re', '[#>:]$', #wait for router prompt, then exit expect, + ); $exp->expect ($timeout, ['',sub{my $fh = shift; $fh->send("exit \n");}] + ); + sub login_error { print "Unable to login to router $router. Try to login manually wit +h SSH. \n"; exit; }
    perl ./ Trying Connected to ( Escape character is '^]'. EX3200-J (ttyp1) login: username1 Password: --- JUNOS 12.3R6.6 built 2014-03-13 06:58:47 UTC username1@EX3200-J> show version | no-more Hostname: EX3200-J Model: ex3200-24t JUNOS Base OS boot [12.3R6.6] JUNOS Base OS Software Suite [12.3R6.6] JUNOS Kernel Software Suite [12.3R6.6] JUNOS Crypto Software Suite [12.3R6.6] JUNOS Online Documentation [12.3R6.6] JUNOS Enterprise Software Suite [12.3R6.6] JUNOS Packet Forwarding Engine Enterprise Software Suite [12.3R6.6] JUNOS Routing Software Suite [12.3R6.6] JUNOS Web Management [12.3R6.6] JUNOS FIPS mode utilities [12.3R6.6] username1@EX3200-J>
Loading an array with file names
5 direct replies — Read more / Contribute
by insta.gator
on May 04, 2015 at 16:22

    PERL noob here.

    I need to load an array with file names produced from a wildcard search on a Windows box. I would like to pass the wildcard to the PERL script if possible. Not having luck. Here is what I have

    use Cwd; my $dir = getcwd; opendir DIR, $dir; my @prfiles = grep { $_ eq $ARGV[0] && $_ ne '.' && $_ ne '..' } readd +ir DIR; closedir DIR;

    I created using examples that I have found. I think that partially, my problem is with the wildcarding.

    This is what I would like to do: "perl testfile*.dat". Assuming I have testfile1,2,3,4.dat in the directory, it would load the array with these file names.

    Any help would be greatly appreciated!

Regular Expression Hiccup
6 direct replies — Read more / Contribute
by Mindsword
on May 04, 2015 at 15:27

    Hello, I have been working on a code for work and I found an odd glitch in my regular expression. I am looking through several blocks of texts for the Titles of some documents. The first title is "Screening Ligands by X-ray Crystallography." The text with that clearly has Screening Ligands by X-ray Crystallography in it. However, my regualr expression (below) does not pick it up. I have removed the 40 lines of code before this for brevities sake.

    #!/usr/bin/perl use warnings; use strict; use feature qw(say); ... ... if ($text =~ /Screening Ligands by X ray Crystallography/) { say "MATCH!"; } else { say "NOPE!"; } }
    However, if I do the code below, I do get a match.
    if ($text =~ /Screening Ligands by/) { say "MATCH!"; } else { say "NOPE!"; } }
    I have narrowed down the issue to the "-" I think. However, I can't search for the titles without the "-" as maybe 2/3rds of them have it. Any thoughts on how to get around this?

    EDIT: Based on what you all wrote, I began looking at a few of the others that were failing and have proven its not the "-" Its still not working correctly, but I at least know its not what I thought. I may even know what the issue is, but I'll need to run some tests. I think it may be an issue from the original titles not matching what's actually in the document.

Mechanize::Firefox can't get it to scroll
1 direct reply — Read more / Contribute
by WonderLemming
on May 04, 2015 at 13:46
    Hello, everyone! I'm having trouble getting WWW::Mechanize::Firefox to scroll down lines or pages in my current window.

    What I'm trying to do is navigate through some dialogues that generate a PDF in the window. Then I use xpath to pull the text from a few specific divs. The problem I'm running into is that it only seems to pre-load the first 2 pages of the PDF document and I need to scroll down to load the rest before xpath will snag all of the text I need.

    Here is my code:

    my $mech = WWW::Mechanize::Firefox->new( activate => 1, ); $mech->get(' +rket.aspx?id=2'); $mech->select("#marketID", '1740'); $mech->field("#startDate", '4/1/2015'); $mech->field("#endDate", $date); $mech->click({ selector => '#btnRun' }); my ($window,$type) = $mech->eval('window'); $window->scrollByLines(188);
    No scrolling occurs however. Any help would be greatly appreciated!



    UPDATE: Looks like scrollByLines does not work in the built in PDF viewer. However, I was able to iterate through the pages numbers using $mech->field("#pageNumber", $i) to produce the effect that I needed.

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!
  • 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
  • Outside of code tags, you may need to use entities for some characters:
            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 chanting in the Monastery: (7)
    As of 2015-05-05 09:18 GMT
    Find Nodes?
      Voting Booth?

      In my home, the TV remote control is ...

      Results (114 votes), past polls