安裝: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
安裝 paramiko
tar -zxvf paramiko-1.7.4.tar.gz
cd paramiko-1.7.4
- #!/usr/local/bin/python
- import string
- import os
- import paramiko
- #設定需要修改的密碼和使用者名
- setpasswd="222222"
- setuser="root"
- #讀配置檔案(IP,端口,使用者名,密碼),以逗号分割的字元串,LINUX格式的回車
- #192.168.1.118,6868,root,111111
- filepwd="./user.txt"
- #讀配置檔案,傳回IP,端口,使用者名,密碼
- def get_config_file(lines):
- ip="192.168.1.100"
- port=6000
- user="root"
- password="111111"
- temp_str=string.split(lines,',') #逗号分割
- ip=temp_str[0]
- port=int(temp_str[1])
- user=temp_str[2]
- password=temp_str[3]
- password=string.split(password,'\n')
- password = password[0]
- return(ip,port,user,password)
- #修改使用者名和密碼
- def connect_modify(ip_port_user_pwd):
- client = paramiko.SSHClient()
- client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
- try:
- client.connect(ip_port_user_pwd[0],ip_port_user_pwd[1],ip_port_user_pwd[2],ip_port_user_pwd[3], timeout=5)
- # print ip_port_user_pwd[0],ip_port_user_pwd[1],ip_port_user_pwd[2],ip_port_user_pwd[3]
- except:
- print ip_port_user_pwd[0],"network not connect"
- return 0
- stdin, stdout, stderr = client.exec_command("/bin/echo %s|/usr/bin/passwd --stdin %s"%(setpasswd,setuser))
- client.close()
- return 1
- def main():
- print"nn","setpassword",setpasswd
- f=open(filepwd,"r")
- print filepwd,"not open ip and passwd file"
- for lines in f.readlines():
- #print lines
- readline=string.split(lines,'\n')
- if len(lines)>20:
- ip_port_user_pwd = get_config_file(lines)
- re=connect_modify(ip_port_user_pwd)
- if re ==1 :
- print ip_port_user_pwd[0],"OK"
- #else
- f.close()
./user.txt檔案的生成按自己要求寫!(如下,如果windows下寫的複制到程式的目錄中,dos2unix指令改一下回事符喲)
192.168.1.110,16000,root,111111
192.168.1.111,16000,root,111111
./xxxxxxxxxxx.py