使用JAVA更改UNIX密码

使用JAVA更改UNIX密码,java,linux,command,jsch,passwd,Java,Linux,Command,Jsch,Passwd,对不起,如果我的英语这么差。我想问一下如何从Java执行“passwd”命令(我使用Netbeans IDE和JSCH库) 这是我的密码 String username = txtusername.getText(); String password = txtpassword.getText(); String ip = txtIP.getText(); int port = 22; Session session = null; Session ses

对不起,如果我的英语这么差。我想问一下如何从Java执行“passwd”命令(我使用Netbeans IDE和JSCH库)

这是我的密码

String username = txtusername.getText();
    String password = txtpassword.getText();
    String ip = txtIP.getText();
    int port = 22;
    Session session = null;
    Session session2=null;
    ChannelExec channel = null;
    Channel channel2 = null;
    StringBuffer result = new StringBuffer();
    try
    {
        JSch shell = new JSch();
        session = shell.getSession(username, ip, port);
        java.util.Properties config = new java.util.Properties();
        config.put("StrictHostKeyChecking", "no");
        session.setConfig(config);
        session.setPassword(password);
        session.connect(3000);
        channel = (ChannelExec) session.openChannel("exec");
        ((ChannelExec)channel).setCommand("passwd");

        channel.setInputStream(null);
        channel.setOutputStream(null);
        ((ChannelExec)channel).setErrStream(System.err);

        InputStream in=channel.getInputStream();
        OutputStream out=channel.getOutputStream();

        PrintStream char_to_send_to_channel = new PrintStream(out,true);
        BufferedReader out_from_channel=new BufferedReader(new InputStreamReader(in));
        char_to_send_to_channel.println();

        String line;
        channel.connect();

        JOptionPane.showMessageDialog(null,"Channel opened!" +"\n");

       // Process p = Runtime.getRuntime().exec("passwd");

        byte[] tmp=new byte[1024];
        while(true){
          while(in.available()>0){
            int i=in.read(tmp, 0, 1024);
            if(i<0) {
                  break;
              }
          }
        }
    } catch (IOException ex) {
        Logger.getLogger(connect.class.getName()).log(Level.SEVERE, null, ex);
    }  catch(JSchException | HeadlessException e){
        JOptionPane.showMessageDialog(null, e);
    }
}
如果我键入/输入当前UNIX密码,则不会发生任何事情。 我想更改Ubuntu服务器帐户密码,它安装在我的VMWare上。 我在我原来的操作系统上运行这个程序。 简单地说,我希望我的输出是交互式的(输入和输出就像linux命令“passwd”的输出一样)


我的代码怎么了(需要建议

passwd
默认情况下将直接从pty而不是stdin读取。您需要将
--stdin
选项传递到
passwd

有关更多信息,请参阅此问题的答案:

(current) UNIX password: