通过Jsch壳牌多条命令 [英] Multiple commands through Jsch Shell

查看:468
本文介绍了通过Jsch壳牌多条命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过使用JSch库SSH协议来执行多个命令。但我似乎已经卡住而无法找到任何解决方案。该 set命令()方法只能每个会话执行单一的命令。但我想按顺序执行的命令,就像在Android平台上的应用程序connectbot。到目前为止,我的code是:

I was trying to execute multiple commands through SSH protocol using the JSch library. But I seem to have stuck and cannot find any solution. The setCommand() method can only execute single commands per session. But I want to execute the commands sequentially just like the connectbot app on the Android platform. So far my code is :

package com.example.ssh;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Properties;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;

public class ExampleSSH extends Activity {
    /** Called when the activity is first created. */
    EditText command;
    TextView result;
    Session session;
    ByteArrayOutputStream baos;
    ByteArrayInputStream bais;
    Channel channel;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        bais = new ByteArrayInputStream(new byte[1000]);
        command = (EditText) findViewById(R.id.editText1);
        result  = (TextView) findViewById(R.id.terminal);
    }

    public void onSSH(View v){
        String username = "xxxyyyzzz";
        String password = "aaabbbccc";
        String host     = "192.168.1.1"; // sample ip address
        if(command.getText().toString() != ""){
            JSch jsch = new JSch();
            try {
                session = jsch.getSession(username, host, 22);
                session.setPassword(password);

                Properties properties = new Properties();
                properties.put("StrictHostKeyChecking", "no");
                session.setConfig(properties);
                session.connect(30000);

                channel = session.openChannel("shell");
                channel.setInputStream(bais);
                channel.setOutputStream(baos);
                channel.connect();

            } catch (JSchException e) {
                // TODO Auto-generated catch block
                Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
            }
        }
        else{
            Toast.makeText(this, "Command cannot be empty !", Toast.LENGTH_LONG).show();
        }
    }

    public void onCommand(View v){
        try {
            bais.read(command.getText().toString().getBytes());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        baos = new ByteArrayOutputStream();
        channel.setOutputStream(baos);
        result.setText(baos.toString());

    }
}

在code似乎可以连接到服务器,但我觉得有一些问题,输入和输出数组缓冲区,因为有没有输出。是否有人可以指导我如何处理输入和输出,并从服务器正确地得到需要的结果?

The code seems to get connected to the server but I think there is some problem with the input and output array buffers because there is no output at all. Can someone please guide me how to handle the input and output to and from the server properly to get the desired output?

推荐答案

如果你没有区分各个命令的输入或输出,从阿龙的回答(给所有的命令一排,由<$分开C $ C> \ñ或; )是好的

If you do not have to distinguish the inputs or outputs of the individual commands, the answer from Aaron (giving all the commands in a row, separated by \n or ;) is fine.

如果你必须单独处理它们,或者不知道以后的命令较早的完成之前:您可以打开多个<一个href="http://epaul.github.com/jsch-documentation/simple.javadoc/index.html?com/jcraft/jsch/ChannelExec.html"相对=nofollow> EXEC -channels 在同一会话(即连接),一前一后(即后一前被关闭)。每个人都有自己的命令。 (但他们不共享的环境中,所以光盘命令,在第一个对后来者没有任何影响。)

If you have to handle them separately, or don't know the later commands before the earlier ones are finished: You can open multiple exec-Channels on the same Session (i.e. connection), one after the other (i.e. after the one before was closed). Each one has its own command. (But they don't share environment, so a cd command in the first one has no effect on later ones.)

您只需要照顾有<一个href="http://epaul.github.com/jsch-documentation/simple.javadoc/index.html?com/jcraft/jsch/Session.html"相对=nofollow>会话对象周围,而不是创建一个新的每一个命令。

You simply have to take care to have the Session object around, and not create a new one for each command.

另一种选择是一个<一href="http://epaul.github.com/jsch-documentation/simple.javadoc/index.html?com/jcraft/jsch/ChannelShell.html"相对=nofollow>壳频道,然后(通过流即)使单个命令来远程shell作为输入。但你必须要照顾到不与下一个命令混合输入一个命令(即这只有当你知道这些命令都在做,或者如果你有一个互动的用户谁可以提供两种输入命令下一个命令,并且知道哪一个是要使用时。)

Another option would be a shell channel, and then passing the individual commands to the remote shell as input (i.e. via a stream). But then you have to take care to not mix the the input to one command with the next command (i.e. this works only if you know what the commands are doing, or if you have an interactive user who can supply both input to the command and the next command, and knows which one is to be used when.)

这篇关于通过Jsch壳牌多条命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆