Loading ...
Sorry, an error occurred while loading the content.

Redirect stdout in a larger statement

Expand Messages
  • Martin McCormick
    I have a small perl script which I found as an example that calls the omshell, familiar to those who administer DHCP servers. You enter commands and get output
    Message 1 of 2 , Jun 7, 2013
    • 0 Attachment
      I have a small perl script which I found as an example that calls the
      omshell, familiar to those who administer DHCP servers. You enter
      commands and get output on stdout.

      I want to capture all that output in a list as in
      @omstrings = < theoutput >;
      but so far, the magic syntax eludes me because the call to omshell is
      part of a larger statement and attempts to use @omstrings = `omshell`,
      something like that just confuses the script. Here it is, slightly
      obfuscated to protect the crown jewels.

      #!/usr/bin/perl -w
      use strict;

      my @omstrings;
      my $mac = "aa:bb:cc:dd:ee:ff";

      my $ip = "192.168.1.54";

      my $name = "zzzzokstate.edu";

      #We need to start capturing output from the omshell.
      open (OMSHELL, "|omshell") || die ("Unable to open omshell\n");
      print OMSHELL "server 192.168.2.3\n";
      print OMSHELL "port 7911\n";
      print OMSHELL "key key_name \"IFiTOLDUIDHAVE2KILLU\"\n";
      print OMSHELL "connect\n";
      print OMSHELL "new host\n";
      print OMSHELL "set name = \"$name\"\n";
      print OMSHELL "set hardware-address = $mac\n";
      print OMSHELL "set hardware-type = 1\n";
      print OMSHELL "set ip-address = $ip\n";
      print OMSHELL "create\n";
      close (OMSHELL) || die "Unable to close omshell.\n";

      As is, with the proper access credentials, this will
      create a static bootP host on the DHCP server but we must
      capture the output from omshell in order to check for errors and
      process the IP address which is delivered in hexadecimal format
      which will confuse some folks so they should not see it in that
      form.

      How do I capture all the output while omshell is open?
      One thing that did work was to use standard unix I/O redirection
      as in omshell >somefile but it would be more elegant to capture
      all the output in a perl array and not write to a file unless
      one needs to preserve it for some reason.

      The fact that this is the omshell is almost irrelevant.
      This is an external application that mostly produces standard
      output so the same idea should work if you called /bin/csh or
      /bin/bash.

      Thanks to all for any constructive solutions.

      Martin McCormick WB5AGZ Stillwater, OK
      Systems Engineer
      OSU Information Technology Department Telecommunications Services Group
      Stillwater, Oklahoma
    • Yaswanth
      Hi Martin, We can use open2 using which we can read/write through different file handles. This should work for you. ... time is: n ; print WRITE date n ;
      Message 2 of 2 , Jul 11 1:38 AM
      • 0 Attachment
        Hi Martin,

        We can use open2 using which we can read/write through different file
        handles. This should work for you.

        >perl -MIPC::Open2 -e 'open2(READ, WRITE,"sh"); print WRITE "echo Current
        time is:\n"; print WRITE "date\n"; close WRITE; @output = <READ>; close
        READ; print @output;'
        Current time is:
        Thu Jul 11 01:36:12 PDT 2013


        Thanks,
        Yaswanth


        On Fri, Jun 7, 2013 at 7:25 PM, Martin McCormick <martin@...>wrote:

        > **
        >
        >
        > I have a small perl script which I found as an example that calls the
        > omshell, familiar to those who administer DHCP servers. You enter
        > commands and get output on stdout.
        >
        > I want to capture all that output in a list as in
        > @omstrings = < theoutput >;
        > but so far, the magic syntax eludes me because the call to omshell is
        > part of a larger statement and attempts to use @omstrings = `omshell`,
        > something like that just confuses the script. Here it is, slightly
        > obfuscated to protect the crown jewels.
        >
        > #!/usr/bin/perl -w
        > use strict;
        >
        > my @omstrings;
        > my $mac = "aa:bb:cc:dd:ee:ff";
        >
        > my $ip = "192.168.1.54";
        >
        > my $name = "zzzzokstate.edu";
        >
        > #We need to start capturing output from the omshell.
        > open (OMSHELL, "|omshell") || die ("Unable to open omshell\n");
        > print OMSHELL "server 192.168.2.3\n";
        > print OMSHELL "port 7911\n";
        > print OMSHELL "key key_name \"IFiTOLDUIDHAVE2KILLU\"\n";
        > print OMSHELL "connect\n";
        > print OMSHELL "new host\n";
        > print OMSHELL "set name = \"$name\"\n";
        > print OMSHELL "set hardware-address = $mac\n";
        > print OMSHELL "set hardware-type = 1\n";
        > print OMSHELL "set ip-address = $ip\n";
        > print OMSHELL "create\n";
        > close (OMSHELL) || die "Unable to close omshell.\n";
        >
        > As is, with the proper access credentials, this will
        > create a static bootP host on the DHCP server but we must
        > capture the output from omshell in order to check for errors and
        > process the IP address which is delivered in hexadecimal format
        > which will confuse some folks so they should not see it in that
        > form.
        >
        > How do I capture all the output while omshell is open?
        > One thing that did work was to use standard unix I/O redirection
        > as in omshell >somefile but it would be more elegant to capture
        > all the output in a perl array and not write to a file unless
        > one needs to preserve it for some reason.
        >
        > The fact that this is the omshell is almost irrelevant.
        > This is an external application that mostly produces standard
        > output so the same idea should work if you called /bin/csh or
        > /bin/bash.
        >
        > Thanks to all for any constructive solutions.
        >
        > Martin McCormick WB5AGZ Stillwater, OK
        > Systems Engineer
        > OSU Information Technology Department Telecommunications Services Group
        > Stillwater, Oklahoma
        >
        >


        [Non-text portions of this message have been removed]
      Your message has been successfully submitted and would be delivered to recipients shortly.