Exemplo n.º 1
0
  @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;
  };
Exemplo n.º 2
0
  public String runCommand(String params) {
    try {
      StringBuilder sb = new StringBuilder();
      Channel channel = ConnectionManager.getSession().openChannel("exec");
      channel.setInputStream(null);
      channel.setOutputStream(System.out);
      ((ChannelExec) channel).setCommand(params);
      ((ChannelExec) channel).setPty(false);
      channel.connect();
      InputStream in = channel.getInputStream();
      //			byte[] tmp = new byte[1024];
      while (true) {
        InputStreamReader is = new InputStreamReader(in);

        BufferedReader br = new BufferedReader(is);
        String read = br.readLine();
        while (read != null) {
          System.out.println(read);
          sb.append(read);
          read = br.readLine();
        }
        if (channel.isClosed()) {
          System.out.println(sb.toString());
          System.out.println("exit-status:" + channel.getExitStatus());
          break;
        }
        try {
          Thread.sleep(1000);
        } catch (Exception ee) {
        }
      }
      channel.disconnect();
      return sb.toString();
    } catch (Exception e) {
      e.printStackTrace();
      return "empty";
    }
  }