[英]Java JSch - Parse inputs to running perl program on linux
我想做的事:
通過JSch連接到Linux服務器
使用ChannelExec
執行Perl腳本
當Perl腳本運行時,它要求輸入。 提供該輸入
步驟1和2已完成,但是我找不到步驟3的答案。程序停止並等待一些輸入,但是我不知道如何將輸入插入到等待的Perl腳本中。
這是我的代碼:
package test_jar;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
public class SSH_Connection {
private static Session session = null;
private static ChannelSftp channelsftp = null;
private static Channel channelexec = null;
private static List<String> commands = new ArrayList<String>();
public static void main(String[] args) {
int res = sftp_connect("xx.xxx.xx.xx", 22, "user", "password");
if ( res != -1 ) {
commands.add("/home/hello.pl");
execCommands();
}
}
private static String build_commandString(List<String> commandlist) {
String cmdstring = "";
for ( String command: commandlist ) {
if ( ! cmdstring.equals("") ) {
cmdstring += "; ";
}
cmdstring += command;
}
return cmdstring;
}
public static int sftp_connect(String ip, int port, String user, String pw){
try {
JSch JavaSecureChannel = new JSch();
session = JavaSecureChannel.getSession(user, ip, port);
session.setPassword(pw);
//just for testing StrictHostKeyChecking = no
session.setConfig("StrictHostKeyChecking", "no");
session.connect();
//Building SFTP Connection
channelsftp = (ChannelSftp) session.openChannel("sftp");
channelsftp.connect();
channelexec = session.openChannel("exec");
return 0;
}
catch(Exception e) {
e.printStackTrace();
return -1;
}
}
public static void execCommands() {
//check if there are commands to exec
if ( ! commands.isEmpty() ) {
try {
System.out.println(build_commandString(commands));
((ChannelExec)channelexec).setCommand(build_commandString(commands));
channelexec.setInputStream(null);
((ChannelExec)channelexec).setErrStream(System.err);
InputStream in = channelexec.getInputStream();
channelexec.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 ( channelexec.isClosed() ) {
System.out.println("exit-status: "+channelexec.getExitStatus());
break;
}
try {
Thread.sleep(1000);
}
catch(Exception ee) {
}
}
channelexec.disconnect();
System.out.println("DONE");
}
catch(Exception e) {
e.printStackTrace();
}
}
}
}
這是我的Perl腳本:
#!/usr/bin/perl -w
# Hello World Program in Perl
#
print "Hello World!\n";
print "Pls enter user:\n";
my $input = <STDIN>;
print "You entered: $input";
print "Hello World2!\n";
我找到了解決方案。 感謝nickmarkham,我搜索了.getOutputStream()
的文檔並找到了以下解決方案:
//channelexec.setInputStream(null);
OutputStream inputstream_for_the_channel = channelexec.getOutputStream();
PrintStream commander = new PrintStream(inputstream_for_the_channel, true);
//Connect to Exec Channel
channelexec.connect();
//inputs for the running script
commander.println("input1");
commander.println("input2");
commander.close();
我認為您想要類似的東西:
Writer writer = new BufferedWriter(new OutputStreamWriter(channelexec.getOutputStream()));
writer.write("this is the input to the script\n");
當然,還要進行適當的錯誤處理和清理。 :)
您還需要刪除以下行:
channelexec.setInputStream(null);
基於我在https://epaul.github.io/jsch-documentation/javadoc/中找到的API文檔
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.