Paramiko,数据库

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Paramiko,数据库相关的知识,希望对你有一定的参考价值。

Paramiko

该模块基于SSH用于连接远程服务器并执行相关操作

SSHClient

用于连接远程服务器并执行基本命令

import paramiko
  
# 创建SSH对象
ssh = paramiko.SSHClient()
# 允许连接不在know_hosts文件中的主机(第一次登陆时需要输入yes,这行代码自动输入yes)
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# 连接服务器
ssh.connect(hostname=c1.salt.com, port=22, username=rooti, password=123)
  
# 执行命令
stdin, stdout, stderr = ssh.exec_command(df)
# 获取命令结果
result = stdout.read()
  
# 关闭连接
ssh.close()

 

SSHClient 封装 Transport

 

import paramiko

transport = paramiko.Transport((hostname, 22))
transport.connect(username=rooti, password=123)

ssh = paramiko.SSHClient()
ssh._transport = transport

stdin, stdout, stderr = ssh.exec_command(df)
print stdout.read()

transport.close()

 

基于公钥连接:

import paramiko
#密钥位置 
private_key = paramiko.RSAKey.from_private_key_file(/home/auto/.ssh/id_rsa)
 
# 创建SSH对象
ssh = paramiko.SSHClient()
# 允许连接不在know_hosts文件中的主机
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# 连接服务器
ssh.connect(hostname=c1.salt.com, port=22, username=rooti, key=private_key)
 
# 执行命令
stdin, stdout, stderr = ssh.exec_command(df)
# 获取命令结果
result = stdout.read()
 
# 关闭连接
ssh.close()

 

SSHClient 封装 Transport

import paramiko

private_key = paramiko.RSAKey.from_private_key_file(/home/auto/.ssh/id_rsa)

transport = paramiko.Transport((hostname, 22))
transport.connect(username=wupeiqi, pkey=private_key)

ssh = paramiko.SSHClient()
ssh._transport = transport

stdin, stdout, stderr = ssh.exec_command(df)

transport.close()

 

SFTPClient

用于连接服务器并执行上传下载

基于用户名密码:

import paramiko
 
transport = paramiko.Transport((hostname,22))
transport.connect(username=root,password=123)
 
sftp = paramiko.SFTPClient.from_transport(transport)
# 将location.py 上传至服务器 /tmp/test.py
sftp.put(/tmp/location.py, /tmp/test.py)
# 将remove_path 下载到本地 local_path
sftp.get(remove_path, local_path)
 
transport.close()

 

基于公钥:

import paramiko
 
private_key = paramiko.RSAKey.from_private_key_file(/home/auto/.ssh/id_rsa)
 
transport = paramiko.Transport((hostname, 22))
transport.connect(username=wupeiqi, pkey=private_key )
 
sftp = paramiko.SFTPClient.from_transport(transport)
# 将location.py 上传至服务器 /tmp/test.py
sftp.put(/tmp/location.py, /tmp/test.py)
# 将remove_path 下载到本地 local_path
sftp.get(remove_path, local_path)
 
transport.close()

 

 

数据库

python mysql API

插入数据:

 

import MySQLdb
  
conn = MySQLdb.connect(host=127.0.0.1,user=root,passwd=1234,db=mydb)
  
cur = conn.cursor()
  
reCount = cur.execute(insert into UserInfo(Name,Address) values(%s,%s),(alex,usa))
# reCount = cur.execute(‘insert into UserInfo(Name,Address) values(%(id)s, %(name)s)‘,{‘id‘:12345,‘name‘:‘wupeiqi‘})
  
conn.commit()
  
cur.close()
conn.close()
  
print(reCount)

批量插入:

import MySQLdb

conn = MySQLdb.connect(host=127.0.0.1,user=root,passwd=1234,db=mydb)

cur = conn.cursor()

li =[
     (alex,usa),
     (sb,usa),
]
reCount = cur.executemany(insert into UserInfo(Name,Address) values(%s,%s),li)

conn.commit()
cur.close()
conn.close()

print(reCount)

 

删除数据:

import MySQLdb
 
conn = MySQLdb.connect(host=127.0.0.1,user=root,passwd=1234,db=mydb)
 
cur = conn.cursor()
 
reCount = cur.execute(delete from UserInfo)
 
conn.commit()
 
cur.close()
conn.close()
 
print(reCount)

 

修改数据:

import MySQLdb
 
conn = MySQLdb.connect(host=127.0.0.1,user=root,passwd=1234,db=mydb)
 
cur = conn.cursor()
 
reCount = cur.execute(update UserInfo set Name = %s,(alin,))
 
conn.commit()
cur.close()
conn.close()
 
print(reCount)

 

查询数据:

import MySQLdb
 
conn = MySQLdb.connect(host=127.0.0.1,user=root,passwd=1234,db=mydb)
cur = conn.cursor()
 
reCount = cur.execute(select * from UserInfo)
 
print(cur.fetchone())
print(cur.fetchone())
cur.scroll(-1,mode=relative)
print(cur.fetchone())
print(cur.fetchone())
cur.scroll(0,mode=absolute)
print(cur.fetchone())
print(cur.fetchone())
 
cur.close()
conn.close()
 
print(reCount)
 
 
 
# ############################## fetchall  ##############################
 
import MySQLdb
 
conn = MySQLdb.connect(host=127.0.0.1,user=root,passwd=1234,db=mydb)
#cur = conn.cursor(cursorclass = MySQLdb.cursors.DictCursor)
cur = conn.cursor()
 
reCount = cur.execute(select Name,Address from UserInfo)
 
nRet = cur.fetchall()
 
cur.close()
conn.close()
 
print reCount
print nRet
for i in nRet:
    print(i[0],i[1])

以上是关于Paramiko,数据库的主要内容,如果未能解决你的问题,请参考以下文章

Paramiko模块,用python代码连接服务器执行命令

使用paramiko执行远程linux主机命令

python之资产采集(paramiko模块)

如何使用 Python 和 Paramiko 创建 SSH 隧道?

paramiko登陆主机代码

paramiko模块,线程,进程