2
« : Мая 18, 2012, 10:51:24 am »
Сам скрипт:
#!/usr/bin/perl -w
use strict;
use warnings;
use Net::Ping::External qw(ping);
# 1 - автоматический режим переключение канала
# 2 - принудительное переключение на второй канал.
my $action = 1;
my $host_gw = "85.21.230.141"; # default gateway
my $gw = "95.104.192.36"; # backup gateway
my $now = localtime time;
my $log = "/var/log/route-change.log";
if($action == 1){
my $command = `netstat -rn | grep default`;
my @b = split('\s+',$command,3);
if (ping(hostname=>$host_gw, count=>5)){
print "host $host_gw is ok\n";
if($b[1] ne $host_gw){
if($b[1] eq ""){
`route add default $host_gw`;
}else{
`route change default $host_gw`;
open(LOG,">>$log");
print LOG "[!] $now Route change to $host_gw\n";
close(LOG);
}
}
}else{
print "host $host_gw is bad.\n";
if($b[1] ne $gw){
`route change default $gw`;
open(LOG,">>$log");
print LOG "[!] $now Route change to $gw\n";
close(LOG);
}
}
}
if($action == 2){
my $command = `netstat -rn | grep default`;
my @b = split('\s+',$command,3);
if($b[1] ne $gw){
if($b[1] eq ""){
`route add default $gw`;
}else{
`route change default $gw`;
open(LOG,">>/change_route.log");
print LOG "[!] $now Route change to $gw\n";
close(LOG);
}
}
}
А вот и ifconfig
lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> metric 0 mtu 16384
options=3<RXCSUM,TXCSUM>
inet 127.0.0.1 netmask 0xff000000
pfsync0: flags=0<> metric 0 mtu 1460
syncpeer: 224.0.0.240 maxupd: 128
pflog0: flags=141<UP,RUNNING,PROMISC> metric 0 mtu 33152
bee: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> metric 0 mtu 1500
options=3<RXCSUM,TXCSUM>
ether 00:25:90:0d:43:97
inet 10.114.4.210 netmask 0xfffffc00 broadcast 10.114.7.255
media: Ethernet autoselect (1000baseT <full-duplex>)
status: active
vlan: 10 parent interface: em1
mts: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> metric 0 mtu 1500
options=3<RXCSUM,TXCSUM>
ether 00:25:90:0d:43:97
inet 172.30.230.2 netmask 0xffffff00 broadcast 172.30.230.255
media: Ethernet autoselect (1000baseT <full-duplex>)
status: active
vlan: 20 parent interface: em1
tun0: flags=8051<UP,POINTOPOINT,RUNNING,MULTICAST> metric 0 mtu 1500
options=80000<LINKSTATE>
inet 192.168.3.1 --> 192.168.3.2 netmask 0xffffffff
Opened by PID 1754
ng0: flags=88d1<UP,POINTOPOINT,RUNNING,NOARP,SIMPLEX,MULTICAST> metric 0 mtu 1460
inet 89.179.xxx.xxx --> 85.21.230.141 netmask 0xffffffff
ng1: flags=88d1<UP,POINTOPOINT,RUNNING,NOARP,SIMPLEX,MULTICAST> metric 0 mtu 1492
inet 95.104.xxx.xxx --> 95.104.192.35 netmask 0xffffffff
ng2: flags=88d1<UP,POINTOPOINT,RUNNING,NOARP,SIMPLEX,MULTICAST> metric 0 mtu 1400
inet 192.168.2.1 --> 192.168.2.37 netmask 0xffffffff