使用 JSch 为在 SSH 服务器上执行的命令提供多行输入
Provide multiple lines of input to command executed on SSH server with JSch
我遇到了另一个类似密码的弹出窗口,这意味着我需要在密码后输入另一个文本,我还需要以编程方式处理它。
以下代码适用于密码
((ChannelExec)channel).setCommand("cd ~demouser/bin;ls; echo demouser | pbrun democommand");
echo 确实可以让我输入密码。
但就在它之后,我需要像密码一样输入文本,但我无法这样做。所以我用管道放了另一个回声,但它不起作用。
我使用的代码相同
((ChannelExec)channel).setCommand("cd ~demouser/bin;ls; echo Automation | echo demouser | pbrun democommand");
我也尝试了下面的参考并编写了如下命令,仍然没有运气
pipe password to sudo
and other data to sudo
ed command
((ChannelExec)channel).setCommand("cd ~demouser/bin;ls; echo Automation | { echo demopass; } | pbrun democommand");
参考截图:
我使用的代码:
try {
JSch jsch = new JSch();
Session session = jsch.getSession(user, host, 22);
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);;
session.setPassword(password);
System.out.println("user=="+user+"\n host=="+host);
session.connect();
System.out.println("connected to host ===="+host);
String sudo_pass="demopassword";
Channel channel=session.openChannel("exec");
System.out.println("cd command");
((ChannelExec)channel).setCommand("cd ~demouser/bin;ls; ( echo demopassword && echo Automation ) | pbrun democommand");
((ChannelExec) channel).setPty(true);
InputStream in=channel.getInputStream();
OutputStream out=channel.getOutputStream();
((ChannelExec)channel).setErrStream(System.err);
channel.connect();
out.write((sudo_pass+"\n").getBytes());
out.flush();
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);
}
}
任何解决方法都会有所帮助
Bash 为命令提供两行输入的语法是:
( echo input1 && echo input2 ) | command
另见 Pipe multiple commands into a single command。
您还可以在 Java 代码中提供输入,而不是使用 shell 结构:
感谢 Martin,下面的方法对我有用
( echo 'command1' && echo 'command2' ) | command
下面的代码适合我
try {
JSch jsch = new JSch();
Session session = jsch.getSession(user, host, 22);
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);;
session.setPassword(password);
System.out.println("user=="+user+"\n host=="+host);
session.connect();
System.out.println("connected to host ===="+host);
String sudo_pass="demo123";
Channel channel=session.openChannel("exec");
System.out.println("cd command");
((ChannelExec)channel).setCommand("cd ~demouser/bin;ls; ( echo 'echo Automation' && echo 'command' ) | pbrun democommand");
((ChannelExec) channel).setPty(true);
InputStream in=channel.getInputStream();
OutputStream out=channel.getOutputStream();
((ChannelExec)channel).setErrStream(System.err);
channel.connect();
out.write((sudo_pass+"\n").getBytes());
// out.write(("\n").getBytes());
out.flush();
byte[] tmp=new byte[102400];
while(true){
while(in.available()>0){
int i=in.read(tmp, 0, 102400);
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){
ee.printStackTrace();
System.out.println(ee.getMessage());
}
}
channel.disconnect();
session.disconnect();
}
catch(Exception e){
System.out.println(e);
}
}
使用的依赖项:
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.50</version>
</dependency>
我遇到了另一个类似密码的弹出窗口,这意味着我需要在密码后输入另一个文本,我还需要以编程方式处理它。
以下代码适用于密码
((ChannelExec)channel).setCommand("cd ~demouser/bin;ls; echo demouser | pbrun democommand");
echo 确实可以让我输入密码。 但就在它之后,我需要像密码一样输入文本,但我无法这样做。所以我用管道放了另一个回声,但它不起作用。
我使用的代码相同
((ChannelExec)channel).setCommand("cd ~demouser/bin;ls; echo Automation | echo demouser | pbrun democommand");
我也尝试了下面的参考并编写了如下命令,仍然没有运气
pipe password to sudo
and other data to sudo
ed command
((ChannelExec)channel).setCommand("cd ~demouser/bin;ls; echo Automation | { echo demopass; } | pbrun democommand");
参考截图:
我使用的代码:
try {
JSch jsch = new JSch();
Session session = jsch.getSession(user, host, 22);
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);;
session.setPassword(password);
System.out.println("user=="+user+"\n host=="+host);
session.connect();
System.out.println("connected to host ===="+host);
String sudo_pass="demopassword";
Channel channel=session.openChannel("exec");
System.out.println("cd command");
((ChannelExec)channel).setCommand("cd ~demouser/bin;ls; ( echo demopassword && echo Automation ) | pbrun democommand");
((ChannelExec) channel).setPty(true);
InputStream in=channel.getInputStream();
OutputStream out=channel.getOutputStream();
((ChannelExec)channel).setErrStream(System.err);
channel.connect();
out.write((sudo_pass+"\n").getBytes());
out.flush();
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);
}
}
任何解决方法都会有所帮助
Bash 为命令提供两行输入的语法是:
( echo input1 && echo input2 ) | command
另见 Pipe multiple commands into a single command。
您还可以在 Java 代码中提供输入,而不是使用 shell 结构:
感谢 Martin,下面的方法对我有用
( echo 'command1' && echo 'command2' ) | command
下面的代码适合我
try {
JSch jsch = new JSch();
Session session = jsch.getSession(user, host, 22);
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);;
session.setPassword(password);
System.out.println("user=="+user+"\n host=="+host);
session.connect();
System.out.println("connected to host ===="+host);
String sudo_pass="demo123";
Channel channel=session.openChannel("exec");
System.out.println("cd command");
((ChannelExec)channel).setCommand("cd ~demouser/bin;ls; ( echo 'echo Automation' && echo 'command' ) | pbrun democommand");
((ChannelExec) channel).setPty(true);
InputStream in=channel.getInputStream();
OutputStream out=channel.getOutputStream();
((ChannelExec)channel).setErrStream(System.err);
channel.connect();
out.write((sudo_pass+"\n").getBytes());
// out.write(("\n").getBytes());
out.flush();
byte[] tmp=new byte[102400];
while(true){
while(in.available()>0){
int i=in.read(tmp, 0, 102400);
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){
ee.printStackTrace();
System.out.println(ee.getMessage());
}
}
channel.disconnect();
session.disconnect();
}
catch(Exception e){
System.out.println(e);
}
}
使用的依赖项:
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.50</version>
</dependency>