我正在从我的服务器检索所有传入的用户 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