#!/usr/bin/perl use strict; use DBI; use Net::Telnet::Cisco; my $sql_host = "localhost"; my $sql_login = "root"; my $sql_pass = "m0td3p4ss3"; my $sql_base = "mgmt_switch"; #Switch ident my $login = "accesspass"; my $enable = "enablepass"; #Connexion à la DB my $dbh = DBI->connect("DBI:mysql:database=$sql_base;host=$sql_host",$sql_login, $sql_pass,{'RaiseError' => 1}); #On récupère la liste des machines à installer my $server = $dbh->prepare("SELECT * from servers where installed=\"0\" "); $server->execute; while(my $ref = $server->fetchrow_arrayref()) { my @arr = @{$ref}; my $id = $arr[0]; my $hostname = $arr[1]; my $switch = $arr[2]; my $vlan = $arr[3]; my $interface = $arr[4]; my $description = $arr[5]; my $ipaddress = $arr[6]; my $installed = $arr[7]; my $session = Net::Telnet::Cisco->new(Host => "$switch", Input_log => "input.log"); $session->login(Password => "$login"); $session->enable("$enable"); $session->cmd("config t"); $session->cmd("int $interface"); $session->cmd("description * $hostname * $ipaddress * $description "); $session->cmd("switchport access vlan $vlan"); $session->cmd("switchport mode access"); $session->cmd("spanning-tree portfast"); $session->cmd("end"); $session->cmd("write memory"); $session->cmd("exit"); $session->close(); my $updated= $dbh->prepare("UPDATE `servers` SET `installed` = '1' WHERE `hostname` ='$hostname' "); $updated->execute; }