POM配置
<dependency>
<groupId>com.jcraftgroupId>
<artifactId>jschartifactId>
<version>0.1.55version>
dependency>
工具类
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 java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
public class SSHTool {
public static void main(String[] args) throws IOException, JSchException {
SSHTool ssh = new SSHTool("", "", "", 22);
ssh.Connection();
ssh.executeCmd("ls -ll");
ssh.disconnection();
return;
}
private int sshPort;
private String username;
private String password;
private String hostIp;
private String charset = "UTF-8";
private Session session;
public SSHTool(String user, String password, String hostip, int port) {
this.username = user;
this.password = password;
this.hostIp = hostip;
this.sshPort = port;
}
public void Connection() throws JSchException {
JSch js = new JSch();
session = js.getSession(username, hostIp, sshPort);
session.setPassword(password);
session.setConfig("StrictHostKeyChecking", "no");
session.connect();
}
public void disconnection() {
if (session != null && session.isConnected()) {
session.disconnect();
}
}
public void executeCmd(String command) throws JSchException, IOException {
BufferedReader reader = null;
Channel channel = null;
System.out.println("**********************************");
System.out.println("InputCommand:" + "【" + command + "】");
System.out.println("**********************************");
channel = session.openChannel("exec");
((ChannelExec) channel).setCommand(command);
channel.setInputStream(null);
((ChannelExec) channel).setErrStream(System.err);
channel.connect();
InputStream in = channel.getInputStream();
reader = new BufferedReader(new InputStreamReader(in,
Charset.forName(charset)));
String buf = null;
System.out.println("OutPutResult:" + "\n");
StringBuffer buffer = new StringBuffer();
while ((buf = reader.readLine()) != null) {
System.out.println(buf);
buffer.append(buf);
buffer.append("\n");
}
System.out.println("**********************************");
System.out.println("ReturnResult:" + "\n" + buffer.toString());
channel.disconnect();
}
public void SysWinCmd() {
BufferedReader bufferreader = new BufferedReader(new InputStreamReader(System.in));
String inputcmd = "";
BufferedReader reader = null;
Channel channel = null;
try {
while ((inputcmd = bufferreader.readLine()) != null) {
channel = session.openChannel("exec");
((ChannelExec) channel).setCommand(inputcmd);
channel.setInputStream(null);
((ChannelExec) channel).setErrStream(System.err);
channel.connect();
InputStream inputstream = channel.getInputStream();
reader = new BufferedReader(new InputStreamReader(inputstream, Charset.forName(charset)));
String buffer = null;
System.out.println("====================================\n");
System.out.println("Command" + "【" + inputcmd + "】Result is\n");
while ((buffer = reader.readLine()) != null) {
System.out.println(buffer);
}
System.out.println("Command Return Finished\n");
System.out.println("====================================\n");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
channel.disconnect();
}
}
}

- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123