Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Awk a string followed by write to MySQL? 1

Status
Not open for further replies.

paulobrads

Programmer
Jul 13, 2006
28
GB

I have tcpdump piping to an awk command and I want to write it to a MySQL database.

I´m not sure the best way of going about this, through shell scripting or perl. A constant connection to the MySQL db will be required as tcpdump is constantly listening on the wire as the piping takes place.

The shell script is currently:
Code:
tcpdump -i eth1 -s 1500 | awk '{print $NF}'

Any ideas?

Cheers.
 
Hi

Why not simply :
Code:
tcpdump -i eth1 -s 1500 | awk '{print "insert into tablename (fieldname) values (" $NF ");"}' | mysql mysqlparameters
Note that you may need to flush the output after each write using [tt]fflush()[/tt]. ( [tt]gawk[/tt] extension ). Or use [tt]perl[/tt] instead. See the discussion in your previous thread, thread822-1483881 .

Feherke.
 
I´m worried that I may need to write to the db quite regularly (up to a number of times a second) and creating a new connection to MySQL every time doesn´t feel very nice.
 
Now you mention it, that does look quite good, however are we missing some sort of escape character?

Here is my shell:

Code:
tcpdump -i eth1 -s 1500 port snmp | awk '{print "insert into oids (OID) values ( " $NF " );"}' | mysql -u myuser -pmypass snmp_OIDs

And I get the error:

Code:
ERROR 1064 (42000) at line 1: 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 '.1 )' at line 1

'.1' is what I´m expecting the value of $NF to equal but why the syntax issues?

Cheers.
 

Maybe you need quotes?
Code:
tcpdump -i eth1 -s 1500 port snmp | awk '{print "insert into oids (OID) values (\"" $NF "\" );"}' | mysql -u myuser -pmypass snmp_OIDs
[noevil]




----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 
Yup - that's exactly it, cheers.

Now any ideas how to cope when the $NF is a string containing double quotes " ?

The MySQL command messes up because it gets terminated early.

Cheers
 
how to cope when the $NF is a string containing double quotes
Code:
tcpdump -i eth1 -s 1500 port snmp | awk "{print \"insert into oids (OID) values ('\" \$NF \"');\"}" | mysql -u myuser -pmypass snmp_OIDs

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top