Posted by : Arjun Lagisetty Thursday, February 20, 2014

How do you use ODI to call OS command on Remote machine?

Use case:

Our requirement was to call a script (for namesake lets call it farFarAwayScript.sh) on a machine 2 from a ODI procedure which is executed by an agent installed on machine 1. So the challenge here, was that our agent did not have access to the script, its not just the matter of not having UNIX permission to execute the script. Agent and the script were present in different machines. 

Solution: 

If agent and farFarAwayScript were present in the same machine it would be a simple matter of using OdiOSCommand tool, but the tool does not execute command on remote machines. So, I decided to write a procedure to invoke the farFarAwayScript. Logic of the procedure would be same as a work flow of a human being executing the farFarAwayScript form his/her laptop. It (the human in question, I am trying to be gender neutral here) would login to the machine2 using ssh/telnet/X11 and execute the command. He/She would need the following details to login to the machine and execute the script.

  1. IP address/ Hostname of the remote machine
  2. Username and Password to log into the remote machine.
  3. Script name and the complete path.
These are the steps a human would follow:
  1. Open the SSH client like putty
  2. Enter the credentials and login
  3. Execute the script
  4. Close the client and the connection.
We have to execute the similar steps programatically via agent. Language of choice for us to do this is Java. We will use Jsch java library which enables us to ssh via Java code in to the remote machine. You can install Jsch by downloading the Jar from the website and copy it into $ODI_HOME\oracledi\agent\lib (Recommended for non driver jars - Just a matter of convention) or  $ODI_HOME\oracledi\agent\drivers folder.

Note: You can use variable or procedure options to pass USER_NAME, PASSWORD, HOST_NAME, SSH_PORT and COMMAND. Additionally error stream and the out stream can be printed to the local files also, this code snippet is meant to be guidance.

Now to the code..
import com.jcraft.jsch.*;
String USER_NAME = "user_name";
String HOST_NAME = "host_name";
int SSH_PORT = 22;
String PASSWORD = "password";
String COMMAND = "absolute_path_to_the_command";
JSch jsch = new JSch();
Session session = jsch.getSession(USER_NAME,HOST_NAME, SSH_PORT);
session.setPassword(PASSWORD);
session.setConfig("PreferredAuthentications", "password");
session.setConfig("StrictHostKeyChecking", "no");
session.connect();
Channel channel = session.openChannel("exec");
((ChannelExec) channel).setCommand(COMMAND);
channel.setInputStream(null);
((ChannelExec) channel).setErrStream(System.err);
InputStream in = channel.getInputStream();
channel.connect();
while (true) {
    if (channel.isClosed()) {
        break;
    }
    try {
        Thread.sleep(1000);
        } catch (Exception ee) {
        throw ee;
    }
}
channel.disconnect();
session.disconnect();
 Parameters to set:

Leave a Reply

Subscribe to Posts | Subscribe to Comments

Popular Post

Labels

Blog Archive

Copyright © ODI Pundits - Oracle Data Integrator - Maintained by Arjun Lagisetty