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)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章