DEV Community

Cover image for APIban-mysql client
Ameed Jamous
Ameed Jamous

Posted on

APIban-mysql client

Apiban-mysql

Introducing simple apiban-mysql project. Fetch SIP attackers IPs from APIBan REST API and save them to MySQL database table.

Download Project

git clone https://github.com/ajamous/apiban-mysql.git

Enter fullscreen mode Exit fullscreen mode

Change Directory


cd apiban-mysql/

Enter fullscreen mode Exit fullscreen mode

Install dependencies

npm install 
Enter fullscreen mode Exit fullscreen mode

Created required tables in your mysql db


CREATE TABLE "last_fetched_id" (
  "id" varchar(32) NOT NULL DEFAULT '',
  PRIMARY KEY ("id")
);

CREATE TABLE "apiban_banned_ips" (
  "ip" varchar(15) NOT NULL DEFAULT '',
  PRIMARY KEY ("ip")
);

INSERT INTO last_fetched_id(id) values('');


Enter fullscreen mode Exit fullscreen mode

Enter APIBan API key and DB Credentials


vi sync.js 

Enter fullscreen mode Exit fullscreen mode
/* validate environment */
const APIBAN_API_KEY = "XXXXXXXXXXXXXXXXX"
const APIBAN_MYSQL_SERVER = "replace_with_mysql_ip_or_host"
const APIBAN_MYSQL_PORT = 3306
const APIBAN_MYSQL_DATABASE = "replace_with_db_name"
const APIBAN_MYSQL_USER = "replace_with_mysql_user"
const APIBAN_MYSQL_PASSWORD = "replace_with_mysql_password"

Enter fullscreen mode Exit fullscreen mode

Run the script

node sync.js
Enter fullscreen mode Exit fullscreen mode

Sample output


$ node sync.js 

getting ips list...
starting from ID: undefined
fetched 250 ips, next ID 1663924065
fetched 250 ips, next ID 1663954967
fetched 250 ips, next ID 1663970136
fetched 250 ips, next ID 1663991432
fetched 250 ips, next ID 1664018643
fetched 250 ips, next ID 1664039285
fetched 250 ips, next ID 1664058709
fetched 250 ips, next ID 1664082297
fetched 250 ips, next ID 1664111965
fetched 250 ips, next ID 1664130848
fetched 250 ips, next ID 1664160433
fetched 250 ips, next ID 1664198153
fetched 250 ips, next ID 1664229896
fetched 250 ips, next ID 1664259993
fetched 250 ips, next ID 1664287578
fetched 250 ips, next ID 1664321167
fetched 250 ips, next ID 1664330768
fetched 250 ips, next ID 1664351231
fetched 250 ips, next ID 1664373761
fetched 250 ips, next ID 1664386920
saving ips list into DB...
done.
Enter fullscreen mode Exit fullscreen mode

When there is no more IPs to add

$ node sync.js

getting ips list...
starting from ID: 1664386920
no new banned IPs since last sync, exiting...
Enter fullscreen mode Exit fullscreen mode

Check your MySQL DB table for updates:

Screenshot 2022-09-30 at 1 57 22 PM

I hope you found it useful.

You may also like

Top comments (0)