- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我尝试先使用 JSch 在远程系统中执行 sudo su - user
命令,然后连续执行管道 shell 命令。我想将命令输出读入 String
。我怎样才能做到这一点?我正在尝试这样的事情,但无法获得任何输出。请指教。
session = getSession();
channel = (ChannelShell)session.openChannel( "shell" );
String sudoCommand = "sudo su - " + sudoAs;
String nextCommand = "ps -eaf | grep user | wc -l";
pipeIn = new PipedInputStream();
pipeOut = new PipedOutputStream( pipeIn );
channel.setInputStream(pipeIn);
channel.setOutputStream(System.out, true);
channel.connect(3*1000);
pipeOut.write(sudoCommand.getBytes());
Thread.sleep(1000);
pipeOut.write(nextCommand.getBytes());
最佳答案
您在命令末尾缺少“Enter”(换行符)。
所以它们实际上从不执行,这就是为什么您无法获得任何输出的原因。
String sudoCommand = "sudo su - " + sudoAs + "\n";
String nextCommand = "ps -eaf | grep user | wc -l\n";
虽然一般来说,您不应该使用“shell” channel 来执行命令。使用“exec”命令。
参见 What is the difference between the 'shell' channel and the 'exec' channel in JSch
查看官方的JSch Exec.java
例子:
http://www.jcraft.com/jsch/examples/Exec.java.html
关于linux - 使用 JSch 执行命令时没有得到任何输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31844192/
我正在使用 jsch 进行 sftp 文件传输。当我通过设置缓冲区大小 512(-B 选项)sftp B 512 [sftp 服务器名称] 并调用 put 命令使用 sftp 命令发送文件时,我可以以
我正在尝试使用 JSch 连接到服务器,然后执行交互式 su 命令。 你能告诉我我错过了什么吗?这些东西每次都会挂起,我认为这种行为没有任何理由。特别是在发送密码之前,我专门等待密码字符串(实际上是
在 JSch 中每个 session 可以打开多少个 channel 是否有限制?我试图打开的 channel 是“ChannelShell”类型 我收到“com.jcraft.jsch.JSchEx
我正在尝试从我的 android 手机通过 ssh 连接到 ssh 服务器。 我的进口线: import com.jcraft.jsch.*; 剩下的就在这里 try{
尝试使用 ssh key 身份验证连接到主机。下面是我的代码: package com.mkyong.common; import com.jcraft.jsch.Channel; import co
我正在将一些 java 代码移植到 GRAILS 框架中,但在导入库 (jcraft) 时不断收到此错误: import com.jcraft.jsch.ChannelSftp; import com
我编写了一个连接到远程 LINUX 计算机并执行命令的程序。我使用 jsch-0.1.42.jar 文件连接到远程计算机。我的程序运行良好并返回输出。现在我想调用方法,该方法是使用以下代码从 JSP
当使用 jsch 版本 0.1.51 连接到远程主机时,我们偶尔会在 ChannelExec 上调用 Channel.connect() 时遇到以下异常。 com.jcraft.jsch.JSchEx
GerritTrigger插件以前工作得很好,但从一个我无法确定的时间点来看,连接永远无法建立,错误消息如下:。Gerit触发器版本:2.39.0。SSH密钥是通过ssh-keygen生成的。我注意到
我正在尝试连接到我的一个 unix 服务器,但由于上述错误而失败,请有人帮忙。 JSch jsch = new JSch(); java.util.Properties configuration =
我使用他下面的代码连接到 ssh 服务器。 JSch jsch = new JSch(); Session session = jsch.getSession("username", "ip", po
我有运行 ssh 服务器的 windows 机器。我知道那台机器上的路径。让它成为 D:/Folder1/Folder2 .我正在使用 JSCH 创建 sftp channel 。但是当我尝试 cd
我正在使用 Jsch 连接 sshd 服务器,我是这方面的新手。我成功发送了非控制字符,例如 a、b ...等等。但是,我需要发送“Esc”键命令(控制字符)来执行特定任务。根据link ,我尝试了几
我无法将一台unix服务器的安全服务器连接到另一台unix服务器。通过ssh从putty我可以轻松连接,但从jsch连接时出现以下错误。 成功putty步骤->在putty中连接server1->成功
我的命令有第一部分,其结果通过管道传输到第二部分。当我运行代码时,我总是返回第一部分的结果,而第二部分应用的过滤器显然没有执行。 所以我得到: CONTAINER ID IMAGE COMMAN
我正在尝试使用 JSCH 库通过 SSH 执行多个命令。我面临一个奇怪的问题。当我尝试在 Eclipse 中运行该程序时,代码完成了执行,但命令(mkdir test)未在服务器中执行但是当我一步步调
我正在使用 JSCH 通过 SSH 访问主机,然后启动一些命令。 主机未使用“通用”操作系统,因为它是 BNG:http://en.wikipedia.org/wiki/Border_Network_
好吧,我收到 UnknownHostKey 异常。我不想像这篇文章 com.jcraft.jsch.JSchException: UnknownHostKey 那样使用 StrictHostKeyCh
我正在使用 JSch 执行 sftp get 操作。当我使用 main 方法运行测试时,一切都很好,但使用单元测试时我遇到了问题。错误如下: java.lang.RuntimeException: c
我正在使用 Jsch lib 访问网络中的路由器,但在某些路由器中存在登录横幅。所以我需要读取输出,识别横幅,将其删除并仅获取命令的输出。我正在执行模式下使用 channel 。我认为的一个解决方案是
我是一名优秀的程序员,十分优秀!