我的 python 代碼不起作用。我得到一個只有成功 mysql 連接的輸出。我想列印組 ID、主機名和其他變數。我得到的唯一輸出是
('Connected to MySQL Server version ', u'5.7.36-0ubuntu0.18.04.1') ("You're connected to database:")
我無法列印組 ID 或其他任何內容。我是python的新手:(
import os
import mysql.connector
import json
execfile("/home/manager/test/mysqlconnector.py")
active_ip = ""
hostname = ""
group_id = 0
def my_funciton():
query = "select value_oid from snmp_trap where name_oid = '1.3.6.1.4.1.14823.2.3.3.1.200.1.17.0'"
cursor = connection.cursor(dictionary=True)
cursor.execute(query)
mac = cursor.fetchone()
mac_string = mac.values()
mac_str = json.dumps(mac_string)
mac_ = mac_str.replace(':','')
mac_ = mac_.replace('"','')
mac_ = mac_.replace(']','')
mac_ = mac_.replace('[','')
return mac_
active_mac = my_function()
query = "select epp_active_ip, epp_hostname, epp_group_id from epp_inventory where epp_active_mac = 'active_mac.upper()'"
cursor = connection.cursor(dictionary=True)
cursor.execute(query)
rows = cursor.fetchall()
#active_ip = ""
#hostname = ""
#group_id = 0
for row in rows:
active_ip = row["epp_active_ip"]
hostname = row["epp_hostname"]
group_id = row["epp_group_id"]
print(group_id)
query = "select wmic_id from group_wmic where group_id = " str(group_id)
cursor = connection.cursor(dictionary=True)
cursor.execute(query)
wmic_ids = cursor.fetchall()
for row in wmic_ids:
query = "select command_line from wmic_commands where id = " row["wmic_id"]
cursor = connection.cursor(dictionary=True)
cursor.execute(query)
command_line = cursor.fetchone()
os.system(command_line)
os.system("ls -al")
#os.system(command)
my_funciton()
uj5u.com熱心網友回復:
除了命名和縮進問題,你應該真正修復,因為它會讓你的代碼成為一場噩夢,需要維護——問題很簡單:
考慮:
def some_function():
print('this prints')
return
print('this does not')
您的代碼有完全相同的問題。在您的 function 中my_funciton,您有以下行:
return mac_
之后什么都不會執行。您需要將 return 陳述句放在函式代碼中您期望它實際回傳的位置。您不能將它放在任何地方并期望該函式執行其余代碼。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/409070.html
標籤:
上一篇:sql將約束從唯一更改為普通文本
