linux 联网脚本

0,使用平台:imx6sl

1,自动监测:

# cat /sbin/auto_switch_netif.sh
#/bin/sh


status_old=0
status=1
mlan0_mac=$(ifconfig mlan0 | grep 'HWaddr' | awk '{print $5}')
eth0_mac=00:53:${mlan0_mac:0-11}
ifconfig eth0 hw ether $eth0_mac
/etc/init.d/networking restart

while true

do
        if [  -f "/tmp/auto_test.sh" ]; then
            exit 0
        fi

        result=$(ethtool eth0 | grep "Link detected: yes")
        if [[ "$result" != "" ]];
        then
                echo "eth0 link"
                status=1
        else
                echo "eth0  not link"
                status=0
        fi

        if [ $status_old != $status ];
        then
          status_old=$status
          /etc/init.d/networking restart
          if [ $status -eq 1 ];
          then
              ifconfig mlan0 down
          else
              ifconfig mlan0 up
          fi

        fi
        sleep 5
done

2.联网脚本

# cat /etc/init.d/networking

#!/bin/sh -e
### BEGIN INIT INFO
# Provides:          networking
# Required-Start:    mountvirtfs $local_fs
# Required-Stop:     $local_fs
# Should-Start:      ifupdown
# Should-Stop:       ifupdown
# Default-Start:     S
# Default-Stop:      0 6
# Short-Description: Raise network interfaces.
### END INIT INFO

PATH="/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin"

[ -x /sbin/ifup ] || exit 0

check_network_file_systems() {
    [ -e /proc/mounts ] || return 0

    if [ -e /etc/iscsi/iscsi.initramfs ]; then
        echo "not deconfiguring network interfaces: iSCSI root is mounted."
        exit 0
    fi

    exec 9<&0 < /proc/mounts
    while read DEV MTPT FSTYPE REST; do
        case $DEV in
        /dev/nbd*|/dev/nd[a-z]*|/dev/etherd/e*)
            echo "not deconfiguring network interfaces: network devices still mounted."
            exit 0
            ;;
        esac
        case $FSTYPE in
        nfs|nfs4|smbfs|ncp|ncpfs|cifs|coda|ocfs2|gfs|pvfs|pvfs2|fuse.httpfs|fuse.curlftpfs)
            echo "not deconfiguring network interfaces: network file systems still mounted."
            exit 0
            ;;
        esac
    done
    exec 0<&9 9<&-
}

check_network_swap() {
    [ -e /proc/swaps ] || return 0

    exec 9<&0 < /proc/swaps
    while read DEV MTPT FSTYPE REST; do
        case $DEV in
        /dev/nbd*|/dev/nd[a-z]*|/dev/etherd/e*)
            echo "not deconfiguring network interfaces: network swap still mounted."
            exit 0
            ;;
        esac
    done
    exec 0<&9 9<&-
}

case "$1" in
start)
        echo -n "Configuring network interfaces... "
        sysctl -e -p /etc/sysctl.conf >/dev/null 2>&1
        ifup -a
        echo "done."
        ;;

stop)
        check_network_file_systems
        check_network_swap

        echo -n "Deconfiguring network interfaces... "
        ifdown -a
        echo "done."
        ;;

force-reload|restart)
        echo "Running $0 $1 is deprecated because it may not enable again some interfaces"
        echo "Reconfiguring network interfaces... "
        ifdown -a || true
        ifup -a
        echo "done."
        ;;

*)
        echo "Usage: /etc/init.d/networking {start|stop}"
        exit 1
        ;;
esac

exit 0

3、ifup、ifdown命令使用的配置文件

# cat /etc/network/interfaces

# /etc/network/interfaces -- configuration file for ifup(8), ifdown(8)

# The loopback interface
auto lo
iface lo inet loopback

# Wireless interfaces
auto mlan0
iface mlan0 inet dhcp
        wireless_mode managed
        wireless_essid any
        wpa-driver wext
        wpa-conf /etc/wpa_supplicant.conf

#iface atml0 inet dhcp

# Wired or wireless interfaces
auto eth0
iface eth0 inet dhcp
#iface eth1 inet dhcp

# Ethernet/RNDIS gadget (g_ether)
# ... or on host side, usbnet and random hwaddr
auto usb0
iface usb0 inet static
        address 192.168.99.9
        netmask 255.255.255.0

# Bluetooth networking
#iface bnep0 inet dhcp


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值