请问嵌入式linux下有没有这样的软件,可以根据/etc下面某文件配置,自动检测网线是否插入,是否使用静态ip/动态获取ip。
就像Ubuntu的图形界面那样
离线
你没搞懂网络驱动吧,好好去看看网卡驱动就知道了
离线
请问嵌入式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
那么可以 实现你要的功能了
离线
我用udev 监测事件,并自动配置
离线
我用udev 监测事件,并自动配置
请教具体怎么操作呢?
离线
netlink
离线
netlink
请教有操作方法吗?
离线
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 $?
这个脚本不错,可以试一试
离线
有个专门做这个的程序叫ifplugd,busybox内提供了,可配合ifup一起,很符合楼主要求
离线
@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
很多板子都是确实是这样配置的。
离线