Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
hackthedev opened this issue May 30, 2021 · 4 comments
Open
Labels
bug Something isn't working help wanted Extra attention is needed in progress

Comments

@hackthedev
Copy link
Owner

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";
        }

    }
@hackthedev hackthedev added the bug Something isn't working label May 30, 2021
@hackthedev hackthedev added the help wanted Extra attention is needed label Jul 8, 2021
@DerSanyar
Copy link

In der ersten Klasse steht:
String port = MainActivity._port;
Aber in der MainActivity Klasse ist es ein Integer.
int port,

Versuch in der ersten Klasse das zuschreiben statt den String
int port = MainActivity._port;

Und bei dem Passwort sind 2 Semikolon am Ende.

@hackthedev
Copy link
Owner Author

Danke ich werd mir das sobald ich von der Arbeit daheim bin mal ansehen und fixen. Weißt du zufällig auch wieso die Methode keinen Wert zurück gibt?

@DerSanyar
Copy link

Suche immer noch den Fehler aber finde einfach keinen.
Oder du hast die Methode gar nicht aufgerufen.
Ansonsten finde ich keinen Fehler im Code.

@hackthedev
Copy link
Owner Author

Aufgerufen wird sie meines Wissens eh sonst würden die nicht gehen aber dennoch erhalte ich keinen Rückgabewert was halt komisch ist aber ja.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed in progress
Projects
None yet
Development

No branches or pull requests

2 participants