我有一個用戶test
。如何在paramiko第一次調用命令時設置密碼?
當用戶使用chage
命令登錄時,我設置了密碼更改。
chage -E 2012-01-25 -M 30 -d 0 -W 10 -I 5 test
所以,當我嘗試運行命令ls
[[email protected] ~]# ssh [email protected] "ls"
WARNING: Your password has expired.
Password change required but no TTY available.
You have new mail in /var/spool/mail/root
然後我嘗試用ssh
[[email protected] ~]# ssh [email protected]
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和特殊字符組合。
但是,當我們給一些短密碼或錯誤發生在更改密碼這樣
[[email protected] ~]# ssh [email protected]
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
在此命令,我們得到了錯誤錯誤密碼:太短所以這個我不能在我的函數來確定。當我做interact.recv(-1)
時,我得到這個錯誤,但這是我想的標準輸出。那麼有什麼方法可以確定這是錯誤嗎?
我檢查paramiko文檔,發現Channel
類有一些方法recv_stderr_ready
和recv_stderr
但這個錯誤沒有出現在該數據。
Thx爲您的幫助提前。
THX @ScottHunter您的回覆。我知道錯誤可能是2-3個可能的單詞。我想,如果我在標準錯誤獲取輸出,而不檢查標準輸出,以提高錯誤。如果我要檢查錯誤輸出和錯誤然後我在標準輸出每一次檢查。 – Nilesh 2011-12-28 04:00:44
不幸的是,如果stdout是被寫入錯誤消息,其中,這就是你必須從閱讀它。抱歉。 – 2011-12-28 15:32:42
奧基我會從STDOUT THX的幫助閱讀。 – Nilesh 2011-12-29 05:46:36