我有一个用户test
。
当该用户使用chage
命令登录时,我设置了密码更改。
chage -E 2012-01-25 -M 30 -d 0 -W 10 -I 5 test
所以当我尝试运行命令时ls
[root@localhost ~]# ssh test@localhost "ls"
WARNING: Your password has expired.
Password change required but no TTY available.
You have new mail in /var/spool/mail/root
然后我尝试连接ssh
[root@localhost ~]# ssh test@localhost
You are required to change your password immediately (root enforced)
Last login: Tue Dec 27 09:55:55 2011 from localhost
WARNING: Your password has expired.
You must change your password now and login again!
Changing password for user test.
Changing password for test.
(current) UNIX password:
而且我可以为用户设置密码。
如果我尝试将其与paramiko
.
In [1]: import paramiko
In [2]: ssh_conn = paramiko.SSHClient()
In [3]: ssh_conn.set_missing_host_key_policy(paramiko.AutoAddPolicy())
In [4]: ssh_conn.load_system_host_keys()
In [5]: ssh_conn.connect('n2001', username='root_acc23', password='test')
In [6]: a = ssh_conn.exec_command('ls')
In [7]: print a[2].read()
WARNING: Your password has expired.
Password change required but no TTY available.
然后我做了一些谷歌并找到了一些解决方案来设置新密码invoke_shell
显示我写了一个函数
def chage_password_change(ssh_conn, password, curr_pass):
'''
If got error on login then set with interactive mode.
'''
interact = ssh_conn.invoke_shell()
buff = ''
while not buff.endswith('UNIX password: '):
resp = interact.recv(9999)
buff += resp
interact.send(curr_pass + '\n')
buff = ''
while not buff.endswith('New password: '):
resp = interact.recv(9999)
buff += resp
interact.send(password + '\n')
buff = ''
while not buff.endswith('Retype new password: '):
resp = interact.recv(9999)
buff += resp
interact.send(password + '\n')
interact.shutdown(2)
if interact.exit_status_ready():
print "EXIT :", interact.recv_exit_status()
print "Last Password"
print "LST :", interact.recv(-1)
这在某些情况下是有效的,例如当我们提供带有数字、alpa 和特殊字符组合的正确密码时。
但是当我们输入一些短密码或密码更改发生错误时
[root@localhost ~]# ssh test@localhost
You are required to change your password immediately (root enforced)
Last login: Tue Dec 27 10:41:15 2011 from localhost
WARNING: Your password has expired.
You must change your password now and login again!
Changing password for user test.
Changing password for test.
(current) UNIX password:
New password:
Retype new password:
BAD PASSWORD: it is too short
在这个命令中,我们得到错误BAD PASSWORD: it is too short所以我无法在我的函数中确定。当我这样做时出现此错误,interact.recv(-1)
但这是我认为的标准输出。那么有什么方法可以确定这是错误。
我检查了 paramiko 文档,发现Channel
该类有一些方法recv_stderr_ready
,recv_stderr
但该错误并未出现在该数据中。
感谢您提前提供帮助。