Java SSH 登录时更改密码

问题描述 投票:0回答:3
private String user = "root",
            newPassword = "test123";

private int port = 22;

public SSHConnection(String host, String password) {
     try {
        JSch jsch = new JSch();

        Session session = jsch.getSession(user, host, port);
        session.setPassword(password);
        Properties config = new Properties();
        config.put("StrictHostKeyChecking", "no");
        session.setConfig(config);
        session.connect();

        ChannelExec channel = (ChannelExec)session.openChannel("exec");
        OutputStream out = channel.getOutputStream();
        ((ChannelExec)channel).setErrStream(System.err);
        channel.connect();

        out.write(password.getBytes());
        out.flush();
        out.write(newPassword.getBytes());
        out.flush();
        out.write(newPassword.getBytes());
        out.flush();

        channel.disconnect();
        session.disconnect();
    }
    catch(Exception e) {
        e.printStackTrace();
    }
}

我第一次登录服务器时被要求更改密码。我正在尝试使用 JSch 来实现这一点,但我不确定如何才能实现这一点。据我了解,我无法使用任何命令,因为我被迫在执行任何操作之前更改密码,所以我无法使用

 (echo old_password; echo new_password; echo new_password) | passwd username
java ssh jsch
3个回答
0
投票

我通过调用channel.setPty(true);解决了我的问题

private String user = "root",
            newPassword = "test123";

private int port = 22;

public SSHConnection(String host, String password) {
     try {
        JSch jsch = new JSch();

        Session session = jsch.getSession(user, host, port);
        session.setPassword(password);
        Properties config = new Properties();
        config.put("StrictHostKeyChecking", "no");
        session.setConfig(config);
        session.connect();

        ChannelExec channel = (ChannelExec)session.openChannel("exec");
        OutputStream out = channel.getOutputStream();

        ((ChannelExec)channel).setErrStream(System.err);
        channel.setPty(true);
        channel.connect();

        out.write((password + "\n").getBytes());
        out.flush();
        Thread.sleep(1000);

        out.write((newPassword + "\n").getBytes());
        out.flush();
        Thread.sleep(1000);

        out.write((newPassword + "\n").getBytes());
        out.flush();
        Thread.sleep(1000);

        channel.disconnect();
        session.disconnect();
    }
    catch(Exception e) {
        e.printStackTrace();
    }
}

为了保持一致性,我在每次输入之前添加了睡眠,通常您会希望在输入每个密码之前等待输出,但对于我的使用来说,这样做就可以了。


0
投票

对我来说,可以在一个命令行中运行下一个命令,而不使用 Thread.sleep

echo -e \"new-password\nnew-password\" | passwd user-linux

0
投票

对于那些想要使用

passwd
命令更改 SSH 密码的人,下面是执行此操作的代码。

我们使用

channel.setCommand
来执行
passwd
命令,并且由于使用了
channel.setPty(true);
,因此启用了
tty

我们循环收听

InputStream
上的提示,并在
OutputStream
上设置适当的密码。

public class SSHPasswordChanger {

public static void main(String[] args) throws IOException, InterruptedException, JSchException {
    if (args.length != 4) {
        System.err.println("Expected 4 parameters. <host> <username> <current-password> <new-password>");
        return;
    }
    final String host = args[0];
    final String user = args[1];
    final String currentPassword = args[2];
    final String newPassword = args[3];
    changePassword(host, user, currentPassword, newPassword);
}

private static void changePassword(final String host, final String user, final String currentPassword,
                                   final String newPassword) throws IOException, InterruptedException, JSchException {
    JSch jsch = new JSch();
    PrintStream printStream = null;
    try {
        final Session session = jsch.getSession(user, host, 22);
        session.setPassword(currentPassword);
        final Properties config = new Properties();
        config.put("StrictHostKeyChecking", "no");
        session.setConfig(config);
        session.connect();

        final ChannelExec channel = (ChannelExec) session.openChannel("exec");
        channel.setCommand("passwd");
        channel.setPty(true);

        final OutputStream out = channel.getOutputStream();
        final InputStream in = channel.getInputStream();
        printStream = new PrintStream(out, true);

        channel.connect();

        byte[] buffer = new byte[1024];
        int length;
        final StringBuilder output = new StringBuilder();
        while (true) {
            while (in.available() > 0 && (length = in.read(buffer)) != -1) {
                output.append(new String(buffer, 0, length));
            }

            if (output.toString().contains("Old password:")) {
                printStream.println(currentPassword);
                output.setLength(0);
            } else if (output.toString().contains("New password:")) {
                printStream.println(newPassword);
                output.setLength(0);
            } else if (output.toString().contains("Enter the new password again:")) {
                printStream.println(newPassword);
                break;
            }

            if (channel.isClosed()) {
                if (in.available() > 0) continue;
                break;
            }
            Thread.sleep(100);
        }

        channel.disconnect();
        session.disconnect();
    } finally {
        if (printStream != null) {
            printStream.close();
        }
    }
}

}

GIT 项目参考:https://github.com/sunildabburi/ssh-password-changer

© www.soinside.com 2019 - 2024. All rights reserved.