Reputation: 9
how can I connect SSH Tunnel TCP/UDP in android using java language, I tried with JSCH but it doesn't work, is there any other way to connect
Upvotes: 0
Views: 869
Reputation: 36
As @jeb said your question is unclear, but if you are trying to make an ssh connection in android using java you can use this :
First of all you have to add the internet permission in your manifest
<uses-permission android:name="android.permission.INTERNET" />
Then use this following code in Thread/Executor or Async class (in this case im using Thread) :
final Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
// create a connection instance and connect to it
Connection ssh = new Connection(hostname);
ssh.connect();
final boolean authorized = ssh.authenticateWithPassword(username,
password);
runOnUiThread(new Runnable() {
@Override
public void run() {
if (!authorized)
Toast.makeText(MainActivity.this, "could not establish connection", Toast.LENGTH_SHORT).show();
else {
Toast.makeText(MainActivity.this, "wohoo", Toast.LENGTH_SHORT).show();
}
}
});
// if authorized, create the session
Session session = ssh.openSession();
// terminate the session
session.close();
// terminate the connection
ssh.close();
} catch (IOException e) {
e.printStackTrace(System.err);
System.out.println(e.getMessage());
//System.exit(2);
}
}
});
Upvotes: 1