#!/usr/bin/python
import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect("172.16.1.3",22,"root","333333")
stdin, stdout, stderr = ssh.exec_command("mysql -uroot -p123456 -Dmysql -e 'select user from user'")
print stdout.readlines()
ssh.close()
实例2:用MySQLdb模块 *** 作MySQL数据库非常方便
#!/usr/bin/python
import os, sys
import MySQLdb
try:
conn MySQLdb.connect(host='172.16.1.3',user='root',passwd='123456’,db='address')
except Exception,e:
print e
sys.exit()
cursor=conn.cursor()
sql=’insert into address(name, address) values(%s, %s)’
value=((“zhangsan”,”haidian”),(“lisi”,”haidian”))
try
cursor.executemany(sql,values)
except Exception, e:
print e
sql=”select * from address”
cursor.execute(sql)
data=cursor.fetchall()
if data
for x in data:
print x[0],x[1]
cursor.close()
conn.close()
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)