1.获取本机ip
import socket
#获得本机ip
def get_host_ip():
try:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(('8.8.8.8', 80))
ip = s.getsockname()[0]
finally:
s.close()
return ip
转载:转载自东东东 陈煜东的博客
2.发送邮件
import yagmail
'''
user_mail:发件人邮箱
password:密码(qq邮箱是授权码的密码)
smtp_server:SMTP服务器(qq:smtp.qq.com)
recipients:收件人邮箱
subject:标题
content:邮件内容
port:端口(qq:465或587)
'''
def sed_mail(user_mail,password,smtp_server,recipients,subject,content,port=465):
with yagmail.SMTP(user=user_mail,password=password,host=smtp_server,port=port) as yag:
yag.send(recipients, subject, content)
3.连接数据库
def mysql_select(m_host, m_user, m_pass, m_dbname, sql, m_port=3306):
'''
连接数据库
m_host:数据库地址
m_user:数据库user
m_pass:数据库密码
m_dbname:库
sql:sql语句
m_port:端口(默认端口3306)
'''
_con_status = True
_max_retries_count = 10 # 设置最大重试连接次数
_con_retries_count = 0 # 初始化重试连接次数
# 设置连接超时时间为3秒,建议使用数据库自己的连接超时时间
# _con_timeout = 3 # 自定义,需在连接参数中,添加
# connect_timeout=_con_timeout
while _con_status and _con_retries_count <= _max_retries_count:
try:
con = pymysql.connect(host=m_host, user=m_user, passwd=m_pass,
db=m_dbname, port=m_port, charset='utf8mb4')
cur = con.cursor()
_con_status = False # 成功则退出
if _con_retries_count > 0:
logging.error("第%d次重新连接成功" % _con_retries_count)
except Exception as e:
_con_retries_count += 1
logging.error("第%d次连接失败;sql:%s" % (_con_retries_count,sql))
logging.error("Error info: %s" % e)
# wait time
time.sleep(3)
continue
try:
cur.execute(sql)
# 将更新语句提交
con.commit()
# 获取数据库信息
get_data = cur.fetchall()
except Exception as f:
# 回执到更新前
logging.error("sql语句执行失败:\n", f)
# mail_error("sql语句执行失败!\nerr:%s" % (f))
con.rollback()
finally:
# 关闭连接
cur.close()
con.close()
# 返回信息
return get_data
4.连接服务器
def ssh_game_clothes(command, Host, Username, Password, Port=22):
'''
连接服务器
command:shell命令
Host:主机IP
Username:账号
Password:密码
Port:端口(默认22)
'''
try:
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy)
ssh.connect(Host, Port, Username, Password)
send_str = command + '\n'
stdin, stdout, stderr = ssh.exec_command(send_str)
err = stderr.readlines()
out = stdout.readlines()
except Exception as e:
logging.error(e)
# mail_error("err:%s" % (e))
finally:
ssh.close()
return out
5.修改zabbix触发器信息
def mysql_zabbix(region,host_name,in_user,is_true):
'''
开关zabbix
is_true:1关闭,0开启
'''
if region == 1:
host = ""
user = ""
passwd = ""
dbs = "zabbix"
if_true = "y"
if if_true == "y":
user_GameServer = str(in_user)+"_GameServer"
user_GatewayServer = str(in_user)+"_GatewayServer"
user_LogServer = str(in_user)+"_LogServer"
user_SessionServer = str(in_user)+"_SessionServer"
userlist = [user_GameServer,user_GatewayServer,user_LogServer,user_SessionServer]
namelist = ['GameServer','GatewayServer','LogServer','SessionServer']
for x in range(0,len(userlist)):
sql = "SELECT `triggers`.triggerid,`triggers`.expression,`triggers`.description, `items`.`name` FROM `triggers`JOIN `functions` ON functions.triggerid = `triggers`.triggerid JOIN items ON items.itemid = `functions`.itemid JOIN `hosts` ON `hosts`.hostid = items.hostid JOIN `interface` ON interface.hostid = `hosts`.hostid WHERE interface.ip = '"+host_name+"' and `triggers`.description like '%宕机' AND items.`name` = '"+userlist[x]+"'"
triggerid_gam = mysql_select(host, user, passwd, dbs, sql)
sql_up = "UPDATE triggers SET status = '%s' WHERE triggerid = %s" % (is_true,triggerid_gam[0][0])
mysql_select(host, user, passwd, dbs, sql_up)
else:
print("已退出")
python3 打包命令:pyinstaller --console --onefile kongzhi.py