9-Openwrt config network

1、Network配置

https://wiki.openwrt.org/doc/uci/network

在openwrt中network的默认值一般在/package/base-files/files/bin/config_generate里面设置,如下:

    case "$protocol" in
        static)
            local ipad
            case "$1" in
                lan) ipad=${ipaddr:-"192.168.1.1"} ;;
                *) ipad=${ipaddr:-"192.168.$((addr_offset++)).1"} ;;
            esac

            netm=${netmask:-"255.255.255.0"}

            uci -q batch <<-EOF
                set network.$1.proto='static'
                set network.$1.ipaddr='$ipad'
                set network.$1.netmask='$netm'
                set network.$1.ip6assign='60'
            EOF
        ;;

        dhcp)
            # fixup IPv6 slave interface if parent is a bridge
            [ "$type" = "bridge" ] && ifname="br-$1"

            uci -q batch <<-EOF
                set network.$1.proto='dhcp'
                delete network.${1}6
                set network.${1}6='interface'
                set network.${1}6.ifname='$ifname'
                set network.${1}6.proto='dhcpv6'
            EOF
        ;;

2、Network拨号

有两种拨号方式,一种是使用pppd拨号,使用开源包如ppp-2.4.7。

2.1、pppd手动拨号

这时候就是使用pppd调用的配置文件来生效的,如pppd call sim0_pppdial,sim0_pppdial位于/etc/ppp/peers/下面。

1.sim0_pppdial

root@OpenWrt:/# cat etc/ppp/peers/sim0_pppdial
/dev/ttyS0
115200
nocrtscts
nocdtrcts
#nobsdcomp
#+pap
modem
#noauth
#auth
#user card
#nodetach
#debug
logfile /var/sim0_ppplog
usepeerdns
defaultroute
noipdefault
0.0.0.0:0.0.0.0
ipcp-accept-remote
noccp
#persist
lock
connect "chat -s -v -f /etc/ppp/peers/sim0_pppup"

2.sim0_pppup

root@OpenWrt:/# cat etc/ppp/peers/sim0_pppup 
ABORT 'BUSY'
ABORT 'NO CARRIER'
ABORT 'ERROR'
ABORT '+CME ERROR:100'
TIMEOUT 3
"" AT
#OK ATE0V1
#OK AT+CGACT=0,1
OK AT+CGDCONT=1,"IP","3gnet"
#OK ATS0=0
OK ATDT*99#
CONNECT ''

拨号后会将拨号的log放在/var/sim0_ppplog里面,内容如下:

abort on (BUSY)
abort on (NO CARRIER)
abort on (ERROR)
abort on (+CME ERROR:100)
timeout set to 3 seconds
send (AT^M)
Device ttyS0 is locked by pid 2710
expect (OK)
^M^M
OK
 -- got it

send (AT+CGDCONT=1,"IP","3gnet"^M)
expect (OK)
^M^M
OK
 -- got it

send (ATD*99#^M)
expect (CONNECT)
^M^M
CONNECT
 -- got it

send (^M)
Serial connection established.
Using interface ppp0
Connect: ppp0 <--> /dev/ttyS0
Device ttyS0 is locked by pid 2710
Device ttyS0 is locked by pid 2710
local  IP address 10.167.107.242
remote IP address 192.168.254.254
primary   DNS address 211.136.20.203
secondary DNS address 211.136.17.107

判断拨号是否成功可以判断该文件的primary DNS address 211.136.20.203是否存在来判断,或者使用ifconfig来看是否ppp0拨号端口出现

ppp0      Link encap:Point-to-Point Protocol  
          inet addr:10.167.107.242  P-t-P:192.168.254.254  Mask:255.255.255.255
          UP POINTOPOINT RUNNING NOARP MULTICAST  MTU:1500  Metric:1
          RX packets:2938 errors:0 dropped:0 overruns:0 frame:0
          TX packets:4944 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:3 
          RX bytes:216176 (211.1 KiB)  TX bytes:377095 (368.2 KiB)
2.1、ifup后台自动拨号

另一种使用ifup wan启动,使用的是/etc/config/network下面的配置信息

cat /etc/config/network

config interface 'wan'
        option proto '3g'
        option auto '1'
        option device '/dev/ttyUSB3'
uci set network.wan.ifname=ppp0
uci set network.wan.proto=3g
uci set network.wan.username=$username
uci set network.wan.password=$password
uci set network.wan.apn=$apn
uci set network.wan.auto=1
uci set network.wan.device=/dev/ttyUSB3
uci commit
ifup wan

/etc/init.d/network restart

如果拨号成功在ifconfig下面会生成3g-wan的接口

root@OpenWrt:/# ifconfig 
3g-wan    Link encap:Point-to-Point Protocol  
          inet addr:10.171.64.120  P-t-P:10.64.64.64  Mask:255.255.255.255
          UP POINTOPOINT RUNNING NOARP MULTICAST  MTU:1500  Metric:1
          RX packets:68 errors:0 dropped:0 overruns:0 frame:0
          TX packets:95 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:3 
          RX bytes:11864 (11.5 KiB)  TX bytes:8400 (8.2 KiB)

使用ubus去读取wan口ip

root@OpenWrt:/# ubus call network.interface.wan status | grep "address" | grep -
oE '[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}'
10.171.64.120

ifup实际也是一个拨号脚本,位于/sbin/ifup,可以通过logread进行查看拨号的过程,与上面pppd拨号时指定的sim0_ppplog文件的内容类似。

如果需要自己加一个设备节点,如ppp2

config interface 'ppp2'
        option ifname 'ppp2'
        option proto '3g'
        option auto '1'
        option device '/dev/ttyUSB3'

则还需要在/etc/config/firewall里面添加ppp2的支持

config zone
        option name             wan
        list   network          'wan'
        list   network          'wan6'
        list   network          'ppp2'
        option input            REJECT
        option output           ACCEPT
        option forward          REJECT
        option masq             1
        option mtu_fix          1

最后调用ifup ppp2即可,在ifconfig下面会生成3g-ppp2的接口,

root@OpenWrt:/# ifconfig 
3g-ppp2    Link encap:Point-to-Point Protocol  
          inet addr:10.171.64.120  P-t-P:10.64.64.64  Mask:255.255.255.255
          UP POINTOPOINT RUNNING NOARP MULTICAST  MTU:1500  Metric:1
          RX packets:68 errors:0 dropped:0 overruns:0 frame:0
          TX packets:95 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:3 
          RX bytes:11864 (11.5 KiB)  TX bytes:8400 (8.2 KiB)

可以ping通自己,但上不了外网

arm开发板的网络配置MAC-IP-GW-DNS

//配置MAC
ifconfig eth0 down
ifconfig eth0 hw ether D6:53:5D:EA:1C:09  //更改MAC地址
ifconfig eth0 up

//配置IP-GW-DNS
ifconfig eth0 192.168.1.88 netmask 255.255.255.0 up  //配置IP地址
ifconfig eth0 up                                               //激活,关闭设备eth0
route add default gw  192.168.1.1                            //配置默认网关
暂时配置dns解析
echo "nameserver 192.168.1.1">> /etc/resolv.conf


ifconfig lo up
ping 127.0.0.1

ping localhost

http://blog.csdn.net/sy373466062/article/details/49021485

猜你喜欢

转载自blog.csdn.net/weixin_34101784/article/details/86808695
今日推荐