简体   繁体   中英

Issue with arguments passed to a perl script using Java

I have a perl script that is used to send an SNMP trap to a remote host

   my (@varbinds,$hostname);

   GetOptions(
          "hostname=s"    =>\$hostname,
   );

   $hostnode = `hostname`;     # Unix command to find the hosts name
   chomp $hostnode;
   push (@varbinds, ".0.1",OCTET_STRING, $hostnode);
   for (my $i;$i <= $#ARGV;$i++){
      push (@varbinds, ".0.".($i + 3),OCTET_STRING, $ARGV[$i]);
   }

   my ($session, $error) = Net::SNMP->session(
                    -hostname  => $hostnode,
                    -community => 'public',
                    -port      => 162,
                    -version   => 2,
   );

   my $result = $session->snmpv2_trap(
                      -varbindlist     => \@varbinds
   );

   $session->close();   


   print " SNMP Trap Hostname   -> $hostnode\n";
   for (my $i;$i <= $#varbinds;$i++)
   {
       print "\n\t\t$varbinds[$i], $varbinds[($i+1)],$varbinds[($i+2)]";
       $i += 2;
   }    

I invoke this script using this command perl snmptrap.pl -h myhost "my snmpMsg now"

where myhost is the remote host that is expecting my snmp trap

When I invoke this perl script the remote host receives the snmp message correctly.

The problem comes when I try to invoke this perl script within java class.

The code that I use is given below.

  String SNMPMSG  = "\"my snmpMsg now\" ";
  String script_url = "./snmptrap.pl "+" -h myhost "+SNMPMSG;
  Process p = Runtime.getRuntime().exec(script_url);

  InputStream stdin= p.getInputStream();
  BufferedReader reader = new BufferedReader (new InputStreamReader(stdin));
  while ((line = reader.readLine ()) != null) {
     System.out.println(" " + line);
  }

strangely with this myhost expecting the snmp message gets the messages truncated based on its spaces an receives as 3 messages.

1 my
2.snmpMsg
3.now

if I used a message with non spaces - eg(my_snmpMsg_now) it receives as one message

I am very new to perl scripting and due to infrastructure relate issues, i can't implement the snmp trap sending process using java and need to use the perl script and invoke it using java for the purpose.

Trying to pass the message String with escape characters dint work as well.

any help on this regard is highly appreciated.

You should code it like this:

  String SNMPMSG  = "my snmpMsg now";
  String[] commandAndArgs = new String[]{
          "./snmptrap.pl",
          "-h",
          "myhost", 
          SNMPMSG
  };
  Process p = Runtime.getRuntime().exec(commandAndArgs);

The problem with the way you are currently doing it is that the quotes around the message (to make it a single argument for the Perl command) are a shell-ism. The exec(String) method doesn't understand the quotes. Instead, it simply splits the command string into "arguments" where there are whitespace characters. The result is that the Perl command sees the wrong number of arguments, and arguments with extraneous quote characters.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM