Auf Thema antworten

Hallo ich konnte es erfolgreich lösen :) ...

[CODE]

/**

 * Created by Roger on 13.03.2015.

 */

import com.jcraft.jsch.ChannelExec;

import com.jcraft.jsch.JSch;

import com.jcraft.jsch.Session;


import java.io.ByteArrayOutputStream;

import java.util.Properties;



public class SSH {

    public static String executeRemoteCommand() throws Exception {

        JSch jsch = new JSch();

        Session session = jsch.getSession("root", "192.168.61.93", 22);

        session.setPassword("schneider");



        // 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("mpc stop");

        channelssh.connect();

        channelssh.disconnect();


        return baos.toString();

    }

}

[/CODE]

[CODE]  btn_werk_srf3.setOnClickListener(

                new Button.OnClickListener() {

                    public void onClick(View v) {


                        new AsyncTask<Integer, Void, Void>(){

                            @Override

                            protected Void doInBackground(Integer... params) {

                                try {

                                    executeRemoteCommand("root","123451234","192.168.61.11", 22, "mpc play 4");

                                } catch (Exception e) {

                                    e.printStackTrace();

                                }

                                return null;

                            }

                        }.execute(1);

                    }

                }

        );


    public static String executeRemoteCommand(String username,String password,String hostname,int port, String message)

            throws Exception {

        JSch jsch = new JSch();

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

        channelssh.connect();

        channelssh.disconnect();


        return baos.toString();

    }[/CODE]

Danke!!



Oben