Java JSch - 解析在linux

时间:2018-05-08 12:48:08

标签: java ssh jsch

我想做什么:

  1. 通过JSch连接到Linux服务器

  2. 使用ChannelExec执行Perl脚本

  3. 在Perl脚本运行时,它会要求输入。提供输入

  4. 步骤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();
    

1 个答案:

答案 0 :(得分:0)

我想你想要这样的东西:

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文档