1 year ago

#329993

test-img

carlit

SSH connection does not working on Android

I'm trying to setup a simples ssh connection to perform a single command on a app. The weird thing is that on Android does not work at all but on eclipse works perfectrly.... I m using for it JSCH library. Basically here is the function and as i said works normally on my windows desktop:

public String executeSSHCommand(String host, int port, String username, String password, String command) {

    try {

        JSch jsch = new JSch();
        Session session = jsch.getSession(username, host, port);
        session.setPassword(password);

        // Avoid asking for key confirmation
        Properties prop = new Properties();
        prop.put("StrictHostKeyChecking", "no");
        session.setConfig(prop);

        session.connect();

        // SSH Channel
        ChannelExec channelssh = (ChannelExec) session.openChannel("exec");
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        channelssh.setOutputStream(baos);

        // Execute command
        channelssh.setCommand(command);

        channelssh.connect();


        while (true) {
            if (channelssh.isClosed()) {
                break;
            }
        }

        Snackbar.make(findViewById(R.id.Activity2), R.string.connection_success,
                Snackbar.LENGTH_SHORT).show();
        channelssh.disconnect();
        return baos.toString();
    } catch (JSchException e) {
        // show the error in the UI
        Snackbar.make(findViewById(R.id.Activity2), R.string.connection_failed
                        + e.getMessage(),
                Snackbar.LENGTH_LONG).show();
        return "ERROR";
    }
}

I already gave permissions to internet on the manifets file as well and im not getting any erros, the only one from my catch.....

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"

java

android

jsch

0 Answers

Your Answer

Accepted video resources