Solved

Setting Environment variables and then running a perl script from a java program.

Posted on 2001-09-18
7
476 Views
Last Modified: 2008-02-01
I am trying to run perl script called mib2c for UCD-SNMP on Linux. To automate the process , I want to have a java program that does the following:

1.   Sets an environment variable i.e export MIBS=ALL

2. Runs the perl script with the above settings i.e /root/snmp/ucd-snmp-4.1.2/local/mib2c <options>.

I thought that I could do the above using Runtime class like :

Runtime myRuntime = Runtime.getRuntime();
Process myProcess = myRuntime.exec("export MIBS=ALL");
and then use the output stream of the "myProcess" to pass the command for running the perl script.

However , I realised that this is not possible as I get the following message when I read the error streams of the process:

export: local server: Call timed out
export: MIBS=ALL: No such file or directory

Another approach could be to  to open a socket on port 23(i.e telnet session) and then give login and password and then pass the commands. However , I want this to be my last resort as this would require password etc. which would decrease the security + the code would be bulky.

Can anyone provide a solution for my problem?
0
Comment
Question by:Siddhartha_s
7 Comments
 
LVL 1

Expert Comment

by:Lab_Rat
Comment Utility
I think you can settthe "export MIBS=ALL" in a batchfile that sets that environment variable.



something like:

(SetAll.Bat)
set MIBS=ALL


then use (erm!, i think you would need some kind of batchfile parser here, maybe command.com?)

Process myProcess=myRuntime.exec("SetAll.Bat");


Erm, this could help with the enviroment variable. But it's early and I could be way off here.
0
 
LVL 19

Expert Comment

by:Jim Cakalic
Comment Utility
There are several variants of the exec method that take String[] or environment variable settings in the form "name=value" that will be supplied to the new process.
    http://java.sun.com/j2se/1.3/docs/api/java/lang/Runtime.html

Jim
0
 

Expert Comment

by:ManojShetty
Comment Utility

To set the environment variable
 System.setProperty(key, Value)

To access it use System.getProperty(key);
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 19

Accepted Solution

by:
Jim Cakalic earned 200 total points
Comment Utility
Here are a couple classes that might help you. The first, Environment, gets the OS environment. It doesn't use JNI but it does assume the command env exists. This is not completely platform-independent. The second, Launch, calls the Runtime.exec method I alluded to earlier. It instantiates an Environment object and adds to it any name=value pairs that are found in the command line arguments. The last command-line argument to Launch should be the command that you want to execute.

Best regards,
Jim Cakalic

---------- Environment.java ----------
import java.io.*;
import java.util.*;

public class Environment {
    private Properties _props;

    public Environment() {
        _props = new Properties();
        try {
            Runtime rt = Runtime.getRuntime();
            Process proc = rt.exec("env");
            InputStream in = proc.getInputStream();
            _props.load(in);
            in.close();
            proc.destroy();
        } catch (Exception e) {
        }
    }
    public String get(String key) {
        return _props.getProperty(key);
    }
    public String get(String key, String fallback) {
        return _props.getProperty(key, fallback);
    }
    public void set(String key, String val) {
        _props.setProperty(key, val);
    }
    public String[] get() {
        String[] values = new String[_props.size()];
        Enumeration each = _props.keys();
        for (int i = 0; each.hasMoreElements(); ++i) {
            String key = (String)each.nextElement();
            String value = _props.getProperty(key);
            values[i] = key + "=" + value;
        }
        return values;
    }

    public static void main(String[] args) throws Exception {
        Environment env = new Environment();
        String[] values = env.get();
        for (int i = 0; i < values.length; ++i) {
            System.out.println(values[i]);
        }
    }
}
---------- end ----------

---------- Launch.java ----------
import java.io.*;

public class Launch {
    public static void main(String[] args) throws Exception {
        Environment env = new Environment();
        for (int i = 0; i < args.length - 1; ++i) {
            String arg = args[i];
            int index;
            if ((index = args[i].indexOf("=")) >= 0) {
                String key = arg.substring(0, index).trim();
                String value = arg.substring(index + 1).trim();
                env.set(key, value);
            }
        }
        Runtime rt = Runtime.getRuntime();
        Process proc = rt.exec(args[args.length - 1], env.get());
        InputStream in = proc.getInputStream();
        while (in.read() >= 0) {
        }
        proc.waitFor();
    }
}
---------- end ----------
0
 
LVL 20

Expert Comment

by:Venabili
Comment Utility
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

- Points for jim_cakalic

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Venabili
EE Cleanup Volunteer
0
 
LVL 19

Expert Comment

by:Jim Cakalic
Comment Utility
OK. Thanks. :-)
Jim
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now