What if your provider is Akado?

    Examples of scripts that simplify life are given under the habrakat :)

    Part 1. Verification of the received address


    At one time, Akado had problems with pppoe authorization.
    And since when loading the system some of the daemons were tied to the received ip address, it was necessary to somehow automate the system boot process and eliminate the downtime situation.
    The idea was to check the address received from the provider after starting pppd.
    The script itself is as follows: and added to /etc/rc.d/ppp. If it was not possible to get the required address, then we send the machine to reboot. Why not 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 stop, and I really did not want to complicate the design.
    i=0
    pppip=""
    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






    Part 2. Connection Check


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

    I want to make a reservation that a packet is flying next. way:
    • for the work of the local network, the address issued from the dhcp server of the provider is used;
    • pppoe is used to go outside.

    The script runs on the crown with a period of 10 minutes and if the ping is unsuccessful to the internal addresses, then we try to get the address from dhcp, to the external ones - we 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 = ;
        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 ​​for checked internal resources (see the definition of the @check_ips array).
    In /var/log/ifaces.log we can track problems.

    Of course, you could remove the use of the Touch module and describe part of the operations differently, but at the time of writing I wanted to do just that :)

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

    Also popular now: