ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

用JAVA更改UNIX密码

2019-10-03 02:02:07  阅读:191  来源: 互联网

标签:jsch passwd java linux command


对不起,如果我的英语太糟糕了.我想问一下从Java执行“passwd”命令(我使用Netbeans IDE& JSCH Library)

这是我的代码

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);
    }
}

问题是,我的输出就像

(current) UNIX password: 

如果我键入/输入当前的UNIX密码,则没有任何反应.
我想更改Ubuntu Server帐户密码,它安装在我的VMWare上.
我在我原来的操作系统上运行这个程序.
简单地说,我希望我的输出交互式(输入和输出就像linux命令’passwd’的输出一样)

我的代码有什么问题? :(需要建议

解决方法:

passwd默认直接从pty而不是stdin读取.您需要将–stdin选项传递给passwd.

有关详细信息,请参阅此问题的答案:Using the passwd command from within a shell script.

标签:jsch,passwd,java,linux,command
来源: https://codeday.me/bug/20191003/1845950.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有