论坛首页 Java企业应用论坛

执行远程服务器上的shell脚本

浏览 1833 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2015-01-05  
package com.linkage.interfaces.webservice;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.net.telnet.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;
  this.command = 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 {
   sp.connect(username, password);
  } 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("131.51.9.15", "wxbss",
    "lq","cd /s1/wxs/sale/businese/program",
    "./sale_bi.sh /ngbss1/wxwlbss/sale/stock_businese/program/upload/20100521024732.txt 8989 3");
  TelnetSample tu2 = new TelnetSample("10.156.76.199", "lq",
    "lq","cd /businese/program",
    "./sale_bi.sh /ngbss/longziqiang/sale/stock_businese/upload_stock/2010.txt 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("130.122.15.29", "root",
    "root","/bss/web/domains/ww_domain",
    "ShellFtp2014.sh",22);
  Thread thread1 = new Thread(tu3);
  thread1.start();

 }

  
}

 

论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics