Forum Discussion

schnizlein's avatar
schnizlein
Contributor
15 years ago

[Resolved]Executing remote Unix Script

I've been doing some research and have been unsuccessful in executing a command on a remote Unix server through SoapUI.  I've seen some recommendations using Pure Java, but was obviously hoping to leverage SoapUI's existing Features.

Anyone have any ideas or experience on this?

4 Replies

  • I was able to figure it out by utilizing JSCH. 

    Groovy Script below, visit http://www.jcraft.com/jsch/ to download  jsch-0.1.42.jar. 
    I abstracted this whole thing to a 'utility' test case I can call with parameters.

    package com.content.jsc;
    import com.jcraft.jsch.*;
    import java.awt.*;
    import javax.swing.*;
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import java.io.*;


    public static class MyUserInfo implements UserInfo{
    String password = "";
    public MyUserInfo(String password) { this.password = password; }
    public String getPassword(){ return password; }
    public boolean promptYesNo(String str){return true;}
    public String getPassphrase(){ return null; }
    public boolean promptPassphrase(String message){ return true; }
    public boolean promptPassword(String message){ return true; }
    public void showMessage(String message){}
    }
    def mode = context.expand( '${#TestCase#RUN_MODE}' )
    def wacssid = context.expand( '${#TestCase#WACS_SID}' )
    def date = context.expand( '${#TestCase#DATE}' )
    def pwd = context.expand( '${#TestCase#PASSWORD}' )
    def host = context.expand( '${#TestCase#HOSTNAME}' )
    def command = context.expand( '${#TestCase#COMMAND}' )

    JSch jsch=new JSch();
    session=jsch.getSession(wacssid,host,22)

    // username and password will be given via UserInfo interface.
    UserInfo ui=new MyUserInfo(pwd);
    session.setUserInfo(ui);
    session.connect();
    Channel channel=session.openChannel("exec");

    //log.info(mode)
    ((ChannelExec)channel).setCommand("/u01/wacsapp/"+wacssid+"/MDM/bin/"+command);
    channel.setInputStream(null);
    ((ChannelExec)channel).setErrStream(System.err);
    instream=channel.getInputStream();
    channel.connect();

    byte[] tmp=new byte[1024];

    while(true){
    while(instream.available()>0){
    int i=instream.read(tmp, 0, 100);
    if(i<0)break;
    log.info(new String(tmp, 0, i));
    }
    if(channel.isClosed()){
    log.info("exit-status: "+channel.getExitStatus());
    if (channel.getExitStatus()!=0)
    throw new Exception( "Script "+command+" Failed" );
    break;
    }

    try{Thread.sleep(1000);}catch(Exception ee){ log.info(ee); }
    }
    channel.disconnect();
    session.disconnect();
  • skalaskar's avatar
    skalaskar
    New Contributor
    So once you download the jsch jar file, where do i save it? I mean in which location?

    should it be under this directory -- C:\Program Files\eviware\soapUI-Pro-3.0.1\.install4j ?
  • skalaskar's avatar
    skalaskar
    New Contributor
    Figured it out. The file needs to be saved under the following directory,

    C:\Program Files\eviware\soapUI-Pro-3.0.1\lib

    Once done right click on the file - > properties and then click on Unblock (if it is blocked).

    Following is the the groovy script which works fine on my box,


    package com.content.jsc;
    import com.jcraft.jsch.*;
    import java.awt.*;
    import javax.swing.*;
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import java.io.*;


    public static class MyUserInfo implements UserInfo{
    String password = "";
    public MyUserInfo(String password) { this.password = password; }
    public String getPassword(){ return password; }
    public boolean promptYesNo(String str){return true;}
    public String getPassphrase(){ return null; }
    public boolean promptPassphrase(String message){ return true; }
    public boolean promptPassword(String message){ return true; }
    public void showMessage(String message){}
    }
    def mode = context.expand( '${#TestCase#RUN_MODE}' )
    def username = "xxxxxxx"
    def date = context.expand( '${#TestCase#DATE}' )
    def pwd = "xxxxxxx"
    def host = "xxxxxxx"
    def command = "sh santosh.sh"

    JSch jsch=new JSch();
    session=jsch.getSession(username,host,22)

    // username and password will be given via UserInfo interface.
    UserInfo ui=new MyUserInfo(pwd);
    session.setUserInfo(ui);
    session.connect();
    Channel channel=session.openChannel("exec");

    log.info(mode)

    ((ChannelExec)channel).setCommand(command);
    channel.setInputStream(null);
    ((ChannelExec)channel).setErrStream(System.err);
    instream=channel.getInputStream();
    channel.connect();

    byte[] tmp=new byte[1024];

    while(true){
    while(instream.available()>0){
    int i=instream.read(tmp, 0, 100);
    if(i<0)break;
    log.info(new String(tmp, 0, i));
    }
    if(channel.isClosed()){
    log.info("exit-status: "+channel.getExitStatus());
    if (channel.getExitStatus()!=0)
    throw new Exception( "Script "+command+" Failed" );
    break;
    }

    try{Thread.sleep(1000);}catch(Exception ee){ log.info(ee); }
    }
    channel.disconnect();
    session.disconnect();