#!/usr/bin/perl
######################################################################
# changeMacinDB.pl
#
# get mac-addresses from database and change format from
# xx:xx:xx:xx:xx:xx >> to >> xxxxxxxxxxxx
#
# version 2021-05-18
#
# (c) computer2know
######################################################################
use Data::Dumper;
use strict;
use DBI;

my $dbname="mactable";
my $dbuser="root",
my $dbpwd="";
my $dbhost="localhost";


my $dbh;
$dbh = DBI->connect("DBI:mysql:$dbname;host=$dbhost", "$dbuser", "$dbpwd") || die "Could not connect to database: $DBI::errstr";
my $sth = $dbh->prepare('select id, mac from macs');
$sth->execute();
my ($id,$mac)='';
my $i=0;
my $j=0;
while(($id,$mac) = $sth->fetchrow()){
$i++;
print "$id,$mac\n";

if ($mac=~/^(..):(..):(..):(..):(..):(..)$/){
my $newmac = "$1$2$3$4$5$6";
print "new mac: $newmac\n";
my $sqlstr = 'update macs set mac=\''.$newmac.'\' where id='.$id.' ';
#print "sqlstr=$sqlstr\n";
$dbh->do($sqlstr);
$j++;
}
}

print "Summary: Number of all macs = $i. Changed mac-adresses = $j\n";

computer2know :: thank you for your visit :: have a nice day :: © 2024