1. First create a table passmod.svrlist to change the password and insert two data
Svr_host #远程连接的主机host
svr_port #通过此端口连接到远程主机
svr_user #远程连接的主机用户
svr_passwd_old #远程连接的主机密码
Svr_pas Swd_new #远程连接的主机需要设置的新密码
mod_time #默认为空, after the modification succeeds, returns the modified time
mod_status enum (' 0 ', ' 1 ') #默认为 ' 0 ', the modification succeeds to ' 1 '
and authorized insert,select,update operation to passmod user ' passmod ' @ '% '
VI tb_create.py #这里用一条sql语句i, even easier to write with Shell. Mainly to learn the MySQLdb module
#!/usr/bin/python import mysqldb Try: conn=mysqldb.connect (host= ' 192.168.163.138 ', User= ' root ', passwd= ' zhang1992 ', port=3306) cur=conn.cursor ()
print ' Connect begin ... ' except: print ' connect mysql error! ' exit (1) def create_tb (cur,db_name,tb_name): cur.execute (" drop database if exists %s "%db_name) cur.execute (" create database %s "%db_name" cur.execute ("use %s"%db_name) Cur.execute ("create table %s (Svr_host varchar) not null,svr_port varchar (20) not null,svr_user varchar ( not null,svr_passwd_old varchar), svr_passwd_ New varchar ( not null,mod_time timestamp null,mod_status enum) (' 0 ', ' 1 ') default ' 0 ') "%tb_name) return 0 def user_grant (cur,db_name,tb_name): sql= "grant select,update,insert on %s.%s to '%s ' @ ' percent ' identified by ' zhang1992 '% (db_name,tb_name,db_name) create_tb (cur,db_name,tb_name) cur.execute (SQL) cur.execute (' Flush privileges ') Def main (): db_name= ' passmod ' tb_name= ' svrlist ' user_grant (cur,db_name,tb_name) cur.execute ("insert into %s.%s (svr_host,svr_port,svr_ User,svr_passwd_new,svr_passwd_old) value (' 192.168.163.138 ', ' + ', ' root ', ' zhang1992 ', ' zhang1992 ') "% ( Db_name,tb_name) cur.execute ("insert into %s.%s (Svr_host,svr_port,svr_user, Svr_passwd_new,svr_passwd_old) value (' 192.168.163.226 ', ' + ', ' root ', ' zhang1992 ', ' zhang1992 ') "% (db_ Name,tb_naMe)) cur.close () conn.commit () conn.close () print ' connect end ... ' return 0 if __name_ _== ' __main__ ': main ()
Executive tb_create.py
Python tb_create.py
650) this.width=650; "title=" 2.png "src=" http://s1.51cto.com/wyfs02/M00/85/67/ Wkiom1eipxntwkgraaafixvqsmw684.png-wh_500x0-wm_3-wmp_4-s_3848544305.png "alt=" Wkiom1eipxntwkgraaafixvqsmw684.png-wh_50 "/>
Querying the database
650) this.width=650; "title=" 1.png "src=" http://s5.51cto.com/wyfs02/M01/85/67/ Wkiol1eipwmx-hoxaaazvrbavzu089.png-wh_500x0-wm_3-wmp_4-s_3368580276.png "alt=" Wkiol1eipwmx-hoxaaazvrbavzu089.png-wh_50 "/>
2. Writing mod_passwd.py Scripts
#!/usr/bin/python import os,mysqldb,paramiko,time Try: #通过passmod用户去连接mysql, the previous script has been authorized conn= MySQLdb.connect (host= ' 192.168.163.138 ', user= ' passmod ', passwd= ' zhang1992 ', db= ' Passmod ', port=3306) except:print "Conn mysql error ..." exit (1) cur=conn.cursor () Def connect_mod (svrlist): # Through the Paramiko module to telnet to the host, and modify the password Cli=paramiko. Sshclient () Cli.set_missing_host_key_policy (Paramiko. Autoaddpolicy ()) Try:cli.connect (Hostname=svrlist[0],port=svrlist[1],username=svrlist[2],password=svrlist[3], timeout=5) except:print "%s connect error ..."%svrlist[0] return 0 stdin,stdout,stderr= Cli.exec_command ("/bin/echo %s|/usr/bin/passwd --stdin %s" % (svrlist[4],svrlist[2])) if len (str (stderr.read ()))!=0:return 0 cli.close () return 1 def main (): Cur.execute ("select  ; * from svrlist ") For row in cur.fetchall (): Svr_host=str (Row[0]) svr_port=int (row [1]) Svr_user=str (row[2]) svr_passwd_old=str (row[3]) svr_passwd_new=str (row[4]) #只有mod_status为0 To change password #修改密码后, update line, modify Mod_st Atus=1,mod_time is the current modification time if int (row[6]) ==0:svrlist= (svr_host,svr_port,svr_user,svr_passwd_old,svr_passwd_new ) Modpass=connect_mod (svrlist) #修改成功, then Modpass is 1 if modpass==1:print svrlist[0], ":", svrlist[2], "Suc Cessful "Mod_time=time.strftime ("%y-%m-%d %h:%m:%s ", Time.localtime (Time.time ())) Cur.execute (" update svrlist set mod_status= ' 1 ', mod_time= '%s ' where svr_host= '%s ' and svr_user= '%s ' " % (Mod_time,svr_host,svr_user)) else:print svrlist[0], ":", svrlist[2], "Failed" Cur.close () conn.commit () con N.close () return 0 if __name__== ' __main__ ': Main ()
Executive mod_passwd.py
650) this.width=650; "title=" 3.png "src=" http://s4.51cto.com/wyfs02/M00/85/67/ Wkiol1eipywy9f1uaaak7nwrz7y805.png-wh_500x0-wm_3-wmp_4-s_1954264238.png "alt=" Wkiol1eipywy9f1uaaak7nwrz7y805.png-wh_50 "/>
Querying the database
650) this.width=650; "title=" 4.png "src=" http://s1.51cto.com/wyfs02/M01/85/67/wKiom1eipzLCSsB_AAAW_ Hborhm754.png-wh_500x0-wm_3-wmp_4-s_252304970.png "alt=" Wkiom1eipzlcssb_aaaw_hborhm754.png-wh_50 "/>
This article is from the "Big Waste" blog, please be sure to keep this source http://bigtrash.blog.51cto.com/8966424/1834201
Python modifies the root user password in bulk from the database