You are on page 1of 6

public class ExecuteShellUtil {

private Vector stdout;

// session

Session session;

public ExecuteShellUtil(final String ipAddress, final String username, final String


password, int port) {

try {

JSch jsch = new JSch();

session = jsch.getSession(username, ipAddress, port);

session.setPassword(password);

session.setConfig("StrictHostKeyChecking", "no");

session.connect(100000);

} catch (Exception e) {

e.printStackTrace();

public int execute(final String command) {

int returnCode = 0;

ChannelShell channel = null;

PrintWriter printWriter = null;


BufferedReader input = null;

stdout = new Vector();

try {

channel = (ChannelShell) session.openChannel("shell");

channel.connect();

input = new BufferedReader(new InputStreamReader(channel.getInputStream()));


printWriter = new PrintWriter(channel.getOutputStream());

printWriter.println(command);

printWriter.println("exit");

printWriter.flush();

log.info("The remote command is: ");

String line;

while ((line = input.readLine()) != null) {

stdout.add(line);

System.out.println(line);

} catch (Exception e) {

e.printStackTrace();

return -1;

}finally {

IoUtil.close(printWriter);

IoUtil.close(input);

if (channel != null) {

channel.disconnect();

return returnCode;

// 断开连接

public void close(){

if (session != null) {
session.disconnect();

// 执行命令获取执行结果

public String executeForResult(String command) {

execute(command);

StringBuilder sb = new StringBuilder();

for (String str : stdout) {

sb.append(str);

return sb.toString();

public static void main(String[] args) {

ExecuteShellUtil executeShellUtil = new ExecuteShellUtil("XXX","abc","XXX",22);

// 执行 ls /opt/命令

String result = executeShellUtil.executeForResult("ls /opt/");

System.out.println(result);

executeShellUtil.close();

3.下载和上传文件

/**

* 下载和上传文件

*/
public class ScpClientUtil {

private String ip;

private int port;

private String username;

private String password;

static private ScpClientUtil instance;

static synchronized public ScpClientUtil getInstance(String ip, int port, String username,
String passward) {

if (instance == null) {

instance = new ScpClientUtil(ip, port, username, passward);

return instance;

public ScpClientUtil(String ip, int port, String username, String passward) {

this.ip = ip;

this.port = port;

this.username = username;

this.password = passward;

public void getFile(String remoteFile, String localTargetDirectory) {

Connection conn = new Connection(ip, port);

try {

conn.connect();

boolean isAuthenticated = conn.authenticateWithPassword(username, password);

if (!isAuthenticated) {

System.err.println("authentication failed");
}

SCPClient client = new SCPClient(conn);

client.get(remoteFile, localTargetDirectory);

} catch (IOException ex) {

ex.printStackTrace();

}finally{

conn.close();

public void putFile(String localFile, String remoteTargetDirectory) {

putFile(localFile, null, remoteTargetDirectory);

public void putFile(String localFile, String remoteFileName, String


remoteTargetDirectory) {

putFile(localFile, remoteFileName, remoteTargetDirectory,null);

public void putFile(String localFile, String remoteFileName, String


remoteTargetDirectory, String mode) {

Connection conn = new Connection(ip, port);

try {

conn.connect();

boolean isAuthenticated = conn.authenticateWithPassword(username, password);

if (!isAuthenticated) {

System.err.println("authentication failed");

SCPClient client = new SCPClient(conn);


if ((mode == null) || (mode.length() == 0)) {

mode = "0600";

if (remoteFileName == null) {

client.put(localFile, remoteTargetDirectory);

} else {

client.put(localFile, remoteFileName, remoteTargetDirectory, mode);

} catch (IOException ex) {

ex.printStackTrace();

}finally{

conn.close();

public static void main(String[] args) {

ScpClientUtil scpClient = ScpClientUtil.getInstance("XXX", 22, "XXX", "XXX");

// 从远程服务器/opt下的index.html下载到本地项目根路径下

scpClient.getFile("/opt/index.html","./");

// 把本地项目下根路径下的index.html上传到远程服务器/opt目录下

scpClient.putFile("./index.html","/opt");

You might also like