使用JAVA更改UNIX密码 [英] Change UNIX password with JAVA

查看:204
本文介绍了使用JAVA更改UNIX密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,如果我的英语是这么糟糕。我想要问关于从Java执行passwd命令(我使用Netbeans IDE和JSCH库)

sorry if my english is so bad. i wanna ask about executing "passwd" command from Java (i use 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);
    }
}

问题是,我的输出就像



The problem is, my output is just like

(current) UNIX password: 

如果我键入/输入当前UNIX密码,没有发生。
我想更改Ubuntu Server帐户密码,它安装在我的VMWare上。
我在我的原始操作系统上运行这个程序。
简单来说,我想要我的输出交互(输入和输出就像linux命令'passwd'的输出)

And if i type / input current UNIX password,nothing happened. I want change Ubuntu Server account password, its installed on my VMWare. And i run this program on my original OS. Simply, i want my output interactive (input and output just like output from linux command 'passwd')

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

Whats wrong with my code? :( Need Suggestions

推荐答案

passwd pty而不是stdin。您需要将 - stdin 选项传递给 passwd

passwd will by default read directly from the pty instead of stdin. You will need to pass the --stdin option to passwd.

有关详细信息,请参阅此问题的答案:在shell脚本中使用passwd命令

For more information, see the answers to this question: Using the passwd command from within a shell script.

这篇关于使用JAVA更改UNIX密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆