RT-IPC_camera/bin/vslocal/cgi-bin/netenv.conf

667 lines
13 KiB
Bash

#!/bin/sh
export PATH=$PATH:/wifi:/bin/vs/cgi-bin:/bin/vs
wifi_check_path=/etc/ipcamera/network/wifi_disconnect_status
config_path=/etc/network
tmpfs_path=/tmpfs
wifidev_config=$config_path/wifidev.conf
wifi_config=$config_path/wifi.conf
wifi_config_tmp=$tmpfs_path/wifi.conf
wifi_config_test=$tmpfs_path/wifi_test.conf
if [ -e $wifi_config_test ];then
wifi_config=$wifi_config_test
fi
ip_config=$config_path/ifattr
ip_config_bak=$tmpfs_path/ifattr
##wpa_config=$tmpfs_path/wpa_supp.conf
wpa_config=$config_path/wpa_supp.conf
wpa_config_tmp=$tmpfs_path/wpa_supp.conf
wpa_driver=nl80211
fixnet_config=$config_path/fixnet.conf
wifimode=$tmpfs_path/wifimode
wifiscanresult=$tmpfs_path/wifiaplist
net_action_file=/tmpfs/net/net_action
wifi_state_file=/tmpfs/net/wifi_state
ip_cache1=$tmpfs_path/net/ip_cache1
ip_cache2=$tmpfs_path/net/ip_cache2
mac_lined_file=$tmpfs_path/net/mac.lined
mac_wifi_file=$tmpfs_path/net/mac.wifi
ap_cfgfile="/etc/network/wifiap.conf"
ap_ip=192.168.234.1
ap_mask=255.255.255.0
ap_gateway=192.168.234.1
######在下面这个文件中定制ap_ip ap_mask ap_gateway
if [ -e /etc/wifiap_ifattr ];then
. /etc/wifiap_ifattr
fi
IFCONF=$config_path/ifattr
IFRESOLV=/etc/resolv.conf
NETDEV=$tmpfs_path/netdev
####TODO maybe get from configfile
export cfg_wifi_retry=4
### 每次重试的间隔时间
export cfg_wifi_retry_internal=6
#export cfg_wifi_retry_internal=2
####TODO maybe get from configfile
export cfg_wifiwps_retry=12
### 每次重试的间隔时间
export cfg_wifiwps_retry_internal=10
### fastlink多久没有接收到数据
export cfg_wififastlink_retry_internal=30
networkmedia=$config_path/networkmedia.conf
default_net_priflag=1
#iface=
default_net_dhcp=n
default_net_dnstype=specify
default_net_address=192.168.0.136
default_net_netmask=255.255.255.0
default_net_gateway=192.168.0.1
. $wifidev_config
export wifidev
export currdev=eth0
#echo wifidev $wifidev
export wifistate_curr=NONE
export net_action_curr=NONE
export wifimode_curr=NONE
export cfg_wifiautosmtcn=0
export cfg_wifiautofatcn=0
export cfg_stafailedafter=NONE
if [ -e $ap_cfgfile ]
then
. $ap_cfgfile
if [ -z $ap_auto ]
then
export cfg_wifiautoap=1
else
export cfg_wifiautoap=$ap_auto
fi
if [ ! -z $smtcn_auto ]
then
export cfg_wifiautosmtcn=$smtcn_auto
fi
if [ ! -z $fatcn_auto ]
then
export cfg_wifiautofatcn=$fatcn_auto
fi
if [ ! -z $fixnet_auto ]
then
export cfg_wifiautofixnet=$fixnet_auto
fi
if [ ! -z $sta_failed_after ]
then
export cfg_stafailedafter=$sta_failed_after
fi
else
export cfg_wifiautoap=0
fi
#customernum1=`cat /etc/ipcamera/config_devm.ini | grep ^customernum= | sed '2,$d' | cut -d = -f2`
#if [ -z $customernum1 ];then
# customernum1="00"
#fi
export customernum=`paraconf r 2 customernum`
#获取wifi类型
get_wifi_module_type_path="/bin/vslocal/cgi-bin/getwifimoduletype.sh"
if [ -e $get_wifi_module_type_path ]
then
wifi_module_type_tmp=`$get_wifi_module_type_path`
export wifi_module_type=$wifi_module_type_tmp
else
export wifi_module_type=7601
fi
##smtcn_node=/proc/smart_connection
smtcn_node=/proc/elian
get_smtcn_node()
{
if [ -e /proc/elian ];then
echo "/proc/elian"
else
echo "/proc/smart_connection"
fi
}
show_info()
{
echo -e "\033[32m $1 \033[0m" >&2
}
show_info1()
{
echo -e "\033[35m $1 \033[0m" >&2
}
show_error()
{
echo -e "\033[41;37m $1 \033[0m" >&2
}
show_dbg()
{
echo -e "\033[35m debug $* \033[0m" >&2
}
show_dbg2()
{
echo -e "\033[36m debug2 $* \033[0m" >&2
}
show_func()
{
echo -e "\033[35m func $* \033[0m" >&2
}
str_lastn()
{
str=$1
n=$2
strlen=${#str}
if [ ${strlen} -le ${n} ]
then
sn=${str}
else
let start=strlen-n
sn=${str:$start:$n}
fi
echo $sn
}
str_frontn()
{
str=$1
n=$2
strlen=${#str}
if [ ${strlen} -le ${n} ]
then
sn=${str}
else
let start=0
sn=${str:$start:$n}
fi
echo $sn
}
net_off()
{
show_dbg "net_off $1"
ifconfig $1 0.0.0.0
ifconfig $1:1 0.0.0.0 2>/dev/null
}
net_up()
{
show_dbg "net_up $1"
ifconfig $1 up
#ifconfig $1:1 0.0.0.0 2>/dev/null
}
net_down()
{
show_dbg "net_down $1"
net_off $1
ifconfig $1 down
}
## 获取指定网卡网络状态, 输入$1 为网卡, 返回状态,
## 0 不存在, 1 在线, 2, 只有无线
net_getstatus()
{
v=`cat /sys/class/net/$1/carrier 2>$tmpfs_path/err`
status=1
# iface down
if [ -z $v ]
then
status=2
else
status=$v
fi
echo $status
}
## 获取有线状态
net_getlinedstatus()
{
linedstatus=0
networkmedia_lined=YES
if [ -e $networkmedia ]
then
. $networkmedia
if [ -z $networkmedia_lined ]
then
networkmedia_lined=YES
fi
#echo "DDD netype lined is $networkmedia_lined"
else
networkmedia_lined=YES
fi
if [ $networkmedia_lined = "NO" ]
then
linedstatus=2
else
linedstatus=$(net_getstatus eth0)
fi
echo $linedstatus
}
#NONE
#STA
#AP
#WPS
wifi_setmode()
{
show_info "wifi setmode to $1"
wifimode_curr=$1
echo $wifimode_curr > $wifimode
}
wifi_getmode()
{
wifim="NONE"
if [ -e $wifimode ]
then
wifim=`cat $wifimode`
fi
echo $wifim
}
wifi_getenable()
{
wifienable=0
ifconfig $wifidev >$tmpfs_path/ra0status 2>&1
ra0status=$?
if [ $ra0status = 1 ]
then
echo -1
return 0
fi
if [ -e $wifi_config ]
then
##get wifienable from config file
. $wifi_config
else
wifienable=-1
fi
echo $wifienable
}
net_gen_ifattr()
{
show_info "Not exist ifattr var."
priflag=$default_net_priflag
dhcp=$default_net_dhcp
dnstype=$default_net_dnstype
address=$default_net_address
netmask=$default_net_netmask
gateway=$default_net_gateway
echo -e "##autogen\n" \
"priflag=$priflag\n"\
"dhcp=$dhcp\n"\
"dnstype=$dnstype\n"\
"address=$address\n"\
"netmask=$netmask\n"\
"gateway=$gateway\n"> $ip_config
sync
}
net_setmac()
{
show_info "[ipconfig]Set MAC"
linediface=eth0
mac=`paraconf r 1 mac`
ret=$?
if [ -z ${mac} ] && [ -f /tmpfs/paraconf ]
then
mac=`cat /tmpfs/paraconf | grep mac | cut -d= -f2`
fi
if [ "${mac}A" = "A" ] || [ "${mac}" = "null" ]
then
. /etc/board.conf
mac=`randommac $boardtype`
show_info "auto gen MAC:$mac"
#ifconfig $linediface down
ifconfig $linediface hw ether $mac
#ifconfig $linediface up
paraconf w 1 mac $mac
echo "$mac" >> /etc/ipcamera/mac.lst
else
show_info "set MAC $mac"
#ifconfig $linediface down
net_down $linediface
ifconfig $linediface hw ether $mac
#ifconfig $linediface up
net_up $linediface
fi
echo "macaddress=$mac" > $mac_lined_file
sync
}
#设置当前使用的网卡
net_setdev()
{
if [ -z $1 ]
then
echo eth0 > $tmpfs_path/netdev
else
echo $1 > $tmpfs_path/netdev
fi
sync
}
net_getdev()
{
if [ -e $tmpfs_path/netdev ]
then
netdev=`cat $tmpfs_path/netdev`
else
netdev=eth0
fi
echo $netdev
}
net_setaction()
{
l_act=NONE
if [ ! -z $1 ]
then
l_act=$1
fi
show_dbg "Net Action Change $l_act"
net_action_curr=$l_act
echo "action=$l_act" > $net_action_file
}
net_getaction()
{
l_act=NONE
if [ -e $net_action_file ]
then
. $net_action_file
if [ -z action ]
then
l_act=NONE
else
l_act=$action
fi
fi
echo $l_act
}
wifi_setstate()
{
l_state=NONE
if [ ! -z $1 ]
then
l_state=$1
fi
wifistate_curr=$l_state
show_dbg "wifi state Change $wifistate_curr"
echo $wifistate_curr > $wifi_state_file
}
wifi_getstate()
{
echo $wifistate_curr
}
net_cache()
{
#$1 NETTYPE #$2 IP #$3 MASK #$4 GW #$5 MAC #$6 DHCPFlag #$7 DNSFlag $8IP2
if [ $6 = 0 ]
then
dhcpflag="off"
#dnsflag="0"
else
dhcpflag="on"
#dnsflag="1"
fi
if [ -z $7 ]
then
##show_info "not para 7"
. $ip_config
else
dnstype=$7
fi
if [ $dnstype = "server" ]
then
dnsflag=1
else
dnsflag=0
fi
show_info "dnstype:$dnstype, dnsflag:$dnsflag"
if [ $1 = "ra0" ]
then
nettype="WIRELESS"
else
nettype="LAN"
fi
echo -e -n "var networktype = \"$nettype\" ;\r\n"\
"var ip = \"$2\" ;\r\n"\
"var netmask = \"$3\" ;\r\n"\
"var gateway = \"$4\" ;\r\n"\
"var macaddress = \"$5\" ;\r\n"\
"var dhcpflag = \"$dhcpflag\" ;\r\n"\
"var dnsstat = \"$dnsflag\" ;\r\n" \
"var ip2 = \"$onvif_plugplay_ip\" ;\r\n" > $ip_cache1
echo -e -n "networktype $1 \n"\
"ip $2 \n"\
"netmask $3 \n"\
"gateway $4 \n"\
"macaddress $5 \n"\
"dhcpflag $6 \n"\
"dnsstat $dnsflag \n" \
"ip2 $onvif_plugplay_ip \n" > $ip_cache2
sync
show_info "net change, cache it.nettype $1 ip $2 dhcp $6 ip2 $onvif_plugplay_ip"
logger "gen ip cache nettype $1 ip $2 dhcp $6 ip2 $onvif_plugplay_ip"
}
#icatchplugplay=`cat /etc/ipcamera/onvif.ini | grep ^icatchplugplay= | sed '2,$d' | cut -d = -f2`
#if [ -z $icatchplugplay ];then
# icatchplugplay="0"
#fi
icatchplugplay=0;
net_ipconfig()
{
##$1 netcard
##$2 dhcp
##$3 ip(address)
##$4 netmask
##$5 gateway
##$6 dnsflag
iface=$1
dhcp=$2
address=$3
netmask=$4
gateway=$5
dnsflag=$6
. $mac_lined_file
if [ $icatchplugplay == "1" ]
then
show_info "icahtchplugplay enable"
export onvif_plugplay_ip=`mactoip $macaddress`
if [ $? == "0" ]
then
show_info "onvif_plugplay_ip is $onvif_plugplay_ip ,mac $macaddress"
ifconfig $iface:1 $onvif_plugplay_ip netmask 255.0.0.0
else
show_error "excute mactoip $macaddress error"
fi
fi
show_info "iface:$iface,dhcp:$dhcp,ip:$address,netmask:$netmask,gateway:$gateway:dnsflag:$dnsflag"
if [ $dhcp = y ]
then
#*************run udhcpc************************************
killall -9 udhcpc 2>/dev/null
/sbin/udhcpc -i $iface -b -A 3
elif [ $dhcp = n ]
then
#*****************stop udhcpc TODO:use pidfile**************
killall -9 udhcpc 2>/dev/null
#***************** Flush route table, if use 2 iface, thie ???? *****
###By BT 20140827 ip route show | awk -v k="$iface" '$0 !~ k { print " ip route delete " $0 | "sh" }'
ip route show | awk -v k="$iface" '$0 !~ k && $0 !~ "lo" { print " ip route delete " $0 | "sh" }'
#************TODO:set ip mask******************************
#if not lined network , clear ip
if [ -n "$address" -a -n "$netmask" ];then
/sbin/ifconfig $iface $address netmask $netmask
fi
#************set Gateway DNS******************************
route add default gw $gateway dev $iface 2>/dev/null
route add -net 239.0.0.0 netmask 255.0.0.0 $iface 2>/dev/null
. $mac_lined_file
#$1 NETTYPE #$2 IP #$3 MASK #$4 GW #$5 MAC #$6 DHCPFlag #$7 DNSFlag
if [ $icatchplugplay == "1" ]
then
show_info "icahtchplugplay enable"
export onvif_plugplay_ip=`mactoip $macaddress`
if [ $? == "0" ]
then
show_info "onvif_plugplay_ip is $onvif_plugplay_ip ,mac $macaddress"
ifconfig $iface:1 $onvif_plugplay_ip netmask 255.0.0.0
else
show_error "excute mactoip $macaddress error"
fi
fi
net_cache $iface $address $netmask $gateway $macaddress "0" $dnsflag
#echo -e -n "var ip = \"$address\" ;\r\n"\
# "var netmask = \"$netmask\" ;\r\n"\
# "var gateway = \"$gateway\" ;\r\n"\
# "var macaddress = \"$macaddress\" ;\r\n" > $ip_cache1
#sync
/bin/vs/mcast.sh 5 &
fi
arping -c 3 -b -I $iface 192.168.0.1 &
}
net_setip()
{
. $ip_config
#生成默认ifattr
if [ -z $dnstype ]
then
net_gen_ifattr
. $ip_config
fi
nettype=$1
case $nettype in
LINED)
iface=eth0
#ifconfig $wifidev 0.0.0.0 2>/dev/null
net_off $wifidev
net_ipconfig $iface $dhcp $address $netmask $gateway $dnstype
;;
STA)
iface=ra0
#ifconfig eth0 0.0.0.0
net_off eth0
net_ipconfig $iface $dhcp $address $netmask $gateway $dnstype
;;
AP)
iface=ra0
#ifconfig eth0 0.0.0.0
net_off eth0
wifi_ap_ipcfg
;;
*)
;;
esac
}
net_setip_currdev()
{
. $ip_config
iface=$(net_getdev)
show_info "currdev is $iface, dhcp:$dhcp, dnstype:$dnstype"
net_ipconfig $iface $dhcp $address $netmask $gateway $dnstype
}
#$1 as NONE,devsrejoin reboot, WIFI_STA
sys_setaction()
{
if [ -z $1 ]
then
echo "NONE" > /tmpfs/sysmon
else
echo "$1" > /tmpfs/sysmon
fi
fsync /tmpfs/sysmon
}