java连接linux客户端工具
package com.xxx.linux;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
public class LinuxClient {
private String host;
private int port = 22;
private String username;
private String password;
private String prvkey;
private Session session = null;
private Channel channel = null;
private InputStream in;
private OutputStream out;
public static LinuxClient build() {
return new LinuxClient();
}
public LinuxClient host(String host) {
this.host = host;
return this;
}
public LinuxClient port(int port) {
this.port = port;
return this;
}
public LinuxClient username(String username) {
this.username = username;
return this;
}
public LinuxClient password(String password) {
this.password = password;
return this;
}
public LinuxClient prvkey(String prvkey) {
this.prvkey = prvkey;
return this;
}
public LinuxClient session() throws JSchException {
this.session = this.getSession();
return this;
}
public Channel getChannel() {
return channel;
}
private LinuxClient() {}
private LinuxClient(
String username,
String password,
String prvkey) throws JSchException {
this.username = username;
this.password = password;
this.prvkey = prvkey;
this.session = this.getSession();
}
public void initShell() {
try {
this.channel = this.session.openChannel("shell");
this.channel.connect(3000);
} catch (Exception e) {
e.printStackTrace();
closeIo(this.out,this.in);
closeRunnable(this.session,this.channel);
}
}
public String sendShell(String command) {
try {
if(this.out == null)
this.out = this.channel.getOutputStream();
if(this.in == null)
this.in = this.channel.getInputStream();
this.out.write(command.getBytes());
this.out.flush();
TimeUnit.SECONDS.sleep(1);
if (this.in.available() > 0) {
byte[] data = new byte[this.in.available()];
int len = this.in.read(data);
if (len > 0)
return new String(data, 0, len, "UTF-8");
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public void closeShell() {
closeIo(this.out,this.in);
closeRunnable(this.session,this.channel);
}
public static void main(String[] args) throws JSchException, IOException {
LinuxClient linuxClient = null;
try {
linuxClient = LinuxClient.build()
.host("42.xxx.xxx.56")
.username("root")
.prvkey("D:/files/ssh_key/chenwei.pem")
.session();
linuxClient.initShell();
String sendShell = linuxClient.sendShell("free -m \n");
System.out.println(sendShell);
} finally {
if(linuxClient != null)
linuxClient.closeShell();
}
}
public boolean fileUpload(
InputStream in,
String targetPath) {
ChannelSftp sftp = null;
Channel channel = null;
Session sshSession = null;
try {
sshSession = this.getSession();
channel = sshSession.openChannel("sftp");
channel.connect();
sftp = this.convertChannel(channel, ChannelSftp.class);
sftp.put(in, targetPath);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}finally {
closeRunnable(sftp,channel,sshSession);
}
}
private Session getSession() throws JSchException {
JSch jsch = new JSch();
if(this.prvkey != null)
jsch.addIdentity(this.prvkey);
jsch.getSession(username, host, port);
Session sshSession = jsch.getSession(username, host, port);
sshSession.setPassword(password);
Properties sshConfig = new Properties();
sshConfig.put("StrictHostKeyChecking", "no");
sshSession.setConfig(sshConfig);
sshSession.connect();
return sshSession;
}
public InputStream fileDownload(String sourceFile) {
ChannelSftp sftp = null;
Channel channel = null;
Session sshSession = null;
try {
sshSession = this.getSession();
channel = sshSession.openChannel("sftp");
channel.connect();
sftp = this.convertChannel(channel, ChannelSftp.class);
return sftp.get(sourceFile);
} catch (Exception e) {
e.printStackTrace();
} finally {
closeRunnable(sftp,channel,sshSession);
}
return null;
}
@SuppressWarnings("unchecked")
public <T>T convertChannel(
Channel channel,
Class<T> clazz) {
return (T)channel;
}
public void closeRunnable(Runnable ...runnables) {
for (Runnable runnable : runnables) {
if(runnable instanceof Session) {
Session session = (Session)runnable;
if(session.isConnected())
session.disconnect();
}else
if(runnable instanceof Channel) {
Channel channel = (Channel)runnable;
if(channel.isConnected())
channel.disconnect();
}
}
}
public void closeIo(Closeable ...closeables) {
for (Closeable closeable : closeables) {
if(closeable != null)
try {
closeable.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}