https://mvnrepository.com/artifact/com.jcraft/jsch/0.1.54
jar파일 받아서 사용
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;
String username = "root";
String host = "192.168.56.1";
int port = 22;
String password = "root";
System.out.println("==> Connecting to" + host);
Session session = null;
Channel channel = null;
try {
JSch jsch = new JSch();
session = jsch.getSession(username, host, port);
session.setPassword(password);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
channel = session.openChannel("exec");
ChannelExec channelExec = (ChannelExec) channel;
System.out.println("==> Connected to" + host);
channelExec.setCommand("touch /test/jschTest.txt");
channelExec.connect();
System.out.println("==> Connected to" + host);
} catch (JSchException e) {
e.printStackTrace();
} finally {
if (channel != null) {
channel.disconnect();
}
if (session != null) {
session.disconnect();
}
}