python - 如何在第一次调用 paramiko 命令时设置密码?

标签 python unix paramiko change-password

我有一个用户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_readyrecv_stderr 但是这个错误不在那个数据中。

提前感谢您的帮助。

最佳答案

简单的答案是让你的函数在你调用你的 shell 之前检查密码的长度,如果你知道截止是什么。性能也更好。但如果您不知道截止值,那将行不通。

我从你的描述中不清楚,但如果 BAD PASSWORD 消息从 interact.recv(-1) 返回,那么你就知道它发生了,并且可以相应地进行。它似乎应该从 std err 或 stdout 返回,所以检查两者。如果您知道接受新密码后会返回什么文本,那么您也可以检查一下;无论你先得到哪个,都会告诉你发生了什么,你的功能可以从那里继续。

关于python - 如何在第一次调用 paramiko 命令时设置密码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8641463/

相关文章:

python - 在 argparse 中使用变量 arg 名称

regex - 从 Unix 中的完整路径获取文件名

python - 是否可以结合使用 Paramiko 和 Dask 的 read_csv() 方法从远程服务器读取 .csv?

unix - 在 AWK 命令中从配置文件传递输入参数

python - 使用 Paramiko 的 SFTPClient 在远程路径中使用通配符

python - 如何在 Mac OSX 上安装 paramiko?

python - 返回 HTTP 响应并继续处理 - python bottle API

Python质数代码运行缓慢

python - 循环遍历 Names 并在 Name 中添加 1(如果已找到)

bash - bash 中\$$ 有什么用?