- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有代码通过 ssh 连接到远程服务器并向其发送 2 个或更多命令(例如:cd/export/home/ops/bin
和 "./viewlinkload –time 20131205 -19"),但我没有看到执行的命令,也没有收到结果。
我需要获取服务器返回的结果并显示它。
这是代码发送命令:
try {
command = "cd /export/home/ops/bin";
command1="./viewlinkload –time 20131205-19";
session.startShell();
session.getOutputStream().write(command.getBytes());
ChannelInputStream in = session.getInputStream();
ChannelOutputStream out = session.getOutputStream();
InputStream inputStream = session.getInputStream();
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(inputStream));
StringBuilder stringBuilder = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line);
stringBuilder.append('\n');
}
System.out.println("ke qua" + stringBuilder.toString());
// return stringBuilder.toString();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
如果我更改命令是“ls\n”,则最后一条记录在“while ((line = bufferedReader.readLine()) != null)”处挂起并且不运行。帮我。谢谢大家。
最佳答案
Jsch 在示例目录中提供了一些优秀的示例,您可能会特别感兴趣的是 Exec
。您可能还对 Shell
这是一个稍作修改的版本,它跳过从命令行获取信息并提示输入用户信息和命令,而只是尝试直接连接到远程计算机并执行 ls
命令。
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.UserInfo;
import java.io.InputStream;
import java.util.Properties;
public class TestShell {
public static void main(String[] arg) {
try {
JSch jsch = new JSch();
Session session = jsch.getSession("username", "computer", 22);
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
// Skip prompting for the password info and go direct...
session.setPassword("happybunnyslippers");
session.connect();
String command = "ls";
Channel channel = session.openChannel("exec");
((ChannelExec) channel).setCommand(command);
((ChannelExec) channel).setErrStream(System.err);
InputStream in = channel.getInputStream();
System.out.println("Connect to session...");
channel.connect();
byte[] tmp = new byte[1024];
while (true) {
while (in.available() > 0) {
int i = in.read(tmp, 0, 1024);
if (i < 0) {
break;
}
System.out.print(new String(tmp, 0, i));
}
if (channel.isClosed()) {
System.out.println("exit-status: " + channel.getExitStatus());
break;
}
try {
Thread.sleep(1000);
} catch (Exception ee) {
}
}
channel.disconnect();
session.disconnect();
} catch (Exception e) {
System.out.println(e);
}
}
}
我在我的 Windows 机器上测试了这一点,连接到我的一台 Mac 机器,没有任何问题
更新了被黑的 Shell 示例
基本上,这是一个基于 Shell
示例的黑客示例。
这使用自定义OutputStream
来监视从远程计算机发送的内容的更改,并且可以发出命令。这是非常基本的,事实上我所做的就是等待 $
发送到输出流,然后发出下一个命令。
修改它不需要太多的工作,这样,基于当前的命令/命令索引,您就可以进行不同的解析...
import com.jcraft.jsch.*;
import java.awt.*;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import javax.swing.*;
public class TestShell {
public static void main(String[] arg) {
try {
JSch jsch = new JSch();
String host = null;
final Session session = jsch.getSession("user", "remotecomputer", 22);
session.setPassword("fluffybunnyslippers");
session.setConfig("StrictHostKeyChecking", "no");
session.connect(30000); // making a connection with timeout.
final Channel channel = session.openChannel("shell");
PipedInputStream pis = new PipedInputStream();
final PipedOutputStream pos = new PipedOutputStream(pis);
channel.setInputStream(pis);
channel.setOutputStream(new OutputStream() {
private int cmdIndx = 0;
private String[] cmds = {
"ls\n",
"cd ..\n",
"ls\n",
"exit\n"
};
private String line = "";
@Override
public void write(int b) throws IOException {
char c = (char) b;
if (c == '\n') {
logout(line);
System.out.print(line);
line = "";
} else {
line += c;
logout(line);
if (line.endsWith("$ ")) {
String cmd = cmds[cmdIndx];
cmdIndx++;
pos.write(cmd.getBytes());
}
}
}
public void logout(String line) {
if (line.startsWith("logout")) {
System.out.println("...logout...");
channel.disconnect();
session.disconnect();
System.exit(0);
}
}
});
channel.connect(3 * 1000);
} catch (Exception e) {
System.out.println(e);
}
}
}
关于java - 通过 ssh 发送命令并读取输出结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20560240/
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题? Update the question所以它是on-topic对于堆栈溢出。 10年前关闭。 Improve this qu
我错误地设置了 ssh key 的密码。 但是每次使用 key 时都要输入密码很烦人。 我想删除我的 key 的密码: $ ssh-keygen -p -f 但是,我担心 key 会被更改。 由于我
你好!我在连接到远程主机时遇到问题。我忘记了 ssh 密码,但我有 SSH key 的指纹。是否可以仅使用指纹进行连接? 最佳答案 不会。机器的指纹仅用于验证您正在连接您认为正在连接的机器。它是用于避
我有一个 Ubuntu 服务器 example.com 并且我已经通过 ssh 进入了它。但只有在我通过 ssh 进入它之后,我才意识到我打算执行 ssh -L 8000:localhost:9000
通过终端,我可以多次 SSH 连接到服务器: (客户端--->网关--->服务器1---->服务器2---) 但是现在要通过Java的JSch库来实现,怎么办呢?首先尝试了端口转发,但在终端上我没有这
在创建一个新的仓库(本地 + 远程)之后,我正在尝试推送提交。但是我遇到了 SSH key 的问题 在我的 ~/.ssh/config 中,我有一个 gitlab 条目定义为 Host MyRepoJ
我发现 ssh 和 ssh-keygen 有一种奇怪的行为:它们根本没有反应。 Cygwin 以管理员权限启动并正常运行。主机 192.168.1.1 已启动,我可以远程桌面到它: 当我尝试时: $
为什么eval语句返回 Illegal variable name $ eval "$(ssh-agent -s)" Illegal variable name. 最佳答案 请尝试$ eval "ss
我正在尝试通过以下命令从我的本地主机连接到远程主机。但是远程主机中有一个设置,我们登录后不久它会提示输入徽章 ID、密码和登录原因,因为它在 profile 中是这样编码的文件在 remote-hos
有时,我被迫在不稳定的互联网连接上使用 ssh。 ping some.doma.in PING some.doma.in (x.x.x.x): 56 data bytes Request timeou
This thread is a response to the titled question : Why ssh-agent doesn't forward my SSH certificate
关闭。这个问题是off-topic .它目前不接受答案。 想改善这个问题吗? Update the question所以它是 on-topic对于堆栈溢出。 8年前关闭。 Improve this q
因此,我在10.10.10.x子网中有一堆机器,基本上所有这些机器都以相同的方式配置。我将这些与我的10.10.11.x子网中的计算机区分开,后者具有不同的用途。 我希望能够输入“ssh 10.x”以
我不想在 vagrant box 上创建新的 SSH key 对,而是想重新使用主机上的 key 对,使用 agent forwarding 。我尝试过设置 config.ssh.forward_ag
我有一台服务器,可以通过ssh通过pubkey身份验证通过ssh访问: ssh "Host" -l "my identity" 这很好。但是,我无法在一个命令中使用我的私钥进行端口重定向和标识(这是我
创建一对 key 并通过 ssh-copy-id 将公钥发送到服务器后,我仍然无法在没有密码的情况下登录 ssh -v user@host 的输出 debug1:在/home/pumba/.ssh/k
我被困在 Permission denied (publickey) hell 中,试图将公钥复制到远程服务器,以便 Jenkins 可以在构建期间同步文件。 运行: sudo ssh-copy-id
我正在尝试通过我大学的代理服务器ssh到我们实验室的服务器之一。我们的目标是使用paramiko使其自动化,但我试图首先了解终端机级别中正在发生的事情。 我试过了 ssh -o ProxyComman
我在说明问题上有问题,我想通过SSH切换到我的Cisco交换机,但是我遇到了一些麻烦 我写了两个脚本, 有了这个我没有任何问题,我可以通过运行脚本并输入用户名和密码来更改默认网关: from Exsc
每个人 我目前正在使用一个集群,并且会经常使用“ssh”连接到它。有时我需要打开多个终端。因此,每次需要连接时,我都必须输入“ssh username@cluster.com”,然后输入“密码”(我在
我是一名优秀的程序员,十分优秀!