📜 ⬆️ ⬇️

What if your provider is Akado?

Under habrakat are examples of scripts that simplify life :)

Part 1. Checking the received address


At one time, Akad had problems with pppoe authorization.
And since when booting the system, some of the daemons were tied to the resulting ip address, it was necessary to somehow automate the system boot process and eliminate the idle situation.
The idea was to check the address received from the provider after running pppd.
The script itself is the following:
i=0
pppip="<my_ip>"
echo -e "waiting pppoe connection"
until [ "$i" -eq "30" ]
do
iconf=`/sbin/ifconfig tun0 | awk '/inet/ {print $2}'`
if [ "$iconf" = "$pppip" ]
then
echo -e "successful"
i="30"
break
else
i=`expr $i + 1`
echo -n "."
sleep 5
fi
done
if [ "$iconf" != "$pppip" ]
then
echo -e "pppoe not connected!\nSystem will be reboot"
reboot
fi

and added to /etc/rc.d/ppp.
If you could not get the desired address, then send the machine to reboot.
Why aren't we trying to restart the ppp daemon itself?
By the time of verification, not much has been started yet, so we exclude the version that the reboot will be long - until the processes are stopped, but I really didn’t want to complicate the design.

Part 2. Connection check


In addition to the situation described above, problems with connection loss may occur.

I want to make a reservation that the flight of packages occurs trail. in the following way:

The script runs on the crown with a period of 10 minutes and if the ping fails to the internal addresses, then we try to get the address from dhcp, to the external ones - to restart pppd.
  #! / usr / bin / perl

 use strict;
 use warnings;
 use threads;
 use Touch;
 use Net :: Ping;
 use Net :: Ifconfig :: Wrapper qw (Ifconfig);

 my @check_hosts = qw / ya.ru google.com/;
 my @check_ips = qw / 172.18.1.1 172.18.1.2/;
 my $ int_if = 're0';
 my $ ext_if = 're1';


 sub get_ips {
   my ($ iface) = @_;
   my @ifaces = ();
   my $ info = Ifconfig ('list', '', '', '');

   unless (defined ($ info -> {$ iface})) {
     die "Couldn't find ip address on $ iface \ n";
   }

   for (sort keys% {$ info -> {$ iface} {'inet'}}) {
     push (@ifaces, $ _);
   }

   if (scalar (@ifaces)> 0) {
     return \ @ifaces;
   }
   else {
     return undef;
   }
 }


 sub ping_state {
   my ($ dst_src, $ proto, $ iface) = @_;

   die "Check params in sub ping_state"
     unless (defined ($ dst_src) && ref ($ dst_src) eq 'ARRAY');

   $ proto = ''
     unless (defined ($ proto));

   my $ p = Net :: Ping-> new ($ proto);

   my $ int_ips = undef;
   $ int_ips = get_ips ($ iface)
     if (defined ($ iface));
   $ p-> bind ($ int_ips -> [0])
     if (defined ($ int_ips));

   my $ i = 0;
   for my $ sources (@ {$ dst_src}) {
     if ($ p-> ping ($ sources, 10)) {
       $ i ++;  last;
     }
   }

   $ p-> close ();
   if ($ i> 0) {return 1;  }
   else {return 0;  }
 }


 sub do_log {
   my ($ message, $ log_file) = @_;

   $ log_file = '/var/log/ifaces.log'
     unless (defined ($ log_file));

   unless (-e "$ log_file") {
     touch ($ log_file) or die ("Failed touch $ log_file");
   }

   open (LOG, ">> $ log_file");
   my ($ sec, $ min, $ hour, $ mday, $ mon, $ year) = localtime (time);
   print LOG '['.  ($ year + 1900).
             '-'.  (($ mon <10)? ('0'. $ mon): $ mon).
             '-'.  (($ mday <10)? ('0'. $ mday): $ mday). 
             ''.  (($ hour <10)? ('0'. $ hour): $ hour).
             ':'.  (($ min <10)? ('0'. $ min): $ min).
             ':'.  (($ sec <10)? ('0'. $ sec): $ sec).
             ']'.  "$ message \ n";
   close LOG;
 }

 my $ thr = threads-> create (sub {
   unless (ping_state (\ @ check_ips, 'tcp', $ int_if)) {
     do_log ("Check connect to LAN resources: failed");
     system ("dhclient $ ext_if");
   }
 });

 unless (ping_state (\ @ check_hosts, 'icmp')) {

   do_log ("Check connect to WAN resources: failed");
   opendir (RUN, '/ var / run');
   for (grep {/tun\w+\.pid/} readdir (RUN)) {
     open (PID, '/ var / run /'. "$ _");
     local $ / = undef;
     my $ pid = <PID>;
     close (PID);
     chop ($ pid);
     kill (9, $ pid);
   }
   closedir (RUN);

   system ("ppp -ddial akado");
 }

 foreach my $ t (threads-> list (threads :: all)) {
   $ t-> join ();
 } 

')
Do not forget to substitute your values ​​of the checked internal resources (see the definition of the @check_ips array).
In /var/log/ifaces.log we can track problems.

Of course, it was possible to remove the use of the Touch module and otherwise describe some of the operations, but at the time of writing I wanted to do just that :)

PS: used by FreeBSD, Perl 5.10.
Pearl 5.8 will swear to use threads-> list (threads :: all).

Source: https://habr.com/ru/post/66644/


All Articles