Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

perl and omshell for dhcpd status

by mdd (Initiate)
on Feb 04, 2018 at 18:58 UTC ( [id://1208437]=perlquestion: print w/replies, xml ) Need Help??

mdd has asked for the wisdom of the Perl Monks concerning the following question:

I want to create a simple Perl script to determine and display the status of an ISC DHCP failover pair using omapi and omshell. What I have so far:
#!/usr/bin/perl use strict; open(OMSHELL, "|/bin/omshell") || die("Could not open omshell\n"); print OMSHELL "server localhost\n"; print OMSHELL "port 6902\n"; print OMSHELL "key omapi_key fakekey==\n"; print OMSHELL "connect\n"; print OMSHELL "new failover-state\n"; print OMSHELL "set name = \"netreg-failover\"\n"; print OMSHELL "open\n"; close(OMSHELL) || die("Could not close omshell\n");

This works in that it prints a list of dhcpd status messages once it hits the open command. However, instead of display the output on screen, I want to parse it, capture a few lines and do some processing. I don't know how to both open omshell to enter commands and read the output instead of displaying it. I hope that makes sense. I tried searching for the correct way to do it but couldn't wrap my head around the examples I've looked at.

-Mike

Replies are listed 'Best First'.
Re: perl and omshell for dhcpd status -- IPC
by Discipulus (Canon) on Feb 04, 2018 at 20:10 UTC
    Hello mdd and welcome to the monastery and to the wonderful world of Perl!

    If you want to read and write from/to external program you need some IPC (see perlipc and notably this part) as offered by IPC::Open2 IPC::Open3 ( both CORE modules and Open3 to also have STDERR but pay attention to different order in respect to Open2 flavour) or some CPAN module as IPC::Run IPC::Run3

    There are others but the above are more often used, i think. See also portability / system / elegance and Re: open2() in Windows

    L*

    update ah! bravo NetWallah below, i was trying to remember the name of that module with no luck. See also Capture::Tiny

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re: perl and omshell for dhcpd status
by NetWallah (Canon) on Feb 04, 2018 at 21:32 UTC
    See also IPC::Capture which was designed to do this stuff in a simple, portable manner.

                    Is a computer language with goto's totally Wirth-less?

Re: perl and omshell for dhcpd status
by hippo (Bishop) on Feb 05, 2018 at 10:09 UTC
    I tried searching for the correct way to do it but couldn't wrap my head around the examples I've looked at.

    You don't say which examples those are so it's impossible for someone else to explain them to you. It also means that we don't know whether or not you have read the relevant FAQ: How can I open a pipe both to and from a command? which itself refers to the pertinent sections of perlipc among other sources of info.

    If you could refer to the precise examples which are confusing you then maybe someone here would be able explain them.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1208437]
Approved by marto
Front-paged by haukex
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (5)
As of 2024-04-19 13:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found