paramiko 模块
paramiko是基于SSH协议用于连接远程服务器并执行相关操作(SSHClient和 SFTPClient, 即远程连接/上传下载)的服务,使用该模块可以对远程服务器进行命令或文件操作,ansible内部的远程管理就是用paramiko实现的。
#!/usr/bin/env python3
# ^3^ coding=utf8
#
# author: superzyx
# date: 2019/08/24
# usage: paramiko module ssh connection
# import paramiko
#
# private = paramiko.RSAKey.from_private_key_file('/root/.ssh/id_rsa')
#
# # 创建加密管道
# transport = paramiko.Transport('192.168.161.3', 22)
# transport.connect(username='root',pkey=private)
#
# #创建客户端
# client = paramiko.SSHClient()
# client._transport = transport
#
# #查看根下的文件
# stdin, stdout, stderr = client.exec_command(command='ls /',timeout=1)
# print(stdout.read().decode('utf-8'))
#
# transport.close()
import paramiko
private = paramiko.RSAKey.from_private_key_file('/root/.ssh/id_rsa')
#创建管道
transport = paramiko.Transport('192.168.161.3', 22)
transport.connect(username='root', pkey=private)
#创建stfp客户端
sftp = paramiko.SFTPClient.from_transport(transport)
#传输文件
sftp.get('/etc/passwd', '/root/pp.txt')
sftp.put('paramiko_module.py', '/root/ll.txt')
transport.close()
pymysql 模块
使用pymysql第三方库连接mysql数据库,pymysql是使用纯python编写的API,操作起来方便易懂,特别是返回数据格式的支持
#!/usr/bin/env python3
# ^3^ coding=utf8
#
# author: superzyx
# date: 2019/08/24
# usage: connect to mysql server
# import pymysql
#
# client = pymysql.connect(
# host='192.168.161.3',
# port=3306,
# user='superzyx',
# password='******',
# db='Test'
# )
#
# #建立游标
# cursors = client.cursor()
# sql = 'select * from test01;'
#
# cursors.execute(sql)
# print(cursors.fetchall())
#
# client.commit()
# client.close()
import pymysql
import openpyxl
#先创建一个新的xlsx文件
file = openpyxl.Workbook('superzyx.xlsx')
file.save('superzyx.xlsx')
#写入
file = openpyxl.load_workbook('superzyx.xlsx')
sheet = file.create_sheet('log',0)
#链接mysql
client = pymysql.connect(
host='192.168.161.3',
port=3306,
user='superzyx',
passwd='*******',
db='Test'
)
cursors = client.cursor()
sql = 'select * from test01;'
cursors.execute(sql)
for item in cursors.fetchall():
sheet.append(item)
client.close()
file.save('superzyx.xlsx')
redis 模块
redis是一个开源的C语言编写,支持网络,可基于内存亦可持久化的日志型,键值对型,非关系型数据库,并提供多种语言的API,为了保证效率,数据都存储在内存中
#!/usr/bin/env python3
# ^3^ coding=utf8
#
# author: superzyx
# date: 2019/08/24
# usage: redis connection
# import redis
#
# pool =redis.ConnectionPool(host='192.168.161.34', port=6379, db=0)
# client = redis.Redis(connection_pool=pool)
#
# client.set(name='pycl', value='tt')
# client.get(name='pycl')
#
# client.mset(mapping={'name': 'superzyx', 'sex': 'm', 'phone': '110'})
# client.mget('name', 'sex', '110')
#
# client.delete('name', 'pycl', 'sex')
#
# client.hset(name='hashlist', key='name', value='superzyx')
# client.hget(name='hashlist', key='name')
#
# client.hmset(name='hashlist',mapping={'sex': 'm', 'phone': '110'})
# client.hmget('hsahlist','name', 'sex', 'phone')
# client.hdel('hashlist', 'name', 'sex')
# client.delete('hashlist')
#
# client.lpush('list01',1,1,2,3)
# client.rpush('list01', 0,2,9)
# client.lpushx('list01','sdf')
# client.rpushx('list01', 'adhaf;')
# client.linsert(name='list01', where='AFTER', refvalue=3, value='adgksf')
#
# client.lset('list01', 4, 'adajdfh')
# for a in client.lrange('list01', 0 , client.llen('list01')):
# print(a)
#
# data = client.lindex('list01', 9)
#
# client.lrem('list01', 2, 1)
# client.ltrim('list01', 0, 5)
import redis
from redis import sentinel
import logging.config
sentinel = sentinel.Sentinel([('192.168.161.34', 26379)])
logging.config.fileConfig('../log.conf')
try:
master = sentinel.discover_master('mymaster')
print(master)
except redis.sentinel.MasterNotFoundError as error:
logger = logging.getLogger('rotatehandle')
logger.error(error)
finally:
master = sentinel.discover_master('mymaster')
if master != ('192.168.161.34', 6380):
logger.error('[ERROR] change master of {}')