如何重用在另一个类的AsyncTask中创建的SSH(Jsch)会话

时间:2014-09-24 16:11:34

标签: session android-asynctask jsch

我是Android& Java新手,这是我的情况:

我正在尝试使用ssh连接创建连接到Beaglebone Black的应用程序,然后通过发出来自Android设备的命令来控制连接到BBB的一些外围设备。 当用户看到启动画面时,我在AsyncTask中打开(成功)ssh会话,如果连接成功,用户将获得确认,然后可以通过单击一些可用按钮发送预定义命令。

我接下来要做的是让会话打开然后每次我想发出命令并等待来自BBB的响应时创建一个新的通道(exec或shell),但我不知道如何在AsynkTask之外重用这样的ssh会话。

甚至可能吗?

我使用的是Android Studio 0.8.2和Jsch 0.1.51,我的代码如下:

公共类SplashScreen扩展了ActionBarActivity {

public static final int segundos =10;
public static final int milisegundos =segundos*1000;
public static  final int delay=2;
private ProgressBar pbprogreso;
@Override

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash_screen);
    pbprogreso= (ProgressBar)findViewById(R.id.pbprogreso);
    pbprogreso.setMax(maximo_progreso());
    empezaranimacion();
}

public void empezaranimacion()
{
    sshConnect task = new sshConnect();
    task.execute(new String[] {"http:"});

    new CountDownTimer(milisegundos,1000)

    {
        @Override
    public void onTick(long milisUntilFinished){
            pbprogreso.setProgress(establecer_progreso(milisUntilFinished));
        }
       @Override
    public void onFinish(){
           finish();
       }
    }.start();

}
public  int establecer_progreso (long miliseconds)
{
    return (int)((milisegundos-miliseconds)/1000);
}

public int maximo_progreso () {
    return segundos-delay;
}

public class sshConnect extends AsyncTask <String, Void, String>{
    ByteArrayOutputStream Baos=new ByteArrayOutputStream();
    ByteArrayInputStream Bais = new ByteArrayInputStream(new byte[1000]);
    @Override
    protected String doInBackground(String... data) {

        String host = "xxxxxxx";
        String user = "root";
        String pwd = "";
        int port = 22;
        JSch jsch = new JSch();
        try {
            Session session = jsch.getSession(user, host, port);
            session.setPassword(pwd);
            session.setConfig("StrictHostKeyChecking", "no");
            session.connect();

            ChannelExec channel = (ChannelExec)session.openChannel("exec");
            channel.setOutputStream(Baos);
            channel.setInputStream(Bais);
            //Run Command
            channel.setCommand("python ~/BBB_test/testconnect.py");
            channel.connect();
            try{Thread.sleep(3500);}catch (Exception ee){}
            channel.disconnect();
            //session.disconnect();
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
        return Baos.toString();
    }
    @Override
    protected void onPostExecute(String result) {
        if (result.equals("Connected to BBB Baby!!\n")) {
            Intent nuevofrom = new Intent(SplashScreen.this, Principal.class);
            startActivity(nuevofrom);
            finish();
        } else {
            Intent newfrom = new Intent(SplashScreen.this, ConnecError.class);
            startActivity(newfrom);
            finish();

        }
    }
}

//这是我想重用已打开的会话并创建新频道的地方

public class sendCommand extends AsyncTask <String, Void, String >{
    ByteArrayOutputStream Baosc=new ByteArrayOutputStream();
    ByteArrayInputStream Baisc = new ByteArrayInputStream(new byte[1000])

    protected String doInBackground (String... command){
        try {
            ChannelExec channel2 = (ChannelExec)session.openChannel("exec");
            channel2.setOutputStream(Baosc);
            channel2.setInputStream(Baisc);
            //Run Command
            channel2.setCommand("python ~/BBB_test/testgpio.py");
            channel2.connect();
            try{Thread.sleep(3500);}catch (Exception ee){}
            channel2.disconnect();


        }catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return  Baosc.toString();
    }
    protected void onPostExecute(Long result) {
        TextView txt = (TextView) findViewById(R.id.infotext);
        txt.setText(result);

    }

}

如果需要其他东西,请告诉我! (这是我第一次在论坛上提问)

非常感谢您的时间和支持!

2 个答案:

答案 0 :(得分:1)

我设法通过使用DamienKnight的建议在Asynktask类之外创建会话来获得我想要的东西。我使用三种方法创建一个公共类来创建,返回和断开会话:

public static class cSession {
        String host = "xxx.xxx.xxx.xxx";
        String user = "root";
        String pwd = "";
        int port = 22;
        JSch jsch = new JSch();
        public Session Met1 (){
            try {
                session = jsch.getSession(user, host, port);
                session.setPassword(pwd);
                session.setConfig("StrictHostKeyChecking", "no");
            } catch (Exception e2){
                System.out.println(e2.getMessage());
            }return session;
        }
        public Session damesession (){
            return session;
        }
        public void close_ses(){
            session.disconnect();
        }
    }

通过这样做,通道的创建是灵活的,我也可以使用Jsch的方法。

public class sshConnect extends AsyncTask <String, Void, String>{
        ByteArrayOutputStream Baos=new ByteArrayOutputStream();
        ByteArrayInputStream Bais = new ByteArrayInputStream(new byte[1000]);


        @Override
        protected String doInBackground(String... data) {
            cSession jschses = new cSession();
            Session ses =null;
            ses = jschses.Met1();
            try {
                ses.connect();
                ChannelExec channel = (ChannelExec)ses.openChannel("exec");
                channel.setOutputStream(Baos);
                channel.setInputStream(Bais);
                //Run Command
                channel.setCommand("python ~/BBB_test/testconnect.py");
                channel.connect();
                try{Thread.sleep(3500);}catch (Exception ee){}
                channel.disconnect();
                //session.disconnect();
            }catch (Exception e){
                System.out.println(e.getMessage());
            }
            return Baos.toString();

        }

谢谢@Damienknight!

此致

答案 1 :(得分:0)

如果您想重复使用会话,则不需要每次都重新协调通道。将其作为shell连接一次,将输入和输出流插入其中。使用流来传递命令并捕获输出。

See the JSCH example on the JCraft website.

  Channel channel=session.openChannel("shell");

  channel.setInputStream(System.in);
  channel.setOutputStream(System.out);

  channel.connect();