@Override protected Integer doInBackground(String... arg0) { StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy); Thread.currentThread().setName("SSHConnectionThread"); try { JSch jsch = new JSch(); session = jsch.getSession(arg0[1], arg0[0], 22); session.setPassword(arg0[2].getBytes()); session.setConfig("StrictHostKeyChecking", "no"); session.connect(timeout); System.out.println("connesso? " + session.isConnected()); int assinged_port = session.setPortForwardingL(lhost, lport, rhost, rport); session.setPortForwardingL(lhost, 9001, rhost, 502); System.out.println("localhost:" + assinged_port + " -> " + rhost + ":" + rport); ConnectionManager.setSession(session); } catch (Exception e) { // System.out.println(e.getLocalizedMessage()); // String errore = ""; // if(e instanceof JSchException) errore = "Errore SSH "; // else if(e instanceof UnknownHostException) errore = "Ricontrolla il dominio o la // connessione internet, cè qualcosa di errato, attento agli spazi e maiscole"; // else if(e instanceof ConnectException) errore = "Ricontrolla i campi o la connessione cè // qualcosa che non va, attento agli spazi e maiscole"; // else errore = "Ricontrolla user, password e dominio, cè qualcosa di errato, attento agli // spazi e maiscole"; // if(isDialogActivated()){ // AlertMessageTask errorConnection = new AlertMessageTask(); // errorConnection.setActivity(getActivity()); // errorConnection.execute(errore); // } if (fireEvent && !e.getLocalizedMessage().contains("PortForwardingL:")) { System.out.println("non è portforwarding ma " + e.getLocalizedMessage()); if (MainActivity.handle == null) return null; Message msgObj = MainActivity.handle.obtainMessage(); Bundle ba = new Bundle(); ba.putInt("status", 0); msgObj.setData(ba); MainActivity.handle.sendMessage(msgObj); fireEvent = false; } return null; } if (fireEvent) { if (MainActivity.handle == null) return null; Message msgObj = MainActivity.handle.obtainMessage(); Bundle ba = new Bundle(); ba.putInt("status", 1); msgObj.setData(ba); MainActivity.handle.sendMessage(msgObj); fireEvent = false; } return null; };
@Override public void afterPropertiesSet() { jsch = new JSch(); if (!isConnected) { if (null == session || !session.isConnected()) { try { session = jsch.getSession(sshUsername, host, port); session.setPassword(sshPassword); java.util.Properties config = new java.util.Properties(); config.put("StrictHostKeyChecking", "no"); session.setConfig(config); log.info("Открытие ssh соединения"); session.connect(); log.info("Tunnel status: " + session.isConnected()); log.info("Устанавливаем туннель"); session.setPortForwardingL(tunnelLocalPort, tunnelRemoteHost, tunnelRemotePort); } catch (Exception e) { log.error("Ошибка при построении соединения", e); System.exit(1); } } isConnected = true; } }
public void push(String user, String server, File f, String to) { file = f; if (ssh == null) try { ssh = jsch.getSession(user, server, 22); UserInfo ui = new SSHUserInfo(); ssh.setUserInfo(ui); ssh.setConfig("StrictHostKeyChecking", "no"); ssh.setPortForwardingL(28947, "127.0.0.1", 28947); ssh.connect(); } catch (JSchException e) { // TODO Auto-generated catch block e.printStackTrace(); } Thread t = new Thread(this); t.start(); }
public static void main(String[] arg) { int lport; String rhost; int rport; try { JSch jsch = new JSch(); String host = null; if (arg.length > 0) { host = arg[0]; } else { host = JOptionPane.showInputDialog( "Enter username@hostname", System.getProperty("user.name") + "@localhost"); } String user = host.substring(0, host.indexOf('@')); host = host.substring(host.indexOf('@') + 1); Session session = jsch.getSession(user, host, 22); String foo = JOptionPane.showInputDialog("Enter -L port:host:hostport", "port:host:hostport"); lport = Integer.parseInt(foo.substring(0, foo.indexOf(':'))); foo = foo.substring(foo.indexOf(':') + 1); rhost = foo.substring(0, foo.indexOf(':')); rport = Integer.parseInt(foo.substring(foo.indexOf(':') + 1)); // username and password will be given via UserInfo interface. UserInfo ui = new MyUserInfo(); session.setUserInfo(ui); session.connect(); // Channel channel=session.openChannel("shell"); // channel.connect(); int assinged_port = session.setPortForwardingL(lport, rhost, rport); System.out.println("localhost:" + assinged_port + " -> " + rhost + ":" + rport); } catch (Exception e) { System.out.println(e); } }
protected DebugConnectionDescriptor getSshConnectionDescriptor( CloudFoundryApplicationModule appModule, CloudFoundryServer cloudServer, int appInstance, int remoteDebugPort, IProgressMonitor monitor) throws CoreException { CFInfo cloudInfo = cloudServer.getBehaviour().getCloudInfo(); if (cloudInfo instanceof CloudInfoSsh) { ISshClientSupport ssh = cloudServer.getBehaviour().getSshClientSupport(monitor); if (ssh == null) { return null; } try { printToConsole( appModule, cloudServer, NLS.bind( Messages.SshDebugLaunchConfigDelegate_CONNECTING_FOR_USER, appModule.getDeployedApplicationName()), false); Session session = ssh.connect( appModule.getDeployedApplicationName(), appInstance, cloudServer.getServer(), monitor); printToConsole( appModule, cloudServer, NLS.bind( Messages.SshDebugLaunchConfigDelegate_CONNECTION_SUCCESSFUL, appModule.getDeployedApplicationName()), false); int localDebuggerPort = session.setPortForwardingL(0, "localhost", remoteDebugPort); // $NON-NLS-1$ printToConsole( appModule, cloudServer, NLS.bind( Messages.SshDebugLaunchConfigDelegate_PORT_FORWARDING_SUCCESSFUL, remoteDebugPort, localDebuggerPort), false); return new DebugConnectionDescriptor("localhost", localDebuggerPort); // $NON-NLS-1$ } catch (JSchException e) { throw CloudErrorUtil.toCoreException( "SSH connection error " + e.getMessage()); // $NON-NLS-1$ } } else { throw CloudErrorUtil.toCoreException( "Unable to resolve SSH connection information from the Cloud Foundry target. Please ensure SSH is supported."); //$NON-NLS-1$ } }
private Connection getMySQLSSHConnection() { Connection connection = null; // int assigned_port; final int local_port = 3309; // Remote host and port final int remote_port = 3306; final String remote_host = "remote.host.com"; try { JSch jsch = new JSch(); // Create SSH session. Port 22 is your SSH port which // is open in your firewall setup. System.out.println("DEBUG: get session"); Session session = jsch.getSession("user", remote_host, 22); System.out.println("DEBUG: set password"); session.setPassword("password"); // Additional SSH options. See your ssh_config manual for // more options. Set options according to your requirements. java.util.Properties config = new java.util.Properties(); config.put("StrictHostKeyChecking", "no"); config.put("Compression", "yes"); config.put("ConnectionAttempts", "2"); System.out.println("DEBUG: set configuration"); session.setConfig(config); // Connect System.out.println("DEBUG: SSH connect"); session.connect(); // Create the tunnel through port forwarding. // This is basically instructing jsch session to send // data received from local_port in the local machine to // remote_port of the remote_host // assigned_port is the port assigned by jsch for use, // it may not always be the same as // local_port. System.out.println("DEBUG: get assigned port"); assigned_port = session.setPortForwardingL(local_port, remote_host, remote_port); } catch (JSchException e) { System.out.println("DEBUG: SSH exception: fail"); e.printStackTrace(); return null; } if (assigned_port == 0) { System.out.println("Port forwarding failed !"); return null; } // Database access credintials. Make sure this user has // "connect" access to this database; // these may be initialized somewhere else in your code. final String database_user = "******"; final String database_password = "******"; final String database = "db_name"; // Build the database connection URL. StringBuilder url = new StringBuilder("jdbc:mysql://localhost:"); // use assigned_port to establish database connection url.append(assigned_port) .append("/") .append(database) .append("?user="******"&password="******"DEBUG: load mysql driver"); Class.forName("com.mysql.jdbc.Driver").newInstance(); System.out.println("DEBUG: get connection"); connection = DriverManager.getConnection(url.toString()); } catch (Exception e) { System.out.println("DEBUG get connection failed"); e.printStackTrace(); } return connection; }