修改: inss.sh

This commit is contained in:
aixiao 2019-01-19 01:17:29 +08:00
parent a7e44b4571
commit 8d4dd18f65

122
inss.sh
View File

@ -17,10 +17,10 @@ function INIT_() {
ip_addr_array1=(10); # 10 ip_addr_array1=(10); # 10
#填写自己需要的内网IP.IP第二个字节值.SHELL数组. #填写自己需要的内网IP.IP第二个字节值.SHELL数组.
ip_addr_array2=(99 97 44 45 31); # 4 43 31 ip_addr_array2=(99); # 4 43 31
#填写自己的网卡. #填写自己的网卡.
NIC="rmnet_data1"; NIC="rmnet_data0";
#网卡开启状态. #网卡开启状态.
NIC_STATUS="U"; NIC_STATUS="U";
@ -29,20 +29,25 @@ function INIT_() {
SLEEP="7"; SLEEP="7";
if ! ${bbox} &> /dev/null ; then echo "BusyBox No Found !" ; exit 1 ; fi if ! ${bbox} &> /dev/null; then echo "BusyBox No Found !"; exit 1; fi
svc_="$(${bbox} which svc)"; svc_="$(${bbox} which svc)";
settings_="$(${bbox} which settings)"; settings_="$(${bbox} which settings)";
am_="$(${bbox} which am)"; am_="$(${bbox} which am)";
netcfg_="$(${bbox} which netcfg)" netcfg_="$(${bbox} which netcfg)";
#判断必须要命令.否则退出子壳返回错误代码1. #判断必须要命令.否则退出子壳返回错误代码1.
if ${bbox} [[ "${svc_}" = "" ]]; then echo "Svc No Found !" ; exit 1 ; fi if ${bbox} [[ "${svc_}" = "" ]]; then echo "Svc No Found !"; exit 1; fi
if ${bbox} [[ "${settings_}" = "" ]]; then echo "Settings No Found !" ; exit 1 ; fi if ${bbox} [[ "${settings_}" = "" ]]; then echo "Settings No Found !"; exit 1; fi
if ${bbox} [[ "${am_}" = "" ]]; then echo "Am No Found !" ; exit 1 ; fi if ${bbox} [[ "${am_}" = "" ]]; then echo "Am No Found !"; exit 1; fi
if ${bbox} [[ "${parameter}" != "" ]] ; then if ${bbox} [[ "${parameter}" != "" ]]; then
${bbox} echo ${parameter} | grep [0-9] > /dev/null 2>&1 && ip_addr_array2=(${parameter}) : 判断参数是不是全为数字
num=`${bbox} echo ${parameter} | ${bbox} sed 's/[0-9]//g'`;
# ${bbox} echo ${parameter} | grep [0-9] > /dev/null 2>&1 && ip_addr_array2=(${parameter});
if ${bbox} [[ ${num} = "" ]]; then
ip_addr_array2=(${parameter});
fi
fi fi
function HELP_() { function HELP_() {
@ -51,100 +56,96 @@ ${bbox} cat << EOF
Ip network switch script. Ip network switch script.
Usage: Usage:
${0} [N] [N]. ${0} [N] [N].
${0} [option] [parameter]. ${0} [-xch] [FILE].
options: options:
-x : tarck. -x : print debug.
-c : config file. -c : config file.
[FILE] (default: ${inss}/conf/inss.ini).
-h : print help. -h : print help.
parameters:
-c : config file (default: ${inss}/conf/inss.ini).
inss by aixiao@aixiao.me. inss by aixiao@aixiao.me.
EOF EOF
exit 0 exit 0;
} }
} }
function ROOT_(){ function ROOT_(){
# root用户. # root用户.
if ${bbox} [[ "`${bbox} id -u`" != "0" ]] ; then ${bbox} echo "ROOT user run ?" ; exit 1 ; fi if ${bbox} [[ "`${bbox} id -u`" != "0" ]] ; then ${bbox} echo "ROOT user run ?"; exit 1; fi
} }
function LOG_() { function LOG_() {
# 写入日志文件. # 写入日志文件.
ip=$(${bbox} netstat -r | ${bbox} grep ${NIC} | ${bbox} grep ${NIC_STATUS} | ${bbox} awk '{print $1}') ip=$(${bbox} netstat -r | ${bbox} grep ${NIC} | ${bbox} grep ${NIC_STATUS} | ${bbox} awk '{print $1}');
if ${bbox} [[ -d ${inss}/log ]] ; then if ${bbox} [[ -d ${inss}/log ]]; then
today=$(date +"%Y%m%d%H%M%S") today=$(date +"%Y%m%d%H%M%S");
echo ${today} >> ${inss}/log/ip_address.log echo ${today} >> ${inss}/log/ip_address.log;
echo ${ip} >> ${inss}/log/ip_address.log echo ${ip} >> ${inss}/log/ip_address.log;
fi fi
} }
function one_() { function one_() {
# 截取IP第一个字节值. # 截取IP第一个字节值.
ip_addres1=`${bbox} netstat -r | ${bbox} grep ${NIC} | ${bbox} grep ${NIC_STATUS} | ${bbox} awk '{print $1}' | ${bbox} cut -d "." -f 1` ip_addres1=`${bbox} netstat -r | ${bbox} grep ${NIC} | ${bbox} grep ${NIC_STATUS} | ${bbox} awk '{print $1}' | ${bbox} cut -d "." -f 1`;
echo "${ip_addres1}" echo "${ip_addres1}";
} }
function two_() { function two_() {
# 截取IP第二个字节值. # 截取IP第二个字节值.
ip_addres2=`${bbox} netstat -r | ${bbox} grep ${NIC} | ${bbox} grep ${NIC_STATUS} | ${bbox} awk '{print $1}' | ${bbox} cut -d "." -f 2` ip_addres2=`${bbox} netstat -r | ${bbox} grep ${NIC} | ${bbox} grep ${NIC_STATUS} | ${bbox} awk '{print $1}' | ${bbox} cut -d "." -f 2`;
echo "${ip_addres2}" echo "${ip_addres2}";
} }
function INSTATUS_() { function INSTATUS_() {
# 网络状态. # 网络状态.
ip_rmnet0=`${bbox} netstat -r | ${bbox} grep ${NIC} | ${bbox} grep ${NIC_STATUS} | ${bbox} awk 'NR==1 {print $4}'` ip_rmnet0=`${bbox} netstat -r | ${bbox} grep ${NIC} | ${bbox} grep ${NIC_STATUS} | ${bbox} awk 'NR==1 {print $4}'`;
echo "${ip_rmnet0}" echo "${ip_rmnet0}";
} }
function IPSTATUS_() { function IPSTATUS_() {
# 打印完整IP. # 打印完整IP.
${bbox} netstat -r | ${bbox} grep ${NIC} | ${bbox} grep ${NIC_STATUS} | ${bbox} awk '{print $1}' ${bbox} netstat -r | ${bbox} grep ${NIC} | ${bbox} grep ${NIC_STATUS} | ${bbox} awk '{print $1}';
} }
function LOOP_() { function LOOP_() {
# 循环执行sleep直到营运商给手机分配IP. # 循环执行sleep直到营运商给手机分配IP.
while ${bbox} [[ "`two_`" = "" ]]; do while ${bbox} [[ "`two_`" = "" ]]; do
${bbox} sleep 7; ${bbox} sleep 7;
two_ two_;
done done
} }
function MAIN_ { function MAIN_ {
if ${bbox} [[ "`INSTATUS_ 2> /dev/null`" != "${NIC_STATUS}" ]] ; then # 判断网络是否开启. if ${bbox} [[ "`INSTATUS_ 2> /dev/null`" != "${NIC_STATUS}" ]]; then # 判断网络是否开启.
echo "数据连接已经关闭..." echo "数据连接已经关闭...";
echo "数据连接正在打开..." echo "数据连接正在打开...";
else else
echo "数据连接已经开启..." echo "数据连接已经开启..."
for o in ${ip_addr_array1[@]} ; do # 开启还要检查IP对不对. for o in ${ip_addr_array1[@]}; do # 开启还要检查IP对不对.
if ${bbox} [[ "`one_`" = "${o}" ]] ; then if ${bbox} [[ "`one_`" = "${o}" ]] ; then
for t in ${ip_addr_array2[@]} ; do for t in ${ip_addr_array2[@]}; do
if ${bbox} [[ "`two_`" = "${t}" ]] ; then if ${bbox} [[ "`two_`" = "${t}" ]]; then
LOG_ # 调用自定义函数LOG_, 把路由存入日至文件. LOG_; # 调用自定义函数LOG_, 把路由存入日至文件.
exit 1 # IP第二个字节值匹配的话退出, 不匹配进入下面循环. exit 1; # IP第二个字节值匹配的话退出, 不匹配进入下面循环.
fi fi
done done
fi fi
done done
fi fi
while true ; do #循环结构. while true; do #循环结构.
eval ${STOP}; eval ${STOP};
${bbox} sleep ${SLEEP}; # 等待. ${bbox} sleep ${SLEEP}; # 等待.
eval ${START}; eval ${START};
LOOP_ # 调用自定义函数LOOP_, 直到营运商给我分配IP. LOOP_; # 调用自定义函数LOOP_, 直到营运商给我分配IP.
IPSTATUS_ # 调用自定义函数IPSTATUS_, 打印完整IP. IPSTATUS_; # 调用自定义函数IPSTATUS_, 打印完整IP.
for o in ${ip_addr_array1[@]} ; do for o in ${ip_addr_array1[@]} ; do
if ${bbox} [[ "`one_`" = "${o}" ]] ; then # 循环直到IP的第二个字节值为我想要的. if ${bbox} [[ "`one_`" = "${o}" ]]; then # 循环直到IP的第二个字节值为我想要的.
for t in ${ip_addr_array2[@]} ; do # 数组元素赋值给变量t. for t in ${ip_addr_array2[@]}; do # 数组元素赋值给变量t.
if ${bbox} [[ "`two_`" = "${t}" ]] ; then # 数组元素是否等于自定义函数返回值. if ${bbox} [[ "`two_`" = "${t}" ]]; then # 数组元素是否等于自定义函数返回值.
LOG_ LOG_;
exit 1 # 退出. exit 1; # 退出.
fi fi
done done
fi fi
@ -153,15 +154,14 @@ function MAIN_ {
} }
INIT_ INIT_
while getopts :xc:h? l while getopts :xc:h? l; do
do
case ${l} in case ${l} in
x) x)
debug=x debug=x;
;; ;;
c) c)
#载入配置文件. # 载入配置文件.
. $OPTARG . $OPTARG;
;; ;;
h|?) h|?)
HELP_; HELP_;
@ -169,14 +169,14 @@ case ${l} in
esac esac
done done
shift $((OPTIND-1)) shift $((OPTIND-1));
test "${debug}" = "x" && set -x test "${debug}" = "x" && set -x;
ROOT_ # 判断ROOT用户执行. ROOT_; # 判断ROOT用户执行.
one_ # 调用自定义函数one_, 打印IP的第一个字节值. one_ ; # 调用自定义函数one_, 打印IP的第一个字节值.
two_ # 调用自定义函数two_, 打印IP的第二个字节值. two_; # 调用自定义函数two_, 打印IP的第二个字节值.
IPSTATUS_ # 调用自定义函数IPSTATUS_, 打印IP. IPSTATUS_; # 调用自定义函数IPSTATUS_, 打印IP.
INSTATUS_ 2> ${null} # 调用自定义函数INSTATUS_, 打印网络的状态. INSTATUS_ 2> ${null}; # 调用自定义函数INSTATUS_, 打印网络的状态.
MAIN_ MAIN_;
exit exit $?;
201812262344 201812262344
aixiao@aixiao.me aixiao@aixiao.me