jsch.session怎么使用 linux的信任机制
答案:1 悬赏:50 手机版
解决时间 2021-11-25 09:13
- 提问者网友:城市野鹿
- 2021-11-25 03:19
jsch.session怎么使用 linux的信任机制
最佳答案
- 五星知识达人网友:执傲
- 2021-11-25 04:01
使用 jsch 连接linux
1:首先添加maven 依赖
com.jcraft
jsch
0.1.48
使用密码 方式连接 linux
public static String exec(String host, String user, String psw, int port,
String command) {
String result = "";
Session session = null;
ChannelExec openChannel = null;
try {
JSch jsch = new JSch();
session = jsch.getSession(user, host, port);
session.setPassword(psw.getBytes());
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
openChannel = (ChannelExec) session.openChannel("exec");
openChannel.setCommand(command);
int exitStatus = openChannel.getExitStatus();
System.out.println(exitStatus);
openChannel.connect();
InputStream in = openChannel.getInputStream();
BufferedReader reader = new BufferedReader(
new InputStreamReader(in));
String buf = null;
while ((buf = reader.readLine()) != null) {
result += new String(buf.getBytes("gbk"), "UTF-8")
+ "
\r\n";
}
} catch (JSchException | IOException e) {
e.printStackTrace();
result += e.getMessage();
} finally {
if (openChannel != null && !openChannel.isClosed()) {
openChannel.disconnect();
}
if (session != null && session.isConnected()) {
session.disconnect();
}
}
return result;
}
String exec = exec("192.168.80.101", "root", "111", 22,"sleep 2;ls;");
使用 秘钥方式 连接linux
public static String exec1(String ip, String user, int port,
String privateKey, String passphrase, String command) {
String result = "";
Session session = null;
ChannelExec openChannel = null;
try {
JSch jsch = new JSch();
jsch.addIdentity(privateKey);
session = jsch.getSession(user, ip, port);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
openChannel = (ChannelExec) session.openChannel("exec");
openChannel.setCommand(command);
int exitStatus = openChannel.getExitStatus();
System.out.println(exitStatus);
openChannel.connect();
InputStream in = openChannel.getInputStream();
BufferedReader reader = new BufferedReader(
new InputStreamReader(in));
String buf = null;
while ((buf = reader.readLine()) != null) {
result += new String(buf.getBytes("gbk"), "UTF-8")
+ "
\r\n";
}
} catch (JSchException | IOException e) {
e.printStackTrace();
result += e.getMessage();
} finally {
if (openChannel != null && !openChannel.isClosed()) {
openChannel.disconnect();
}
if (session != null && session.isConnected()) {
session.disconnect();
}
}
return result;
}
String result=exec1("192.168.80.101", "root", 22,"C:\\Users\\ebnew\\Desktop\\office-key(1)", "", "sleep 2;ls;");
1:首先添加maven 依赖
jsch
使用密码 方式连接 linux
public static String exec(String host, String user, String psw, int port,
String command) {
String result = "";
Session session = null;
ChannelExec openChannel = null;
try {
JSch jsch = new JSch();
session = jsch.getSession(user, host, port);
session.setPassword(psw.getBytes());
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
openChannel = (ChannelExec) session.openChannel("exec");
openChannel.setCommand(command);
int exitStatus = openChannel.getExitStatus();
System.out.println(exitStatus);
openChannel.connect();
InputStream in = openChannel.getInputStream();
BufferedReader reader = new BufferedReader(
new InputStreamReader(in));
String buf = null;
while ((buf = reader.readLine()) != null) {
result += new String(buf.getBytes("gbk"), "UTF-8")
+ "
\r\n";
}
} catch (JSchException | IOException e) {
e.printStackTrace();
result += e.getMessage();
} finally {
if (openChannel != null && !openChannel.isClosed()) {
openChannel.disconnect();
}
if (session != null && session.isConnected()) {
session.disconnect();
}
}
return result;
}
String exec = exec("192.168.80.101", "root", "111", 22,"sleep 2;ls;");
使用 秘钥方式 连接linux
public static String exec1(String ip, String user, int port,
String privateKey, String passphrase, String command) {
String result = "";
Session session = null;
ChannelExec openChannel = null;
try {
JSch jsch = new JSch();
jsch.addIdentity(privateKey);
session = jsch.getSession(user, ip, port);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
openChannel = (ChannelExec) session.openChannel("exec");
openChannel.setCommand(command);
int exitStatus = openChannel.getExitStatus();
System.out.println(exitStatus);
openChannel.connect();
InputStream in = openChannel.getInputStream();
BufferedReader reader = new BufferedReader(
new InputStreamReader(in));
String buf = null;
while ((buf = reader.readLine()) != null) {
result += new String(buf.getBytes("gbk"), "UTF-8")
+ "
\r\n";
}
} catch (JSchException | IOException e) {
e.printStackTrace();
result += e.getMessage();
} finally {
if (openChannel != null && !openChannel.isClosed()) {
openChannel.disconnect();
}
if (session != null && session.isConnected()) {
session.disconnect();
}
}
return result;
}
String result=exec1("192.168.80.101", "root", 22,"C:\\Users\\ebnew\\Desktop\\office-key(1)", "", "sleep 2;ls;");
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯