python 怎样建立ssh连接 并登陆mysql
2个回答
展开全部
实例1:paramiko模块
#!/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()
#!/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()
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询