Forum Discussion
schnizlein
15 years agoContributor
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();
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();
Related Content
Recent Discussions
- 4 days agoemoya