执⾏远程服务器上的shell脚本
package com.linkage.interfaces.webservice;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.SocketException;
import java.util.ArrayList;
import java.util.List;
import lnet.TelnetClient;
import com.jcraft.jsch.JSchException;
import com.linkage.interfaces.webservice.factory.SSHProtocol;
/
**
* Telnet Util
* @version 20140410 v.2
*/
public class TelnetSample implements Runnable{
private InputStream in;
private PrintStream out;
private char prompt = '$';
private String server;
private String user;
private String password;
private String command;
private String directory;
private int port;
/**
* Overloaded constructor
* @param server
* @param user
* @param password
*/
public TelnetSample(String server, String user, String password,String directory,String command,Integer port){ this.server = server;
this.port = port;
this.user = user;
this.password = password;
thismand = command;
this.directory = directory;
}
/**
* Enable thread
*/
private String curLoginUser = null;
/*account Attributes*/
private List<String> iniGrp = new ArrayList<String>();
private SSHProtocol sp;
public void run() {
login(server,user,password);
execCommond(directory,command);
}
public void login(String server,String username,String password) {
sp = new SSHProtocol(server,22);
curLoginUser = username;
try {
} catch (Exception e) {
} catch (Exception e) {
e.printStackTrace();
}
}
int status = -1;
public boolean execCommond(String directory,String command ){
StringBuffer sb = new StringBuffer();
String cmd="chmod +x "+directory+"/"+command;
try {
status = sp.execCommand(cmd, sb);
if(status==0){
cmd="sh "+directory+"/"+command;
status = sp.execCommand(cmd, sb);
if(status!=0){
return false;
}
}else{
return false;
}
} catch (JSchException e) {
e.printStackTrace();
}finally{
this.logout();
}
return true;
}
public void logout(){
if(sp != null)
sp.disconnect();
}
public boolean isLogin(){
if(sp == null)
return false;
return sp.isConnected();
}
// /**
// * test
// * @param args
// */
public static void main(String[] args) {
/*System.out.println(Thread.currentThread().getName() + " 线程运⾏开始!");
TelnetSample tu1 = new TelnetSample("111.11.1.11", "aaaa",
"11111","cd /s1/wxs/sale/businese/program",
"./sale_bi.sh /ngbss1/wxwlbss/sale/stock_businese/program/ 8989 3"); TelnetSample tu2 = new TelnetSample("10.100.10.100", "aaaa",
"11111","cd /businese/program",
"./sale_bi.sh /ngbss/sale/stock_businese/upload_ 832 3");
Thread thread1 = new Thread(tu1);
thread1.start();
Thread thread2 = new Thread(tu2);
thread2.start();
System.out.println(Thread.currentThread().getName() + " 线程运⾏结束!");*/
TelnetSample tu3 = new TelnetSample("199.199.19.29", "root",
"123456","/bss/web/domains/ww_domain",
"ShellFtp2014.sh",22);
Thread thread1 = new Thread(tu3);
thread1.start();
}
}
}
11111","cd /businese/program",
"./sale_bi.sh /ngbss/sale/stock_businese/upload_ 832 3");
Thread thread1 = new Thread(tu1);
thread1.start();
Thread thread2 = new Thread(tu2);
thread2.start();
System.out.println(Thread.currentThread().getName() + " 线程运⾏结束!");*/
TelnetSample tu3 = new TelnetSample("199.199.19.29", "root",
"123456","/bss/web/domains/ww_domain",
"ShellFtp2014.sh",22);
Thread thread1 = new Thread(tu3);
thread1.start();
}
}
SSHProtocol .java类
package com.linkage.maitain.factory;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.util.Iterator;
import java.util.StringTokenizer;
import java.util.Vector;
import org.apache.log4j.Logger;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.UserInfo;
public class SSHProtocol {
private static final Logger LOGGER = Logger(SSHProtocol.class);
public static final UserInfo defaultUserInfo = new UserInfo() {
public String getPassphrase() {
return null;
}
public String getPassword() {
return null;
}
public boolean promptPassword(String arg0) {
return false;
}
public boolean promptPassphrase(String arg0) {
return false;
}
public boolean promptYesNo(String arg0) {
return true;
}
public void showMessage(String arg0) {
}
};
JSch jsch = null;
String host;
String user;
int port = 22;
String password;
int timeout = 15 * 1000;
public static final String RETR_STR = "\n";
private Session session = null;
public SSHProtocol(String host, int port, String user, String password) { jsch = new JSch();
this.host = host;
this.port = port;
this.user = user;
this.password = password;
}
public SSHProtocol(String host, String user, String password) {
jsch = new JSch();
this.host = host;
this.user = user;
this.password = password;
}
public SSHProtocol(String host, int port) {
jsch = new JSch();
this.host = host;
this.port = port;
}
public void setLoginUser(String user, String password) {
this.user = user;
this.password = password;
}
public int execCommand(String command, StringBuffer sb)
throws JSchException {
Channel channel = null; //
String result = null;
int i;
int status = -1;
if (command == null || !isConnected()) {
return -1;
}
try {
channel = session.openChannel("exec"); //
((ChannelExec) channel).setCommand(command);
channel.setInputStream(null);
((ChannelExec) channel)., true); //
InputStream in = InputStream();
byte[] tmp = new byte[2048];
while (true) {
while (in.available() > 0) {
i = in.read(tmp, 0, 1024);
if (i < 0) {
break;
}
result = new String(tmp, 0, i, "utf-8");
sb.append(result);
}
if (channel.isClosed()) {
status = ExitStatus();
// System.out.println("exit-status: "+status);
break;
}
try {
Thread.sleep(200);
} catch (Exception ee) {
}
}
channel.disconnect();
} catch (JSchException e) {
e.printStackTrace();
throw e;
} catch (IOException ex) {
ex.printStackTrace();
}
return status;
}
public String execShell(String[] commands, String[] resps, boolean fetch) throws JSchException {
Channel channel = null;
StringBuffer result = new StringBuffer();
String tmp;
int i;
boolean execOk = false;
if (commands == null || resps == null) {
throw new JSchException("Empty commands or responses to ssh."); } else if (commands.length != resps.length) {
throw new JSchException(
"Commands and responses length not equal to command."); }
if (!isConnected()) {
throw new JSchException("has not connect to server");
pipedinputstream}
try {
channel = session.openChannel("shell");
PipedInputStream pins = new PipedInputStream();
PipedOutputStream pouts = new PipedOutputStream();
ByteArrayOutputStream out = new ByteArrayOutputStream(2048); channel.setInputStream(pins);
channel.setOutputStream(out);
try {
Thread.sleep(200);
} catch (Exception ex) {
ex.printStackTrace();
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论