我使用SSH.net连接到我的Linux。密码每30天到期一次。 其中一项要求是自动重置密码。
但它失败了。
在回应中,它总是如此 错误“警告:您的密码已过期。\ n需要更改密码,但没有可用的TTY。\ n”
代码示例如下
课程计划 { String host =“host”; int port = 22; String username =“user”; String password =“passwd”; String 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();
}
}