依赖jar包:jsch-0.1.55.jar commons-io-2.5.jar
import com.jcraft.jsch.ChannelExec; import com.jcraft.jsch.JSch; import com.jcraft.jsch.JSchException; import com.jcraft.jsch.Session; import org.apache.commons.io.IOUtils; import java.io.*; public class SSHTest { public static void main(String[] args) throws JSchException, IOException { //建立连接 int port=22; String host="10.XX.XX.XX"; String userName="admin"; String password="XXXX"; JSch jsch = new JSch(); Session session = jsch.getSession(userName, host, port); session.setPassword(password); session.setConfig("StrictHostKeyChecking","no"); session.setTimeout(6000); session.connect(); //建立连接结束 //发送指令 ChannelExec exec = (ChannelExec) session.openChannel("exec"); InputStream in = exec.getInputStream(); exec.setCommand("date"); exec.connect(); String s = IOUtils.toString(in, "GBK"); System.out.println("结果:"+s); in.close(); } }