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
| public class SSHConnection {
private String user = "root";
private String password = "xxxxxx";
private String host = "x.x.x.x";
private int port = 22;
private int local_port = 3307;
private String remote_host = "ip";
private int remote_port = 3306;
private Session session = null;
public void SSHConnection() { try { JSch jsch = new JSch(); session = jsch.getSession(user, host, port); session.setPassword(password); session.setConfig("StrictHostKeyChecking", "no"); session.connect(); session.setPortForwardingL(local_port, remote_host, remote_port); } catch (Exception e) { log.error("建立SSH连接失败"); } }
public void closeSSH () { this.session.disconnect(); } }
|