java jsch shell_java – 通过JSch shell的多个命令

我试图使用JSch库通过SSH协议执行多个命令.但我似乎陷入困境,无法找到任何解决方案. setCommand()方法每个会话只能执行一个命令.但我想按顺序执行命令,就像Android平台上的connectbot应用程序一样.到目前为止我的代码是:

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());

}

}

代码似乎连接到服务器,但我认为输入和输出数组缓冲区存在一些问题,因为根本没有输出.有人可以指导我如何正确处理服务器的输入和输出以获得所需的输出?

解决方法:

该命令是一个String,可以是远程shell接受的任何内容.尝试

cmd1 ; cmd2 ; cmd3

按顺序运行几个命令.要么

cmd1 && cmd2 && cmd3

运行命令直到一个失败.

即使这可能有效:

cmd1

cmd2

cmd3

或者在Java中:

channel.setCommand("cmd1\ncmd2\ncmd3");

旁注:不要将密码和用户名放入代码中.将它们放入属性文件并使用系统属性指定属性文件的名称.这样,您可以将文件保留在项目之外,并确保密码/用户名不会泄漏.

标签:jsch,android,java,shell,ssh

来源: https://codeday.me/bug/20190926/1821096.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值