python 批量修改linux用戶的密碼

安裝:paramiko模塊需要依賴 pycrypto
安裝Python
tar -zxvf Python-2.6.tgz
cd  Python-2.6
./configure
make
make install
cd ..
安裝 pycrypto
tar -zxvf  pycrypto-2.6.tar.gz
cd pycrypto-2.6
python2.6 setup.py install
cd ..

安裝 paramiko
tar -zxvf  paramiko-1.7.4.tar.gz
cd paramiko-1.7.4
python2.6 setup.py install



  1. #!/usr/local/bin/python



  2. import string

  3. import os

  4. import paramiko



  5. #設置需要修改的密碼和用戶名

  6. setpasswd="222222"

  7. setuser="root"

  8. #讀配置文件(IP,端口,用戶名,密碼),以逗號分割的字符串,LINUX格式的回車

  9. #192.168.1.118,6868,root,111111

  10. filepwd="./user.txt"


  11. #讀配置文件,返回IP,端口,用戶名,密碼

  12. def get_config_file(lines):

  13.    ip="192.168.1.100"

  14.    port=6000

  15.    user="root"

  16.    password="111111"

  17.    temp_str=string.split(lines,',') #逗號分割

  18.    ip=temp_str[0]

  19.    port=int(temp_str[1])

  20.    user=temp_str[2]

  21.    password=temp_str[3]

  22.    password=string.split(password,'\n')

  23.    password = password[0]

  24. return(ip,port,user,password)



  25. #修改用戶名和密碼

  26. def connect_modify(ip_port_user_pwd):

  27.    client = paramiko.SSHClient()

  28.    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())

  29. try:

  30.        client.connect(ip_port_user_pwd[0],ip_port_user_pwd[1],ip_port_user_pwd[2],ip_port_user_pwd[3], timeout=5)

  31.    #    print ip_port_user_pwd[0],ip_port_user_pwd[1],ip_port_user_pwd[2],ip_port_user_pwd[3]

  32. except:

  33. print ip_port_user_pwd[0],"network not connect"

  34. return 0

  35.    stdin, stdout, stderr = client.exec_command("/bin/echo %s|/usr/bin/passwd --stdin %s"%(setpasswd,setuser))

  36.    client.close()

  37. return 1




  38. def main():

  39. print"nn","setpassword",setpasswd

  40. try:

  41.            f=open(filepwd,"r")

  42. except:

  43. print filepwd,"not open ip and passwd file"

  44. return 0

  45. for lines in f.readlines():

  46.        #print lines

  47.            readline=string.split(lines,'\n')

  48. if len(lines)>20:

  49.            ip_port_user_pwd = get_config_file(lines)

  50.            re=connect_modify(ip_port_user_pwd)

  51. if re ==1 :

  52. print ip_port_user_pwd[0],"OK"

  53.        #else

  54.    f.close()


  55. main()

./user.txt文件的生成按自己要求寫!(如下,如果windows下寫的複製到程序的目錄中,dos2unix命令改一下回事符喲)

192.168.1.110,16000,root,111111
192.168.1.111,16000,root,111111

./xxxxxxxxxxx.py



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章