您尚未登录。

楼主 #1 2020-11-25 08:38:11

redstar
会员
注册时间: 2020-08-30
已发帖子: 2
积分: 2

请问嵌入式linux下有没有这样的软件,可以根据/etc下面某文件配置,自动检测网线是否插入,是否使用静态ip/动态获取ip

请问嵌入式linux下有没有这样的软件,可以根据/etc下面某文件配置,自动检测网线是否插入,是否使用静态ip/动态获取ip。

就像Ubuntu的图形界面那样

离线

#2 2020-11-25 09:33:49

varzhou
会员
注册时间: 2020-07-14
已发帖子: 104
积分: 3

Re: 请问嵌入式linux下有没有这样的软件,可以根据/etc下面某文件配置,自动检测网线是否插入,是否使用静态ip/动态获取ip

你没搞懂网络驱动吧,好好去看看网卡驱动就知道了

离线

#3 2020-11-25 10:18:44

raspberryman
会员
注册时间: 2019-12-27
已发帖子: 503
积分: 465

Re: 请问嵌入式linux下有没有这样的软件,可以根据/etc下面某文件配置,自动检测网线是否插入,是否使用静态ip/动态获取ip

redstar 说:

请问嵌入式linux下有没有这样的软件,可以根据/etc下面某文件配置,自动检测网线是否插入,是否使用静态ip/动态获取ip。

就像Ubuntu的图形界面那样

我觉得应该可行

busybox自带的配置

开机自动执行脚本初始化网络 /etc/init.d/S40network

#!/bin/sh
#
# Start the network....
#

# Debian ifupdown needs the /run/network lock directory
mkdir -p /run/network

case "$1" in
  start)
        printf "Starting network: "
        /sbin/ifup -a
        [ $? = 0 ] && echo "OK" || echo "FAIL"
        ;;
  stop)
        printf "Stopping network: "
        /sbin/ifdown -a
        [ $? = 0 ] && echo "OK" || echo "FAIL"
        ;;
  restart|reload)
        "$0" stop
        "$0" start
        ;;
  *)
        echo "Usage: $0 {start|stop|restart}"
        exit 1
esac

exit $?

其中 /sbin/ifup /sbin/ifdown 会自动读 /etc/network/interfaces 配置文件初始化网卡

# interface file auto-generated by buildroot

auto lo
iface lo inet loopback

auto eth0
iface eth0 inet static
      address 192.168.1.33
      netmask 255.255.255.0
      gateway 192.168.1.1

现在测试静态的是OK的, dhcp没有测试

然后接着写一个死循环脚本监测 /sys/class/net/eth0/carrier 网卡状态

发现 carrier 状态有变化,且当前等于1(插上网线),  就可以执行 /etc/init.d/S40network restart

那么可以 实现你要的功能了

离线

#4 2020-11-25 22:49:35

bitter
会员
注册时间: 2019-01-19
已发帖子: 22
积分: 20.5

Re: 请问嵌入式linux下有没有这样的软件,可以根据/etc下面某文件配置,自动检测网线是否插入,是否使用静态ip/动态获取ip

我用udev 监测事件,并自动配置

离线

#5 2020-11-25 23:34:11

歌以咏志
会员
注册时间: 2019-09-21
已发帖子: 219
积分: 210

Re: 请问嵌入式linux下有没有这样的软件,可以根据/etc下面某文件配置,自动检测网线是否插入,是否使用静态ip/动态获取ip

bitter 说:

我用udev 监测事件,并自动配置

请教具体怎么操作呢?

离线

#6 2020-11-26 00:25:33

powerpan
会员
注册时间: 2019-05-05
已发帖子: 52
积分: 45

Re: 请问嵌入式linux下有没有这样的软件,可以根据/etc下面某文件配置,自动检测网线是否插入,是否使用静态ip/动态获取ip

netlink

离线

#7 2020-11-26 00:34:13

raspberryman
会员
注册时间: 2019-12-27
已发帖子: 503
积分: 465

Re: 请问嵌入式linux下有没有这样的软件,可以根据/etc下面某文件配置,自动检测网线是否插入,是否使用静态ip/动态获取ip

powerpan 说:

netlink

请教有操作方法吗?

离线

#8 2020-11-26 08:38:27

smartcar
会员
注册时间: 2018-02-19
已发帖子: 735
积分: 735

Re: 请问嵌入式linux下有没有这样的软件,可以根据/etc下面某文件配置,自动检测网线是否插入,是否使用静态ip/动态获取ip

https://github.com/ccrisan/motionpie/blob/master/board/common/overlay/etc/init.d/S40network

#!/bin/sh

mkdir -p /var/lib/dhcp
dh_conf="/var/cache/dhclient.conf"
sys_static_conf="/etc/static_ip.conf"
static_conf="/data/etc/static_ip.conf"
watch_conf="/data/etc/watch.conf"

link_watch=yes
link_watch_timeout=20
ip_watch=yes
ip_watch_timeout=40

link_nego_timeout=10
eth=eth0
wlan=wlan0

if [ -f $watch_conf ]; then
    source $watch_conf
fi

if [ -f $sys_static_conf ] && ! [ -f $static_conf ]; then
    mkdir -p $(dirname $static_conf)
    cp $sys_static_conf $static_conf
fi

test -r $static_conf && source $static_conf

watch_eth() {
    count=0
    while true; do
        sleep 5
        if mii-tool $eth 2>&1 | grep "link ok" > /dev/null; then
            count=0
        else
            if [ $count -lt $link_watch_timeout ]; then
                count=$(($count + 5))
                logger -t ethernet -s "disconnected"
            else
                logger -t ethernet -s "disconnected for $link_watch_timeout seconds, rebooting"
                reboot
            fi
        fi
    done
}

watch_ip() {
    iface=$1
    count=0
    while true; do
        sleep 5
        if ip addr show dev $iface | grep inet &>/dev/null; then
            count=0
        else
            if [ $count -lt $ip_watch_timeout ]; then
                count=$(($count + 5))
                logger -t network -s "$iface has no IP address"
            else
                logger -t network -s "$iface had no IP address for $ip_watch_timeout seconds, rebooting"
                reboot
            fi
        fi
    done
}

start_lo() {
    ifconfig lo up
}

start_wlan() {
    if ! ifconfig $wlan >/dev/null 2>&1; then
        echo "$wlan: no device"
        return
    fi

    if [ "$(cat /sys/class/net/$wlan/carrier 2>/dev/null)" != "1" ]; then
        echo "$wlan: no link"
        return
    fi

    if [ -n "$static_ip" ]; then
        echo "$wlan: setting static IP to $static_ip"
        ifconfig $wlan $static_ip up
        static_ip="" # won't be used again
    else
        echo "$wlan: starting dhclient"
        dhclient -cf "$dh_conf" $wlan
    fi

    if [ "$ip_watch" == "yes" ]; then
        watch_ip $wlan &
    fi
}

start_eth() {
    # wait up to 3 seconds for driver
    count=0
    while ! ifconfig $eth >/dev/null 2>&1; do
        sleep 1
        count=$(($count + 1))
        if [ $count -ge 3 ]; then
            echo "$eth: no device"
            return
        fi
    done

    # bring it up
    ifconfig $eth up

    # wait up to 3 seconds for operstate
    count=0
    while [ "$(cat /sys/class/net/$eth/operstate 2>&1)" == "unknown" ]; do
        sleep 1
        count=$(($count + 1))
        if [ $count -ge    3 ]; then
            echo "$eth: no link"
            return
        fi
    done

    # wait up to link_nego_timeout seconds for link
    count=0
    while [ "$(cat /sys/class/net/$eth/carrier 2>&1)" != "1" ]; do
        sleep 1
        count=$(($count + 1))
        if [ $count -ge $link_nego_timeout ]; then
            echo "$eth: no link"
            return
        fi
    done

    if [ -n "$static_ip" ]; then
        echo "$eth: setting static IP to $static_ip"
        ifconfig $eth $static_ip up
        static_ip="" # won't be used again
    else
        echo "$eth: starting dhclient"
        dhclient -cf "$dh_conf" $eth
    fi

    if [ "$link_watch" == "yes" ]; then
        watch_eth &
    fi

    if [ "$ip_watch" == "yes" ]; then
        watch_ip $eth &
    fi
}

start() {
    hostname=$(hostname)
    echo "send host-name = \"$hostname\";" > /var/cache/dhclient.conf

    start_lo
    start_wlan
    
    # if wifi or ppp connection configured, start eth in background
    ssid=$(cat /data/etc/wpa_supplicant.conf 2>&1 | grep ssid | grep -v scan_ssid | cut -d '"' -f 2)
    if [ -n "$ssid" ] || [ -r /data/etc/ppp/modem ]; then
        start_eth &>/dev/null &
    else
        start_eth
    fi

    if [ -n "$static_gw" ]; then
        echo "setting static gateway to $static_gw"
        ip route add default via $static_gw
    fi

    if [ -n "$static_dns" ]; then
        echo "setting static DNS server to $static_dns"
        echo "nameserver $static_dns" > /etc/resolv.conf
    fi

    # print the current network configuration
    ifconfig -a
}

case "$1" in
    start)
        echo -n "Starting network: "
        start
        echo "done"
        ;;

    stop)
        echo -n "Stopping network: "
        killall dhclient
        ps | grep S40network | grep -v $$ | grep -v grep | tr -s ' ' | sed -e 's/^\s//' | cut -d ' ' -f 1 | xargs -r kill
        echo "done"
        ;;

    restart|reload)
        "$0" stop
        "$0" start
        ;;

    *)
        echo "Usage: $0 {start|stop|restart}"
        exit 1
esac

exit $?

这个脚本不错,可以试一试

离线

#9 2020-11-26 10:06:59

shaoxi2010
会员
注册时间: 2019-06-13
已发帖子: 363
积分: 312

Re: 请问嵌入式linux下有没有这样的软件,可以根据/etc下面某文件配置,自动检测网线是否插入,是否使用静态ip/动态获取ip

有个专门做这个的程序叫ifplugd,busybox内提供了,可配合ifup一起,很符合楼主要求

离线

#10 2020-11-30 16:06:28

马前卒
会员
注册时间: 2020-08-08
已发帖子: 60
积分: 57

Re: 请问嵌入式linux下有没有这样的软件,可以根据/etc下面某文件配置,自动检测网线是否插入,是否使用静态ip/动态获取ip

@raspberryman

https://github.com/tpetazzoni/buildroot/commit/61a5dd332968286a4abe9b1ec0302cf3d334910d

board/stmicroelectronics/stm32mp157-dk/overlay/etc/network/interfaces

auto lo
iface lo inet loopback

auto eth0
iface eth0 inet static
      address 192.168.42.2
      netmask 255.255.255.0

很多板子都是确实是这样配置的。

离线

页脚

工信部备案:粤ICP备20025096号 Powered by FluxBB

感谢为中文互联网持续输出优质内容的各位老铁们。 QQ: 516333132, 微信(wechat): whycan_cn (哇酷网/挖坑网/填坑网) service@whycan.cn