0

我正在从我的服务器检索所有传入的用户 IP 地址,我已经检索了这些地址并将它们写入一个文件。我还从文件中过滤了 IP 地址并将它们检索到变量 ip。但是,当我无法将 ip 地址上传到 mysql 数据库时。

到目前为止,我有这个,但它不起作用。

with open("netstat.txt", 'rb') as f:
    for row in f.readlines()[1:]:
        columns = row.split()
        if len(columns) > 2:
            ip = row.split()[1]
            user = row.split()[2]
            ip = ip.strip('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')
            con = mdb.connect('server', 'user', 'pass', 'db');

            with con:
                cur = con.cursor()
                cur.executemany("INSERT INTO Juliet_Data(ip,user) VALUES,(%s,%s)" % ip,user)

我将表格设置为以下内容:

user = longtext
ip = blob

给出的错误:

    Traceback (most recent call last):
   File "C:\Documents and Settings\finance\Desktop\Server 
Monitoring\System Load Tracker\User Tracking\netstat.py", line 32, in 
<module>
     cur.execute("INSERT INTO Juliet_Data(ip) VALUES,(%s)" % ip)
   File "C:\Python27\lib\site-packages\MySQLdb\cursors.py", line 174, in 
execute
     self.errorhandler(self, exc, value)
   File "C:\Python27\lib\site-packages\MySQLdb\connections.py", line 36, 
in defaulterrorhandler
     raise errorclass, errorvalue
_mysql_exceptions.ProgrammingError: (1064, "You have an error in your 
SQL syntax; check the manual that corresponds to your MySQL server 
version for the right syntax to use near '()' at line 1")

示例 IP 地址仅查看 IP 和用户:

 0.0.0.0:22             newjulietleft:0        LISTENING
   TCP    0.0.0.0:135            newjulietleft:0        LISTENING
   TCP    0.0.0.0:445            newjulietleft:0        LISTENING
   TCP    0.0.0.0:1311           newjulietleft:0        LISTENING
   TCP    0.0.0.0:3306           newjulietleft:0        LISTENING
   TCP    0.0.0.0:3389           newjulietleft:0        LISTENING
   TCP    0.0.0.0:5800           newjulietleft:0        LISTENING
   TCP    0.0.0.0:5900           newjulietleft:0        LISTENING
   TCP    0.0.0.0:47001          newjulietleft:0        LISTENING
   TCP    0.0.0.0:49152          newjulietleft:0        LISTENING
   TCP    0.0.0.0:49153          newjulietleft:0        LISTENING
   TCP    0.0.0.0:49154          newjulietleft:0        LISTENING
   TCP    0.0.0.0:49158          newjulietleft:0        LISTENING
   TCP    0.0.0.0:49170          newjulietleft:0        LISTENING
   TCP    10.22.22.22:139        newjulietleft:0        LISTENING
   TCP    10.22.22.22:3389       gregk:17415            ESTABLISHED
   TCP    10.22.22.22:49156      superman:microsoft-ds  ESTABLISHED
   TCP    10.22.22.22:49160      juliet:microsoft-ds    ESTABLISHED
   TCP    10.22.22.22:49164      pong:microsoft-ds      ESTABLISHED
   TCP    10.22.22.22:49165      ftpserve:microsoft-ds  ESTABLISHED
   TCP    10.22.22.22:50376      dbmlog:3306            ESTABLISHED
   TCP    10.22.22.22:50377      hulk:microsoft-ds      ESTABLISHED
   TCP    10.22.22.22:50383      julietz:netbios-ssn    TIME_WAIT
   TCP    10.22.22.22:50384    
4

2 回答 2

3

,之后的VALUES,无效

当您收到语法错误时,您只需要查看错误指向的位置。此错误可能会令人困惑,因为它不会在脚本中为您提供该行,而是在您执行的第一个 statmeent 中的 mysql 中,这是您的插入语句。

对于您评论中的第二个问题:

您应该使用参数化,例如:

VALUES (?, ?), (ip, user)

应该管用

于 2012-07-17T20:24:29.137 回答
0
cur.executemany("INSERT INTO Julient_Data (ip,user) VALUES (%s,%s)", ((ip, user),)) 

cur.executemany("INSERT INTO Julient_Data (ip,user) VALUES (%s,%s)", ((ip, user),('192.168.1.1', 'test1')))
于 2014-08-05T02:04:45.770 回答