I am retrieving all incoming user IP adress from my server, I have retrieved the adresses and wrote them to a file. I have also filtered the ip adresses from the file and retrieved them to a variable, ip. However, when I am unable to upload the ip adresses to the mysql database.
So far I have this but it does not work.
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)
I have the tables set to the following:
user = longtext
ip = blob
Error Given:
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")
Sample IP Adresses Look at Just the IP and the USer:
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
the ,
from after VALUES,
is invalid
When you receive a syntax error you only need to look at where the error points to. This error can be confusing because it doesn't give you the line in your script but instead inside mysql at the first statmeent you execute which is your insert statement.
For your second issue in your comment:
you should be using parameterization so something like:
VALUES (?, ?), (ip, user)
should work
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')))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.