python知识点细节

1:将mysql查询出的数据转化为列表

DB=database(db_host,db_user,db_pwd,db_database)
sql='SELECT Vl_name  from vulnerability  limit 0 ,3'
res=DB.query_sql(sql)
print('元组数据:',res)
a=[ i[0] for i in res]
print('列表数据:',a)

在这里插入图片描述
2,数据库查询出来的数据
需要注意的2个地方 1个地方为时间

sql1="select tool_type,Vl_name,vl_leave,vl_introduce,vl_fix,CAST(create_time AS CHAR) AS create_time ,CAST(update_time AS CHAR ) AS update_time,bug_type from Vulnerability where Vl_name='%s'"%(pymysql.escape_string(i))
sql2="insert into Vulnerability(tool_type,Vl_name,vl_leave,vl_introduce,vl_fix, create_time ,update_time ,bug_type)VALUES %s"%a

sql1 注意问题,某些字段带有特殊符号。双单引号问题。pymysql.escape_string(i)
sql2 注意问题create_time,update_time python查询出来的格式为 datatime.dataime(xx.xxx.xxx.xx)格式,需要转换才能插入
3,python 远程登陆服务器,并且切换为root命令,同时获取返回值的结果

import paramiko
import requests
requests.packages.urllib3.disable_warnings()
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname="127.0.0.1", port=22, username='xx', password='xxxx')

# 假设以上以普通用户登录
chan = ssh_client.invoke_shell()  # 使用伪终端,默认vt100创建交互式
chan.send('su  - \n')  # 发送su 命令
time.sleep(2)
chan.send('Act@1122'+"\n")  # 发送root密码
time.sleep(2)
shell='linux命令'
chan.send(shell + "\n")
time.sleep(2)
check_result = chan.recv(9999).decode().replace("\r", "").split("\n")
check_result = check_result[len(check_result)-2]
if "root@" in check_result:
    check_result = 'none'
print(check_result)
print("------end------")

4:linux下切换程序的执行路径
适用需求,有时候通过linux命令执行的程序,涉及路径问题,容易出问题,比如导入模块等

c=os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
os.chdir(c)#改变工作执行路径
sys.path.append(c)

5 python 拼接路径

pwdpath = os.path.dirname(os.path.abspath(__file__))
jar_path=os.path.join(pwdpath,"java_word.jar")

猜你喜欢

转载自blog.csdn.net/qq_34237321/article/details/106781959