5

I have created a shell script to connect to a DateBase and INSERT IPs and MACs from dhcpd.log file by connecting to the DB only once:

#/!bin/bash
dhcpLogFile="/var/log/dhcpd.log"
NumberOfLines=$(awk '/DHCPACK/ { print $8} ' $dhcpLogFile | awk '!x[$0]++'|awk 'BEGIN{i=0}{i++;}END{print i}')
j=1
while [ $NumberOfLines -gt 0 ]
do
ip=$(awk '/DHCPACK/ { print $8} ' $dhcpLogFile | awk '!x[$0]++' |cut -f$j -d$'\n')
mac=$(awk '/DHCPACK/ { print $10} ' $dhcpLogFile | awk '!x[$0]++' |cut -f$j -d$'\n')  
let "j +=1"
let "NumberOfLines -=1"
INSERT INTO IP_MACTable (IP_Address, MAC) VALUES ('$ip','$mac');
done | mysql -u root --password='pw' MatchingDB

In MySql I have set "IP,MAC" attributes as a unique so "ip,mac" field won't be duplicated, so when It duplicated in the log of dhcp.log the DB reply with a message ERROR 1062 (23000) at line 1: Duplicate entry '192.168.1.20-00:0c:29:95:fd:10' for key 'IP_Address'
my question is : As I connect to the DB only once , sql will interpret multiple queries in one connection, but the problem is when an error occured it won't complete interpreting other queries , so is it a way to ignore an SQL error and go on to other queries?

1 Answer 1

15

Update your insert statement like this:

INSERT IGNORE INTO IP_MACTable (IP_Address, MAC) VALUES ('$ip','$mac');

Also you can force mysql to continue on error by using the --force option:

mysql -u root --password='pw' --force MatchingDB

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .