2012-02-16 78 views
0

我正在使用以下腳本從第三方工具中提取數據,在MySQL數據庫中創建一個表並使用結果數據填充它。該腳本貫穿始終,我可以在Python Shell窗口中看到所有請求的數據。但是,當我打開數據庫時,該表是使用列名創建的,但沒有行並且沒有數據。我搜索了一遍,發現我不需要使用'conn.commit'來獲取只是檢索數據的腳本。這是這種情況嗎?如果沒有人看到數據不填充表的另一個原因?Python MySQL數據導入

import httplib2, urllib, json, pprint, getpass, string, time, MySQLdb 

def usage(): 
    print "Usage: python26 mysql.py or ./mysql.py" 
    sys.exit(1) 

if len(sys.argv) != 1: 
    usage() 

# Connect to the database and create the tables 
conn = MySQLdb.connect (host = "localhost", 
        user = "XXXXXXXXX", 
        passwd = "XXXXXXXX") 
cursor = conn.cursor() 
cursor.execute ("DROP DATABASE IF EXISTS tenable") 
cursor.execute ("CREATE DATABASE tenable") 
cursor.execute ("USE tenable") 
cursor.execute (""" 
CREATE TABLE cumvulndata 
(
    offset   BIGINT(10), 
    pluginName  TEXT, 
    repositoryID SMALLINT(3), 
    severity  TINYINT(2), 
    pluginID  MEDIUMINT(8), 
    hasBeenMitigated TINYINT(1), 
    dnsName  VARCHAR(255), 
    macAddress  VARCHAR(40), 
    familyID  INT(4), 
    recastRisk  TINYINT(1), 
    firstSeen  DATETIME, 
    ip    VARCHAR(15), 
    acceptRisk  TINYINT(1), 
    lastSeen  DATETIME, 
    netbiosName VARCHAR(255), 
    port   MEDIUMINT(5), 
    pluginText  MEDIUMTEXT, 
    protocol  TINYINT(3) 
) 
    """) 
# 
# Security Center organizational user creds 
user = 'XXXXXXXXX' 
passwd = 'XXXXXXXX' 
url = 'https://Security Center Server/request.php' 

def SendRequest(url, headers, data): 
    http = httplib2.Http() 
    response, content = http.request(url, 
            'POST', 
            headers=headers, 
            body=urllib.urlencode(data)) 
    if 'set-cookie' in response: 
     headers['Cookie'] = response['set-cookie'] 
    return response, content 

headers = {"Content-type": "application/x-www-form-urlencoded"} 

input = {'password': passwd, 
    'username': user} 

# Convert input to login JSON 
inputjson = json.dumps(input) 

data = {"request_id": "8", 
    "module": "auth", 
    "action": "login", 
    "input": inputjson} 

# Send Login Request 
response, content = SendRequest(url, headers, data) 

# Decode JSON to python data structure 
result = json.loads(content) 

if result["error_code"] == 0: 
    print "SC4 Login Successful" 
    token = result['response']['token'] 
    print "Session Token:",token 

# Construct the cumulative vuln query JSON 
cuminput = {'tool':'vulndetails', 
    'startOffset':'0', 
    'endOffset':sys.maxint, 
    'sortField':'ip', 
    'sortDir':'asc', 
    'sourceType':'cumulative', 
    'filters': [ 
          {'filterName':'lastSeen', 
          'value':'31', 
          'operator':'<='}, 
          {"filterName":"severity", 
          "value":"1,2,3", 
          "operator":"="} 

      ]} 
cuminputjson = json.dumps(cuminput) 

# 
cumdata = {"request_id": "1", 
     "module": "vuln", 
    "action": "query", 
    "input":cuminputjson, 
     "token": token} 

# Send the cumulative JSON and then populate the table 
cumresponse, content = SendRequest(url, headers, cumdata) 
resultc = json.loads(content) 
off = 0 
    print "\nFilling cumvulndata table with vulnerabilities from the cumulative database.  Please wait..." 
for result in resultc['response']['results']: 
    off += 1 
    cursor.execute ("""INSERT INTO cumvulndata (offset,pluginName,repositoryID,severity,pluginID,hasBeenMitigated,dnsName,macAddress,familyID,recastRisk,firstSeen,ip,acceptRisk,lastSeen,netbiosName,port,pluginText,protocol) 
    VALUES 
    (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,(FROM_UNIXTIME(%s)),%s,%s,(FROM_UNIXTIME(%s)),%s,%s,%s,%s)""", (off,result["pluginName"],result["repositoryID"],result["severity"],result["pluginID"],result["hasBeenMitigated"],result["dnsName"],result["macAddress"],result["familyID"],result["recastRisk"],result["firstSeen"],result["ip"],result["acceptRisk"],result["lastSeen"],result["netbiosName"],result["port"],result["pluginText"],result["protocol"])) 


# Close the cursor and connection 
cursor.close() 
conn.close() 

print "Done!!" 
+2

你的腳本不是「只是檢索數據」 - 畢竟,你正試圖*寫*數據到你的新表中。 – Amber 2012-02-16 02:15:12

回答

0

試試這個

import httplib2, urllib, json, pprint, getpass, string, time, MySQLdb 
import sys 
def usage(): 
    print "Usage: python26 mysql.py or ./mysql.py" 
    sys.exit(1) 

if len(sys.argv) != 1: 
    usage() 

# Connect to the database and create the tables 
conn = MySQLdb.connect (host = "localhost", 
        user = "XXXXXXXXXX", 
        passwd = "XXXXXXXX") 
cursor = conn.cursor() 
cursor.execute ("DROP DATABASE IF EXISTS tenable") 
cursor.execute ("CREATE DATABASE tenable") 
cursor.execute ("USE tenable") 
cursor.execute (""" 
CREATE TABLE cumvulndata 
(
    offset   BIGINT(10), 
    pluginName  TEXT, 
    repositoryID SMALLINT(3), 
    severity  TINYINT(2), 
    pluginID  MEDIUMINT(8), 
    hasBeenMitigated TINYINT(1), 
    dnsName  VARCHAR(255), 
    macAddress  VARCHAR(40), 
    familyID  INT(4), 
    recastRisk  TINYINT(1), 
    firstSeen  DATETIME, 
    ip    VARCHAR(15), 
    acceptRisk  TINYINT(1), 
    lastSeen  DATETIME, 
    netbiosName VARCHAR(255), 
    port   MEDIUMINT(5), 
    pluginText  MEDIUMTEXT, 
    protocol  TINYINT(3) 
) 
    """) 
cursor.execute ("""INSERT INTO cumvulndata (offset,pluginName,repositoryID,severity,pluginID,hasBeenMitigated,dnsName,macAddress,familyID,recastRisk,firstSeen,ip,acceptRisk,lastSeen,netbiosName,port,pluginText,protocol) 
    VALUES 
    (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)""", ('123','plugin','10','1','12','1',"dnsName","macAddress",'15','1','2011-2-2',"ip",'9','2012-5-2',"netbiosName",'123',"pluginText","2")) 

#Commit the changes. 
conn.commit() 
cursor.close() 
conn.close() 

commit的變化,那麼你會得到插入的數據。