ARM40-A5应用——ARM40联网的几种应用场景配置示例

ARM40-A5应用——ARM40联网的几种应用场景配置示例

2018.9.15

  ARM40主机可以使用eth0、eth1、4G/3G、GPRS、wifi等多种形式上网,本文介绍了三种场景下的配置示例,分别是:
  2.1、应用场景一:使用ppp0连接公网,eth1用于调试,不使用eth0
  2.2、应用场景二:使用eth0连接公网,eth1用于调试,不使用ppp0
  2.3、应用场景三:有线存在则用有线eth0,有线不存在则用无线ppp0,eth1用于调试
  更多应用场景,可根据示例自行配置。

一、网络配置

  本示例:共使用3个网络:eth0、eth1、ppp0,其配置与用途分别为:
  eth0  192.168.0.250  业务  将数据传送到服务器
  eth1  192.168.6.6   调试  使用网络与PC机连接,用于 telnet 登陆
  ppp0  移动分配IP   业务  将数据传送到服务器
  eth0插有网线时,使用eth0传送数据到服务器;否则,使用ppp0传送。

1.1、eth0

  上电启动时,/etc/init.d/S40network 会读取 /etc/network/interfaces.eth0 的内容,配置eth0网口。
  interfaces.eth0 的内容示例:

# Configure eth0
MACADDRESS=28:3e:34:00:01:01
IPADDRESS=192.168.0.250
NETMASK=255.255.255.0
GATEWAY=192.168.0.1

1.2、eth1

  上电启动时,/etc/init.d/S41network 会读取 /etc/network/interfaces.eth1 的内容,配置eth1网口。
  interfaces.eth1 的内容示例:

# Configure eth1
MACADDRESS=28:3e:34:00:02:02
IPADDRESS=192.168.6.6
NETMASK=255.255.255.0
GATEWAY=192.168.6.1

1.3、ppp0

  参考《ARM40-A5应用——GPRS模块ppp拨号上网》。
  无线网络使用GPRS模块,通过ppp模式拨号上网。
  在/etc/profile 中,/etc/ppp/pon-M26-auto.sh & 用于启动GPRS模块。

logintty=$(tty|grep -c "console")
if [ $logintty -eq 1 ]; then # ssh,telnet can't get into this line
        /etc/ppp/pon-M26-auto.sh &
fi

  脚本 /etc/ppp/pon-M26-auto.sh 每60s 会 ping 223.5.5.5 和 223.6.6.6,如果不通,则重启GPRS模块。所以GPRS模块断网后,一般会在2分钟左右重新连上。

二、应用场景

2.1、应用场景一:使用ppp0连接公网,eth1用于调试,不使用eth0

  ppp连接建立后,系统自动调用/etc/ppp/ip-pre-up、/etc/ppp/ip-up等脚本,在其中一个脚本中增加路由信息即可。
  脚本中使用的参数如下所示,第4个参数是系统获得的动态ip。

#!/bin/bash
#
# Script which handles the routing issues as necessary for pppd
# Only the link to Newman requires this handling.
#
# When the ppp link comes up, this script is called with the following
# parameters
# $1 the interface name used by pppd (e.g. ppp3)
# $2 the tty device name
# $3 the tty device speed
# $4 the local IP address for the interface
# $5 the remote IP address
# $6 the parameter specified by the 'ipparam' option to pppd
#

  我们在/etc/ppp/ip-up脚本中增加路由:

route del default 
route add default gw $5 $1    # $5 the remote IP address,$1 e.g. ppp0
#route add -net default netmask 0.0.0.0 gw 192.168.254.254 ppp0
#route add default dev ppp0
#route add default gw $(ifconfig ppp0|grep P-t-P|awk '{print $3}'|cut -d: -f2) ppp0
#route add -net 192.168.6.0 netmask 255.255.255.0 dev eth1   #所有192.168.6开头的,全部走eth1

  测试:

killall pppd
killall pon-M26-auto.sh
/etc/ppp/pon-M26-auto.sh &

  然后 ping 223.5.5.5 可以看到是通过ppp0发出的数据包(ppp0的数据传输较慢,ping用时长)

root@ARM40:/# ping 223.5.5.5
PING 223.5.5.5 (223.5.5.5): 56 data bytes
64 bytes from 223.5.5.5: seq=0 ttl=51 time=1163.151 ms
64 bytes from 223.5.5.5: seq=1 ttl=51 time=387.219 ms
64 bytes from 223.5.5.5: seq=2 ttl=51 time=353.236 ms
^C
--- 223.5.5.5 ping statistics ---
4 packets transmitted, 3 packets received, 25% packet loss
round-trip min/avg/max = 353.236/634.535/1163.151 ms

  route可以看到:
这里写图片描述

2.2、应用场景二:使用eth0连接公网,eth1用于调试,不使用ppp0

  配置eth0的默认网关,在/etc/init.d/S40network 中增加::

GATEWAY=`awk '/GATEWAY/ {print $1}' /etc/network/interfaces.eth0 |cut -d '=' -f 2`

  和

  route)                                         
    if ! [ $GATEWAY = null ]; then                                       
        /sbin/route del default                                          
        /sbin/route add -net default netmask 0.0.0.0 gw $GATEWAY dev eth0
    fi                                                                   
    ;;  

  测试:
  (1)killall pon-M26-auto.sh
  (2)killall pppd
  (3)route del -net default
  (4)/etc/init.d/S40network route
  (5)ping 223.5.5.5 可以看到是通过eth0发出的数据包

root@ARM40:/# ping 223.5.5.5
PING 223.5.5.5 (223.5.5.5): 56 data bytes
64 bytes from 223.5.5.5: seq=0 ttl=50 time=9.229 ms
64 bytes from 223.5.5.5: seq=1 ttl=50 time=9.152 ms
64 bytes from 223.5.5.5: seq=2 ttl=50 time=8.889 ms
64 bytes from 223.5.5.5: seq=3 ttl=50 time=8.564 ms
^C
--- 223.5.5.5 ping statistics ---
4 packets transmitted, 4 packets received, 0% packet loss
round-trip min/avg/max = 8.564/8.958/9.229 ms

  route可以看到:
这里写图片描述
· 第2行
  IP目的地是192.168.0.0~192.168.0.255 的数据包,在eth0的网卡上传送。
· 第3行
  IP目的地是192.168.6.0~192.168.6.255 的数据包,在eth1的网卡上传送。
· 第1行(默认路由(网关))
  当主机不能在第2、3行的路由中查找到目标主机的IP地址或网络路由时,数据包就被发送到默认路由(默认网关 192.168.0.1)上。默认路由的Flags字段为G。
  即所有不符合第2、3行的IP目的地,都应该送往Gateway 192.168.0.1,这个IP地址是中间路由器接入在eth0的网卡IP地址。

2.3、应用场景三:有线存在则用有线eth0,有线不存在则用无线ppp0,eth1用于调试

  每60s检测一次eth0口的网线是否插入,若有网线,则使用有线(eth0)连接;若无网线,则使用无线(ppp0)连接。
cat /sys/class/net/eth0/carrier
  读取到的值是1,表示connect,则使用eth0访问公网;
  读取到的值是为0,表示disconnect,则使用ppp0访问公网。
  我们使用shell脚本,完成这个任务:
  建立 /etc/network/if-connect-switch.sh 文件,
touch /etc/network/if-connect-switch.sh
chmod 755 /etc/network/if-connect-switch.sh
  其内容为:

#!/bin/sh
#automatically connect to eth0 network when it is available
dns1="223.5.5.5" #aliyun dns
dns2="223.6.6.6" #aliyun dns
while true
do
if [ $(cat /sys/class/net/eth0/carrier) -eq 1 ]; then       #eth0 is link up
        ping -c 1 -w 10 -I eth0 $dns1 >/dev/null            #ping dns1,1 count,timeout is 10s
        if [ "$?" != "0" ]; then                            #ping request timed out
                ping -c 2 -w 10 -I eth0 $dns2          #ping dns2,2 count,timeout is 10s
                if [ "$?" != "0" ]; then                    #ping request timed out
                    /etc/init.d/S40network route
                    continue
                fi
        else
                procnum=`ps -ef|grep "pppd"|grep -v grep|wc -l`
                if [ $procnum -ne 0 ]; then
                    killall pon-M26-auto.sh
                    killall pppd
                fi
        fi
else
        procnum=`ps -ef|grep "pon-M26-auto.sh"|grep -v grep|wc -l`
        if [ $procnum -eq 0 ]; then
                /etc/ppp/pon-M26-auto.sh &
        fi
fi
sleep 60
done

  若eth0上有网线存在,则每60s通过eth0 ping 一次dns,如果无法ping通,则配置eth0上的默认网关;如能ping通,则kill无线连接。
  若eth0上没有网线存在,则打开无线连接。
  测试:
  (1)killall pon-M26-auto.sh
  (2)killall pppd
  (3)/etc/network/if-connect-switch.sh &
  (4)在eth0口插上网线,等待约2分钟,ping 223.5.5.5 可以看到是通过eth0发出的数据包
  (5)拔下eth0口的网线,等待约2分钟,ping 223.5.5.5 可以看到是通过ppp0发出的数据包
  (6)再次在eth0口插上网线,等待约2分钟,ping 223.5.5.5 可以看到是通过eth0发出的数据包
  若要上电自启动 /etc/network/if-connect-switch.sh,则修改/etc/profile,

logintty=$(tty|grep -c "console")
if [ $logintty -eq 1 ]; then # ssh,telnet can't get into this line
        /etc/network/if-connect-switch.sh &
fi

参考文章

  ARM40-A5应用——GPRS模块ppp拨号上网
  Linux下路由配置梳理
  https://www.cnblogs.com/kevingrace/p/6490627.html
  Shell脚本实现动态配置IP与路由:解决嵌入式Android/Linux有线和无线网卡双网共存问题
  https://blog.csdn.net/HowieXue/article/details/75937972
  linux 路由表 的一些相关资料
  http://www.cnblogs.com/gunl/archive/2010/09/14/1826234.html
  荟聚计划:共商 共建 共享 Grant

附:

(1)/etc/init.d/S40network 的内容:

#!/bin/sh                                                                                
#                                                                                        
# Start the network....                                                                
#                                                                                     
                                                                                      
MACADDR=null                                  
IPADDR=null                                                                              
NETMASK=null                                                                             
                                                                                         
if [ -e "/etc/network/interfaces.eth0" ]; then                                           
    MACADDR=`awk '/MACADDRESS/ {print $1}' /etc/network/interfaces.eth0 |cut -d '=' -f 2`
    IPADDR=`awk '/IPADDRESS/ {print $1}' /etc/network/interfaces.eth0 |cut -d '=' -f 2`
    NETMASK=`awk '/NETMASK/ {print $1}' /etc/network/interfaces.eth0 |cut -d '=' -f 2`
    GATEWAY=`awk '/GATEWAY/ {print $1}' /etc/network/interfaces.eth0 |cut -d '=' -f 2`
fi                                                                       
                                                                         
# start lo                                   
/sbin/ifconfig lo up                         
                                                          
case "$1" in                                              
  start)                                            
    echo "Starting network..."                                
#   /sbin/ifup -a                                             
                                                          
    cat /proc/cmdline |grep "nfsroot" > /dev/null                                        
    if [ $? = 0 ]; then                                                                
        exit 1                                                                        
    fi                                                                                
                                              
    if ! [ $MACADDR = null ]; then                                                       
        /sbin/ifconfig eth0 down                                                         
        /sbin/ifconfig eth0 hw ether $MACADDR                                            
    fi                                                                                   
    if ! [ $IPADDR = null ] && ! [ $NETMASK = null ]; then                               
        /sbin/ifconfig eth0 $IPADDR netmask $NETMASK                                   
    fi                                                                                
    #/sbin/ifconfig eth0 192.192.192.211 netmask 255.255.255.0                        
    /sbin/ifconfig eth0 up                                               
    ;;                                                                   
  stop)                                      
    echo -n "Stopping network..."            
#   /sbin/ifdown -a                                       
#   /sbin/ifconfig eth0 down                              
#   /sbin/ifconfig eth1 down                        
    ;;                                                        
  restart|reload)                                             
    "$0" stop                                             
    "$0" start                                            
    ;;                                                                   
  route)                                                                 
    if ! [ $GATEWAY = null ]; then                                       
        /sbin/route del default                                          
        /sbin/route add -net default netmask 0.0.0.0 gw $GATEWAY dev eth0
    fi                                                                   
    ;;                                                                   
  *)                                                          
    echo $"Usage: $0 {start|stop|restart|route}"              
    exit 1                                                    
esac                                                          
                                                                         
exit $?

猜你喜欢

转载自blog.csdn.net/vonchn/article/details/82735441