Java JSch-解析linux上运行perl程序的输入
我想做的是:Java JSch-解析linux上运行perl程序的输入,java,ssh,jsch,Java,Ssh,Jsch,我想做的是: 通过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.j
ChannelExec
执行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();
}
}
}
}
我想你想要的是:
Writer writer = new BufferedWriter(new OutputStreamWriter(channelexec.getOutputStream()));
writer.write("this is the input to the script\n");
当然,要进行正确的错误处理和清理。:)
您还需要删除此行:
channelexec.setInputStream(null);
根据我在中找到的API文档,感谢您的回答,但我得到了一个错误:类型SSH_ConnectionWhoops的方法OutputStreamWriter(OutputStream)未定义……遗漏了一个“new”。
Writer writer = new BufferedWriter(new OutputStreamWriter(channelexec.getOutputStream()));
writer.write("this is the input to the script\n");
channelexec.setInputStream(null);