修改: inss.sh

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

90
inss.sh
View File

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