#!/usr/physics/gauche/bin/gosh (use srfi-1) (use srfi-13) (use gauche.process) (use dbi) (define (process-entry atlist dbc) (if (= (length atlist) 9) (let ((match (#/\.(\d+\.\d+\.\d+\.\d+)$/ (first atlist))) (mac (string-join (take-right atlist 6) ":"))) (if match (do-sql dbc "INSERT INTO addrmap ( mac, ip ) VALUES ( '~a', '~a' );" mac (match 1))) ) ) ) (define (read-loop op dbc) (port-for-each (lambda (l) (process-entry (string-tokenize l) dbc)) (lambda () (read-line op)) ) ) (define (main args) (let ((sp (start-snmp (second args))) (dbc (dbi-connect "dbi:sqlite3:addrmap.db"))) (do-sql dbc "CREATE TABLE addrmap ( mac text, ip text );") (do-sql dbc "PRAGMA synchronous=OFF;") (do-sql dbc "PRAGMA temp_store=MEMORY;") (do-sql dbc "PRAGMA cache_size=8000;") (read-loop (process-output sp) dbc) (dbi-close dbc) (process-wait sp) ) ) (define (start-snmp hostname) (run-process (list "/usr/physics/net-snmp/bin/snmpbulkwalk" "-Os" "-c" "public" "-v" "2c" hostname "1.3.6.1.2.1.3.1.1.2") :output :pipe :error "/dev/null") ) (define (do-sql conn sql . args) (if (null? args) (dbi-execute (dbi-prepare conn sql :pass-through #t)) (dbi-execute (dbi-prepare conn (apply format #f sql (map (lambda (a) (if (string? a) (dbi-escape-sql conn a) a)) args)) :pass-through #t)) ) )