Automatically run programs to monitor network connections after Ubuntu is connected to IOT Platform

Source: Internet
Author: User
A previous article introduces how to use update-rc.d to manage the Startup Program of Ubuntu. For more information, see: update-rc.d management Ubuntu boot program prepared before an article introduced the use of update-rc.d to manage Ubuntu boot run the program, see: update-rc.d management Ubuntu boot program http://www.linuxidc.com/Linux/2013-01/77557.htm, at that time, I wanted to solve the problem by starting automatic connection to vpn at startup. However, this method may cause the vpn process to become frozen because the vpn connection is related to the NIC status, this section describes how to automatically run a custom program after the network is connected in the Debian/Ubuntu release, run scripts in the corresponding directory in/etc/network/before, after, and before, and after the network is established.
  • If-down.d -- before Network Shutdown
  • If-post-down.d -- after the network is turned off
  • If-pre-up.d-before network creation
  • If-up.d-after the network is built
Idea (1) After the network is established, we need to add a script to automatically connect to the vpn under the/etc/network/if-up.d/directory, in this way, you can connect to the vpn as long as it is in the network state.

#! /Bin/bash

# Variable definition
PATH =/sbin:/usr/sbin:/bin:/usr/bin
Config_path = "/home/wangzhengyi/cloud-1/openvpn-client.ovpn"

# Import Environment Variables
./Lib/lsb/init-functions
Export PATH = $ PATH

# Run openvpn after the network is established
Log_daemon_msg "starting client openvpn"
Nohup openvpn $ config_path

(2) Before the network is disconnected, we need to stop all vpn processes to prevent program bugs.

  1. First, let's talk about a bug where scripts in ubuntu's/etc/network/if-down.d/and/etc/network/if-post-down.d/directory won't be executed when the network is disconnected, why is it not found by google?
  2. It should be emphasized that there should be no way of thinking. It takes several hours to solve this problem, that is, the way of thinking. You can consider writing a monitoring script to check whether the network is smooth, if the network is down, kill all vpn processes. Then, the crontab runs regularly and the script code is used.

#! /Bin/bash

# Variable definition
Urls = ("www.88181.com" "www.linuxidc.com" "www.6688.cc ")
Http_code = ("200" "301" "302" "404 ")
Count =$ {# urls [*]}
Connected = 0


# Network detection
For (I = 0; I <$ count; I ++ ))
Do
Url =$ {urls [$ I]}
Result = $ (curl-o/dev/null-s-m 10-w % {http_code} $ url)
For flag in $ {http_code [*]}
Do
If [$ flag = $ result]; then
Connected = $ (expr $ connected + 1)
Fi
Done
Done

# Kill all vpn processes when the network is down
If [$ connected-eq 0]; then
Vpn_pids = $ (ps-aux | grep-I 'openvp' | grep-v 'grep' | awk '{print $2 }')
For pid in $ {vpn_pids [*]}
Do
Kill-9 $ pid
Done
Fi

Crontab scheduled run

* ***** Monitor_scripts

Postscript
You have a better method. Please leave a message and I hope to discuss it with you!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.