Skip to content

Method "cmd()" in ControlActivity.java not returning command output #3

@hackthedev

Description

@hackthedev

The following Code is not returning any command output. If anyone can help me here I'd be really happy.

    public String cmd(
            String command) throws Exception {

        try{
            String username = MainActivity._user;
            String password = MainActivity._pass;;
            String hostname = MainActivity._host;
            String port = MainActivity._port;

            JSch jsch = new JSch();
            Session session = jsch.getSession(username, hostname, Integer.parseInt(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);

            channelssh.setCommand(command);

            channelssh.connect();

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

            channelssh.disconnect();

            return baos.toString();
        } catch (Exception e){
            Log.e("ControlActivity-Login:", e.getMessage());

            return "ERROR";
        }

    }

Although the same function in MainActivity.java does.

    public static String executeRemoteCommand(
            String username,
            String password,
            String hostname,
            int port,
            String command) throws Exception {

        try{
            hostname = hostname.replace(" ", "");

            JSch jsch = new JSch();
            Session session = jsch.getSession(username, hostname, 22);
            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);

            channelssh.setCommand(command);

            channelssh.connect();

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

            channelssh.disconnect();

            return baos.toString();
        } catch (Exception e){
            Log.e("MainActivity-Login:", e.getMessage());

            return "ERROR";
        }

    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions