1

我使用 SSH.net 连接到我的 Linux。密码每 30 天过期一次。要求之一是自动重置密码。

但它失败了。

在 response.error 中,始终显示错误“警告:您的密码已过期。\n需要更改密码,但没有可用的 TTY。\n”

代码示例如下

类程序{字符串主机=“主机”;国际端口 = 22;字符串用户名 = "用户"; 字符串密码 = "passwd"; 字符串 newpassword = "newpasswd";

    PasswordConnectionInfo connectionInfo;

    void HandlePasswordExpired(Object sender, AuthenticationPasswordChangeEventArgs e)
    {
        e.NewPassword = UTF8Encoding.UTF8.GetBytes(newpassword);
    }


    void Init()
    {
        connectionInfo = new PasswordConnectionInfo(host, username, password);

        connectionInfo.PasswordExpired += HandlePasswordExpired;

    }

    void TestConnection()
    {
        try
        {

            using (var client = new SshClient(connectionInfo))
            {
                client.Connect();
                var resp = client.RunCommand("ls -l /");
                Console.WriteLine("Resp -> {0}", resp.Result);
                client.Disconnect();
            }
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
        }
    }

    static void Main(string[] args)
    {
        Program prog = new Program();
        prog.Init();
        prog.TestConnection();
    }
}
4

0 回答 0