Your account has expired; please contact your system administrator.
需求是批量验证密码是否过期,密码是否错误,正常不做处理。分别在零点和前台点击触发定时任务(5分钟后)的形式进行效验。(需要检测的就是上面图片中的内容)
这次的需求只需要验证密码,不需要远程执行。结果测试发现密码验证居然拿不到linux返回的验证错误信息。返回的内容一直都是Password authentication failed.
仅凭这个报错是无法判断该主机用户的密码是否已经过期的。断点追踪源码的时候看到AuthenticationManager内部的banner保存了返回结果,高兴的手舞足蹈。但随即发现这个类在Connection中没有提供访问的入口,正常的访问根本无法触碰到am这个属性。
然后各种操作,发现Connection类不是final修饰的,尝试重写…越重写越多…
最后简单的利用几个核心类成功获取到密码失效后的返回结果~
@Test
public void bingo() throws IOException {
SecureRandom generator = null;
if (generator == null) {
generator = new SecureRandom();
}
String hostname = "192.168.0.104";
String username = "z";
String password = "z";
String softwareversion = String.format("Ganymed_%s", Version.getSpecification());
ClientTransportManager tm = new ClientTransportManager();
tm.setSoTimeout(0);
tm.setTcpNoDelay(false);
tm.setConnectionMonitors(new ArrayList<>());
tm.connect(hostname, 22, softwareversion, new CryptoWishList(), null,
new DHGexParameters(), 0, generator);
AuthenticationManager am = new AuthenticationManager(tm);
try {
boolean b = am.authenticatePassword(username, password);
log.info("{}", b);
} catch (Exception e)
{
log.info(am.getBanner());
} finally {
if (tm != null)
{
Throwable t = new Throwable("shutdown failed.");
tm.close(t, true);
}
}
}