多宽带联网(三) OpenWrt中使用cURL方法编写Shell脚本自动完成网页宽带认证登录(校园网认证登录解决方案) 使用awk命令

  对于非PPPoE方式认证,而是采用网页认证登录的宽带用户(如校园网)。采用《多宽带联网(二)》中的方法一个一个接口进行认证登录过于繁琐。本节将采用Shell脚本和cURL方法,模拟人工认证步骤,自动使能单一接口并完成认证登录流程。

目录

一、cURL安装

 二、cURL认证请求捕获

1. 捕获浏览器认证请求

2. 编辑浏览器认证请求

三、编写Shell脚本

1. 准备工作(grep筛选IP、awk修改配置文件)

2. 编写Shell脚本

3. 保存Shell脚本

4. 执行Shell脚本

四、开机自动执行Shell脚本

附录、完整的Shell认证登录脚本代码


一、cURL安装

  登录OpenWrt的Web管理后台,选择 “系统->Software(软件)”菜单,在 “Filter” 中输入cURL,在软件列表中,选择名为 “curl” 的应用进行安装(图14)。注:若搜索不到任何软件,请先在 “操作” 中点击 “Update lists”,来更新软件列表。更新时,需确保OpenWrt能正确访问广域网,即:在 “网络->接口” 中,至少需要有一个接口能成功连接至广域网。

图14 cURL软件安装

 二、cURL认证请求捕获

  通过cURL,可以模拟浏览器发送请求给服务器,完成宽带账户认证登录的过程。首先,需要捕获浏览器发送给服务器的请求,然后使用cURL方法模拟发送。

1. 捕获浏览器认证请求

  1)仅启用一个虚拟macvlan接口:在OpenWrt的Web面板中,进入 “网络-> 负载均衡(load balancing)” 菜单,选择 “接口” 选项卡,在接口列表中,编辑各接口,仅保留一个接口作为后续捕获认证登录数据包所用,禁用其余接口,防止宽带登录完毕后OpenWrt通过负载均衡切换至其他出口,影响调试。

  2)打开浏览器(建议使用Chrome),访问宽带认证登录网页。

  3)按键盘的F12按键,打开开发者调试工具。

  4)选择 “Network” 选项卡(图15),在这里,可以看到浏览器与服务器的交互请求。首先,点击左上角Clear按钮(图15 开发者调试工具的左上角红框处),清空现有请求(若已经为空则不需清空)。

图15 浏览器开发者调试工具

   5)完成准备后,输入用户名和密码,点击登录按钮,完成所有登录流程。此时,可以在开发者调试工具中,看到登录请求数据。在该条数据处单击右键,选择 “Copy -> Copy as cURL (bash)”,图16。说明:bash形式的cURL可以在Linux(如OpenWrt)终端或Shell脚本中执行,cmd形式的cURL可以在Windows系统的命令提示符(cmd)中执行。

图16 捕获浏览器登录认证请求

   6)将复制的cURL请求,粘贴至文本文件中。例如本次实验捕获的认证数据如下所示。

curl 'http://10.255.255.34/api/v1/login' \
  -H 'Connection: keep-alive' \
  -H 'Accept: application/json, text/plain, */*' \
  -H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.82 Safari/537.36' \
  -H 'Content-Type: application/json;charset=UTF-8' \
  -H 'Origin: http://10.255.255.34' \
  -H 'Referer: http://10.255.255.34/authentication/login' \
  -H 'Accept-Language: zh-CN,zh;q=0.9' \
  --data-raw '{"username":"**********5","password":"******","channel":"3","ifautologin":"0","pagesign":"secondauth","usripadd":"10.12.57.76"}' \
  --compressed \
  --insecure

2. 编辑浏览器认证请求

  1)对捕获的浏览器认证请求,去除末尾的下列两行,并去除这两行的上一行末尾的换行符 “\”。

--compressed \
--insecure

  去除后完整请求如下所示:

curl 'http://10.255.255.34/api/v1/login' \
  -H 'Connection: keep-alive' \
  -H 'Accept: application/json, text/plain, */*' \
  -H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.82 Safari/537.36' \
  -H 'Content-Type: application/json;charset=UTF-8' \
  -H 'Origin: http://10.255.255.34' \
  -H 'Referer: http://10.255.255.34/authentication/login' \
  -H 'Accept-Language: zh-CN,zh;q=0.9' \
  --data-raw '{"username":"**********5","password":"******","channel":"3","ifautologin":"0","pagesign":"secondauth","usripadd":"10.12.57.76"}'

  2)以root账户登录OpenWrt终端(可直接本机登录,或采用SSH等方式登录)。复制上述编辑完毕的cURL请求文本,粘贴至OpenWrt终端内,回车运行。运行完毕后可看到服务器返回的登录成功数据,本实验的数据如(图17)。

图17 终端执行结果

三、编写Shell脚本

  Shell脚本将模拟人工启用/禁用接口、认证登录的行为,对每一个MWAN3接口依次启用,并关闭其余接口,完成该接口对应的macvlan虚拟网卡认证登录。完成所有macvlan虚拟网卡认证登录后,再开启所有的MWAN3接口,即可通过各接口进行负载均衡。

1. 准备工作(grep筛选IP、awk修改配置文件)

  从捕获的cURL认证请求中,可以看到请求参数还需要网卡的IP地址(不同的认证页面需要的参数也不一样,如有的可能还需要网卡MAC地址,有的只需要账号密码),因此需准备好各虚拟macvlan网卡的IP地址。网卡的IP地址可通过OpenWrt的Web管理页面,选择 “网络->接口” 菜单,在对应的接口(如wan0)处,可查看到网卡的IPv4地址。此处,我们需要在终端中,通过命令方式获取网卡IP。

  1)查看网卡信息

  在OpenWrt终端内,输入 ifconfig 命令可查看所有网卡信息。

  输入 ifconfig vth0 命令可查看单一网卡信息(如vth0),结果如(图18)。第一个红框为vth0的MAC地址,第二个红框为vth0的IPv4地址。

图18 查看网卡信息

   2)筛选出IPv4地址

  通过命令 grep "text",可筛选出具有 “text” 文本所在的行。

  通过 cut -d '分隔符' -f 显示字段 命令,可以显示文本中的指定字段的内容。如执行命令:

echo "a/b/c" | cut -d '/' -f 2

  echo 表示输出后面的结果,cut -d '/' 表示以符号 / 作为分隔符,将字符串 a/b/c分割为a、b、c三个字段,-f 2 表示显示第二个字段,即字符b。因此返回结果为b。

  综上所述,结合 ipconfig 查询的网卡信息格式(图18),使用下列命令可获取指定网卡的IPv4地址(图19)。

ifconfig vth0 |grep 'inet addr:'|cut -d ' ' -f 12|cut -d ':' -f 2
图19 筛选IPv4地址

  若认证信息还需MAC地址,可自行编写命令获取。

  3)准备MWAN3接口的启用/停止命令

  MWAN3配置文件位于:/etc/config/mwan3,在终端内使用下列命令查看MWAN3配置文件。

vim /etc/config/mwan3

  打开MWAN3配置文件后,可查看配置文件(图20)。其中,config interface 为接口配置章节,每个接口均对应一个 config interface。接下来的 option … 为接口章节对应的各配置选项,重点关注 option enabled 选项。通过修改该选项的值为 “1” 或 “0”,便可开启或关闭MWAN3接口。

图20 MWAN3配置文件解读

  使用awk函数编写Shell脚本,实现自动定位到各接口,然后开启该接口,并关闭其余接口的功能。当某个接口唯一打开后,便可采用cURL方法为该接口登录网页认证账户。

       使用下列命令,便可自动寻找到wan0的章节位置,并在该章节下寻找到enabled的配置选项,自动将0替换为1后,保存至文件。对于其他接口,仅需将命令中第一行的 'wan0' 替换为对应mwan3接口的名字即可。第四行 gsub(0,1) 用于将0替换为1,若需要关闭接口将其改为gsub(1,0)即可。

awk -F "'" -v section="config interface 'wan0'" '
    $0==section{flag=1;print;next}
    /config/{flag=0}
    flag==1&&/option enabled/{gsub(0,1)}
    {print}' /etc/config/mwan3 1<>/etc/config/mwan3

  更改配置文件后,需要使用下列命令重启服务。

/etc/init.d/mwan3 restart

【详细解释】awk函数使用说明

  • awk语句中,单引号部分为执行的指令部分,前面为参数设置,后面为文件操作。
  • -F参数:分隔符参数。默认分隔符为空格。此处指定为单引号。

  例如一文件,内有字符串:option enabled '1'

  使用 awk -F "'" '{print $0;print $1;print $2}' 文件名 命令后,可按单引号将上述字符串分割开。在awk内,变量$0为完整字符串,$1为第一部分,即 option enabled;$2为第二部分,即1。

  • -v 参数:可指定自定义变量。如 -v section="config interface 'wan0'",在执行的指令部分即可使用 section 变量来表达 config interface 'wan0'
  • 指令部分:-v参数后面的单引号部分为执行的指令部分。此处一共有四段指令。awk将对文件逐行执行各个指令。指令中,{} 内为执行的操作,{} 前面为该操作的条件。

  其中,next 表示立即跳转至下一行。即:忽略后面剩下的指令,立即跳转至下一行。在本例中,当 $0==section 匹配满足后,即寻找到了wan0接口的章节,将flag置1后,立即执行下一行。这样可避免下一段内使用正则表达式 /config/ 指令再次匹配到 config interface ‘wan0’ 所在的行,而又将flag立刻置为0。正确的操作应该是 /config/ 匹配到下一章开头时,再将flag置0。

  flag为1的区间,即为wan0所在的配置选项范围。在此范围内继续匹配 option enabled 选项即可。匹配到flag为1且 option enabled 后,使用 gsub(),即可将0替换为1。然后将修改结果输出至文件即可。


2. 编写Shell脚本

  1)编写一个虚拟macvlan网卡(如vth0)的Shell脚本。

FILEDIR=/etc/config/mwan3
# Open only one mwan interface.
awk -F "'" -v section="config interface 'wan0'" '
    $0==section{flag=1;print;next}
    /config/{flag=0}
    flag==1&&/option enabled/{gsub(0,1)}
    {print}' $FILEDIR 1<>$FILEDIR
awk -F "'" -v section="config interface 'wan1'" '
    $0==section{flag=1;print;next}
    /config/{flag=0}
    flag==1&&/option enabled/{gsub(1,0)}
    {print}' $FILEDIR 1<>$FILEDIR
awk -F "'" -v section="config interface 'wan2'" '
    $0==section{flag=1;print;next}
    /config/{flag=0}
    flag==1&&/option enabled/{gsub(1,0)}
    {print}' $FILEDIR 1<>$FILEDIR
awk -F "'" -v section="config interface 'wan3'" '
    $0==section{flag=1;print;next}
    /config/{flag=0}
    flag==1&&/option enabled/{gsub(1,0)}
    {print}' $FILEDIR 1<>$FILEDIR
awk -F "'" -v section="config interface 'wan4'" '
    $0==section{flag=1;print;next}
    /config/{flag=0}
    flag==1&&/option enabled/{gsub(1,0)}
    {print}' $FILEDIR 1<>$FILEDIR
awk -F "'" -v section="config interface 'wan5'" '
    $0==section{flag=1;print;next}
    /config/{flag=0}
    flag==1&&/option enabled/{gsub(1,0)}
    {print}' $FILEDIR 1<>$FILEDIR
awk -F "'" -v section="config interface 'wan6'" '
    $0==section{flag=1;print;next}
    /config/{flag=0}
    flag==1&&/option enabled/{gsub(1,0)}
    {print}' $FILEDIR 1<>$FILEDIR
awk -F "'" -v section="config interface 'wan7'" '
    $0==section{flag=1;print;next}
    /config/{flag=0}
    flag==1&&/option enabled/{gsub(1,0)}
    {print}' $FILEDIR 1<>$FILEDIR
/etc/init.d/mwan3 restart

# Get the IP address of the current network card.
CUIP=$(ifconfig vth0 |grep 'inet addr:'|cut -d ' ' -f 12|cut -d ':' -f 2)

# Log in through curl. This part is the general code of vth0 ~ vth7.
NStat=$(curl 'http://10.255.255.34/api/v1/login' \
  -H 'Connection: keep-alive' \
  -H 'Accept: application/json, text/plain, */*' \
  -H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.82 Safari/537.36' \
  -H 'Content-Type: application/json;charset=UTF-8' \
  -H 'Origin: http://10.255.255.34' \
  -H 'Referer: http://10.255.255.34/authentication/login' \
  -H 'Accept-Language: zh-CN,zh;q=0.9' \
  --data-raw '{"username":"**********5","password":"******","channel":"3","ifautologin":"0","pagesign":"secondauth","usripadd":"'$CUIP'"}')

# Judge whether the login is successful.
echo "${NStat}"
NCode=$(echo "${NStat}" | grep "code" | cut -d " " -f 2 | cut -d "," -f 1)
if [ $NCode != 200 ]; then
    # Login failed.
    vth0ISP=0
else
    vth0ISP=1
fi

  行1~行43:启用vth0的接口,禁用vth1~vth7接口。

  行45~46,获取vth0网卡的IP地址,并保存至CUIP变量。

  行48~57,执行cURL请求,并将服务器返回结果存储至NStat变量。注意:需在捕获的cURL外,加入$()来获取返回的字符串结果。还需将请求中的IP地址替换为CUIP变量,变量读取方法为$CUIP。

  行59~67,根据图17,自行编写返回结果筛选代码,此处筛选返回的状态参数code,经测试,本实验的code为200时表明登录成功。


  登录成功后vth0ISP变量置1,在所有接口开启完毕后,将会逐个判断vth*ISP变量,若为1则启用对应接口,否则禁用对应接口。脚本执行完毕后,登录OpenWrt后台,转到 “网络->负载均衡” 页面,选择 “接口” 选项卡,查看各接口的启用状态即可知道哪些接口认证登录失败。


  2)编写完整的Shell脚本

  将上述Shell脚本复制,并修改行1~行43的启用接口和禁用接口,修改行46的网卡名称,修改行48~57中的宽带账户和密码,修改行64、66的标志变量名称即可。

  完整的参考脚本见文末。

  在最后,加入如下代码,开启所有成功认证登录的接口。

# Start all mwan interface.
if [ $vth0ISP != 0 ]; then
    awk -F "'" -v section="config interface 'wan0'" '
	    $0==section{flag=1;print;next}
		/config/{flag=0}
		flag==1&&/option enabled/{gsub(0,1)}
		{print}' $FILEDIR 1<>$FILEDIR
else
	awk -F "'" -v section="config interface 'wan0'" '
	    $0==section{flag=1;print;next}
		/config/{flag=0}
		flag==1&&/option enabled/{gsub(1,0)}
		{print}' $FILEDIR 1<>$FILEDIR
fi
if [ $vth1ISP != 0 ]; then
    awk -F "'" -v section="config interface 'wan1'" '
	    $0==section{flag=1;print;next}
		/config/{flag=0}
		flag==1&&/option enabled/{gsub(0,1)}
		{print}' $FILEDIR 1<>$FILEDIR
else
	awk -F "'" -v section="config interface 'wan1'" '
	    $0==section{flag=1;print;next}
		/config/{flag=0}
		flag==1&&/option enabled/{gsub(1,0)}
		{print}' $FILEDIR 1<>$FILEDIR
fi
if [ $vth2ISP != 0 ]; then
    awk -F "'" -v section="config interface 'wan2'" '
	    $0==section{flag=1;print;next}
		/config/{flag=0}
		flag==1&&/option enabled/{gsub(0,1)}
		{print}' $FILEDIR 1<>$FILEDIR
else
	awk -F "'" -v section="config interface 'wan2'" '
	    $0==section{flag=1;print;next}
		/config/{flag=0}
		flag==1&&/option enabled/{gsub(1,0)}
		{print}' $FILEDIR 1<>$FILEDIR
fi
if [ $vth3ISP != 0 ]; then
    awk -F "'" -v section="config interface 'wan3'" '
	    $0==section{flag=1;print;next}
		/config/{flag=0}
		flag==1&&/option enabled/{gsub(0,1)}
		{print}' $FILEDIR 1<>$FILEDIR
else
	awk -F "'" -v section="config interface 'wan3'" '
	    $0==section{flag=1;print;next}
		/config/{flag=0}
		flag==1&&/option enabled/{gsub(1,0)}
		{print}' $FILEDIR 1<>$FILEDIR
fi
if [ $vth4ISP != 0 ]; then
    awk -F "'" -v section="config interface 'wan4'" '
	    $0==section{flag=1;print;next}
		/config/{flag=0}
		flag==1&&/option enabled/{gsub(0,1)}
		{print}' $FILEDIR 1<>$FILEDIR
else
	awk -F "'" -v section="config interface 'wan4'" '
	    $0==section{flag=1;print;next}
		/config/{flag=0}
		flag==1&&/option enabled/{gsub(1,0)}
		{print}' $FILEDIR 1<>$FILEDIR
fi
if [ $vth5ISP != 0 ]; then
    awk -F "'" -v section="config interface 'wan5'" '
	    $0==section{flag=1;print;next}
		/config/{flag=0}
		flag==1&&/option enabled/{gsub(0,1)}
		{print}' $FILEDIR 1<>$FILEDIR
else
	awk -F "'" -v section="config interface 'wan5'" '
	    $0==section{flag=1;print;next}
		/config/{flag=0}
		flag==1&&/option enabled/{gsub(1,0)}
		{print}' $FILEDIR 1<>$FILEDIR
fi
if [ $vth6ISP != 0 ]; then
    awk -F "'" -v section="config interface 'wan6'" '
	    $0==section{flag=1;print;next}
		/config/{flag=0}
		flag==1&&/option enabled/{gsub(0,1)}
		{print}' $FILEDIR 1<>$FILEDIR
else
	awk -F "'" -v section="config interface 'wan6'" '
	    $0==section{flag=1;print;next}
		/config/{flag=0}
		flag==1&&/option enabled/{gsub(1,0)}
		{print}' $FILEDIR 1<>$FILEDIR
fi
if [ $vth7ISP != 0 ]; then
    awk -F "'" -v section="config interface 'wan7'" '
	    $0==section{flag=1;print;next}
		/config/{flag=0}
		flag==1&&/option enabled/{gsub(0,1)}
		{print}' $FILEDIR 1<>$FILEDIR
else
	awk -F "'" -v section="config interface 'wan7'" '
	    $0==section{flag=1;print;next}
		/config/{flag=0}
		flag==1&&/option enabled/{gsub(1,0)}
		{print}' $FILEDIR 1<>$FILEDIR
fi
/etc/init.d/mwan3 restart

3. 保存Shell脚本

  1)脚本文件创建

  在OpenWrt终端内,运行如下命令,将脚本文件创建在/etc/config文件夹下(也可自行选择保存路径)。

vim /etc/config/nw_log.bash

  按键盘按键 “i”,进入编辑状态,将上述完整的Shell脚本输入或粘贴至脚本文件内。注意:需要在脚本最顶部插入一行作为第一行,输入下列脚本解释器声明代码。

#!/bin/bash

  编辑完毕后,按键盘按键 “ESC”,退出编辑状态。并输入如下代码后按回车按键,保存并关闭文件。

:wq

  2)为Shell脚本赋予执行权限

  在OpenWrt终端内,运行如下命令。

chmod +x /etc/config/nw_log.bash

4. 执行Shell脚本

  在OpenWrt终端内,运行如下命令。

sh /etc/config/nw_log.bash

四、开机自动执行Shell脚本

  登录OpenWrt的Web管理面板,选择 “系统->启动项” 菜单,并切换至 “本地启动脚本” 选项卡。在exit 0 上方,输入如下代码并点击右下角保存按钮。

sleep 10

# Log in to the network.
sh /etc/config/nw_log.bash

  sleep 10 为延迟10s再执行,开机后各接口将注册并启用,此处的延时可以等待各接口成功从服务器获取到IP地址,对于需要IP地址来认证登录宽带账户的请求而言非常重要。

  重启服务器,可以在 “网络->负载均衡” 的 “接口” 选项卡下,可以看到成功认证登录的接口为启用状态。

附录、完整的Shell认证登录脚本代码

   【提示】采用数组等结构,可以实现部分代码的复用,简化代码结构。但系统本身的Shell脚本不具备数组这类结构,需查找资料另行安装配置。

  【注意】不同的网页登录认证页面,所发送的登录请求也不一样。代码仅供参考,您至少需要理解代码自行修改运营商标识NClassawk命令cURL请求代码 才能正常使用。

#!/bin/bash

# Essential information.
# China Mobile. ISP=1
N1Class="2"
N1User="**********2"
N1Pass="******"

# China Telecom. ISP=2
N2Class="3"
N2User="**********5"
N2Pass="******"

# China Unicom ISP=3
N3Class="4"
N3User="**********1"
N3Pass="******"

# ISP selection and Enablement. ISP 1~3: CMob,CTel,CUni. ISP0: Disabled.
vth0ISP=2
vth1ISP=2
vth2ISP=1
vth3ISP=1
vth4ISP=1
vth5ISP=0
vth6ISP=0
vth7ISP=0

FILEDIR=/etc/config/mwan3
# Log in to the network in turn.
# ============ vth0 ============
ISP=$vth0ISP
# Open only one mwan interface.
awk -F "'" -v section="config interface 'wan0'" '
    $0==section{flag=1;print;next}
    /config/{flag=0}
    flag==1&&/option enabled/{gsub(0,1)}
    {print}' $FILEDIR 1<>$FILEDIR
awk -F "'" -v section="config interface 'wan1'" '
    $0==section{flag=1;print;next}
    /config/{flag=0}
    flag==1&&/option enabled/{gsub(1,0)}
    {print}' $FILEDIR 1<>$FILEDIR
awk -F "'" -v section="config interface 'wan2'" '
    $0==section{flag=1;print;next}
    /config/{flag=0}
    flag==1&&/option enabled/{gsub(1,0)}
    {print}' $FILEDIR 1<>$FILEDIR
awk -F "'" -v section="config interface 'wan3'" '
    $0==section{flag=1;print;next}
    /config/{flag=0}
    flag==1&&/option enabled/{gsub(1,0)}
    {print}' $FILEDIR 1<>$FILEDIR
awk -F "'" -v section="config interface 'wan4'" '
    $0==section{flag=1;print;next}
    /config/{flag=0}
    flag==1&&/option enabled/{gsub(1,0)}
    {print}' $FILEDIR 1<>$FILEDIR
awk -F "'" -v section="config interface 'wan5'" '
    $0==section{flag=1;print;next}
    /config/{flag=0}
    flag==1&&/option enabled/{gsub(1,0)}
    {print}' $FILEDIR 1<>$FILEDIR
awk -F "'" -v section="config interface 'wan6'" '
    $0==section{flag=1;print;next}
    /config/{flag=0}
    flag==1&&/option enabled/{gsub(1,0)}
    {print}' $FILEDIR 1<>$FILEDIR
awk -F "'" -v section="config interface 'wan7'" '
    $0==section{flag=1;print;next}
    /config/{flag=0}
    flag==1&&/option enabled/{gsub(1,0)}
    {print}' $FILEDIR 1<>$FILEDIR
/etc/init.d/mwan3 restart

# Get the IP address of the current network card.
CUIP=$(ifconfig vth0 |grep 'inet addr:'|cut -d ' ' -f 12|cut -d ':' -f 2)

# Log in through curl. This part is the general code of vth0 ~ vth7.
if [ $ISP == 1 ]; then
    NStat=$(curl 'http://10.255.255.34/api/v1/login' \
      -H 'Connection: keep-alive' \
      -H 'Accept: application/json, text/plain, */*' \
      -H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.82 Safari/537.36' \
      -H 'Content-Type: application/json;charset=UTF-8' \
      -H 'Origin: http://10.255.255.34' \
      -H 'Referer: http://10.255.255.34/authentication/login' \
      -H 'Accept-Language: zh-CN,zh;q=0.9' \
      --data-raw '{"username":"'$N1User'","password":"'$N1Pass'","channel":"'$N1Class'","ifautologin":"0","pagesign":"secondauth","usripadd":"'$CUIP'"}')
elif [ $ISP == 2 ]; then
    NStat=$(curl 'http://10.255.255.34/api/v1/login' \
      -H 'Connection: keep-alive' \
      -H 'Accept: application/json, text/plain, */*' \
      -H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.82 Safari/537.36' \
      -H 'Content-Type: application/json;charset=UTF-8' \
      -H 'Origin: http://10.255.255.34' \
      -H 'Referer: http://10.255.255.34/authentication/login' \
      -H 'Accept-Language: zh-CN,zh;q=0.9' \
      --data-raw '{"username":"'$N2User'","password":"'$N2Pass'","channel":"'$N2Class'","ifautologin":"0","pagesign":"secondauth","usripadd":"'$CUIP'"}')
elif [ $ISP == 3 ]; then
   NStat=$(curl 'http://10.255.255.34/api/v1/login' \
      -H 'Connection: keep-alive' \
      -H 'Accept: application/json, text/plain, */*' \
      -H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.82 Safari/537.36' \
      -H 'Content-Type: application/json;charset=UTF-8' \
      -H 'Origin: http://10.255.255.34' \
      -H 'Referer: http://10.255.255.34/authentication/login' \
      -H 'Accept-Language: zh-CN,zh;q=0.9' \
      --data-raw '{"username":"'$N3User'","password":"'$N3Pass'","channel":"'$N3Class'","ifautologin":"0","pagesign":"secondauth","usripadd":"'$CUIP'"}')
fi

# Judge whether the login is successful.
if [ $ISP != 0 ]; then
    echo "${NStat}"
    NCode=$(echo "${NStat}" | grep "code" | cut -d " " -f 2 | cut -d "," -f 1)
    if [ $NCode != 200 ]; then
        # Login failed.
    	vth0ISP=0
    fi
fi

# ============ vth1 ============
ISP=$vth1ISP
# Open only one mwan interface.
awk -F "'" -v section="config interface 'wan0'" '
    $0==section{flag=1;print;next}
    /config/{flag=0}
    flag==1&&/option enabled/{gsub(1,0)}
    {print}' $FILEDIR 1<>$FILEDIR
awk -F "'" -v section="config interface 'wan1'" '
    $0==section{flag=1;print;next}
    /config/{flag=0}
    flag==1&&/option enabled/{gsub(0,1)}
    {print}' $FILEDIR 1<>$FILEDIR
awk -F "'" -v section="config interface 'wan2'" '
    $0==section{flag=1;print;next}
    /config/{flag=0}
    flag==1&&/option enabled/{gsub(1,0)}
    {print}' $FILEDIR 1<>$FILEDIR
awk -F "'" -v section="config interface 'wan3'" '
    $0==section{flag=1;print;next}
    /config/{flag=0}
    flag==1&&/option enabled/{gsub(1,0)}
    {print}' $FILEDIR 1<>$FILEDIR
awk -F "'" -v section="config interface 'wan4'" '
    $0==section{flag=1;print;next}
    /config/{flag=0}
    flag==1&&/option enabled/{gsub(1,0)}
    {print}' $FILEDIR 1<>$FILEDIR
awk -F "'" -v section="config interface 'wan5'" '
    $0==section{flag=1;print;next}
    /config/{flag=0}
    flag==1&&/option enabled/{gsub(1,0)}
    {print}' $FILEDIR 1<>$FILEDIR
awk -F "'" -v section="config interface 'wan6'" '
    $0==section{flag=1;print;next}
    /config/{flag=0}
    flag==1&&/option enabled/{gsub(1,0)}
    {print}' $FILEDIR 1<>$FILEDIR
awk -F "'" -v section="config interface 'wan7'" '
    $0==section{flag=1;print;next}
    /config/{flag=0}
    flag==1&&/option enabled/{gsub(1,0)}
    {print}' $FILEDIR 1<>$FILEDIR
/etc/init.d/mwan3 restart

# Get the IP address of the current network card.
CUIP=$(ifconfig vth1 |grep 'inet addr:'|cut -d ' ' -f 12|cut -d ':' -f 2)

# Log in through curl. This part is the general code of vth0 ~ vth7.
if [ $ISP == 1 ]; then
    NStat=$(curl 'http://10.255.255.34/api/v1/login' \
      -H 'Connection: keep-alive' \
      -H 'Accept: application/json, text/plain, */*' \
      -H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.82 Safari/537.36' \
      -H 'Content-Type: application/json;charset=UTF-8' \
      -H 'Origin: http://10.255.255.34' \
      -H 'Referer: http://10.255.255.34/authentication/login' \
      -H 'Accept-Language: zh-CN,zh;q=0.9' \
      --data-raw '{"username":"'$N1User'","password":"'$N1Pass'","channel":"'$N1Class'","ifautologin":"0","pagesign":"secondauth","usripadd":"'$CUIP'"}')
elif [ $ISP == 2 ]; then
    NStat=$(curl 'http://10.255.255.34/api/v1/login' \
      -H 'Connection: keep-alive' \
      -H 'Accept: application/json, text/plain, */*' \
      -H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.82 Safari/537.36' \
      -H 'Content-Type: application/json;charset=UTF-8' \
      -H 'Origin: http://10.255.255.34' \
      -H 'Referer: http://10.255.255.34/authentication/login' \
      -H 'Accept-Language: zh-CN,zh;q=0.9' \
      --data-raw '{"username":"'$N2User'","password":"'$N2Pass'","channel":"'$N2Class'","ifautologin":"0","pagesign":"secondauth","usripadd":"'$CUIP'"}')
elif [ $ISP == 3 ]; then
   NStat=$(curl 'http://10.255.255.34/api/v1/login' \
      -H 'Connection: keep-alive' \
      -H 'Accept: application/json, text/plain, */*' \
      -H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.82 Safari/537.36' \
      -H 'Content-Type: application/json;charset=UTF-8' \
      -H 'Origin: http://10.255.255.34' \
      -H 'Referer: http://10.255.255.34/authentication/login' \
      -H 'Accept-Language: zh-CN,zh;q=0.9' \
      --data-raw '{"username":"'$N3User'","password":"'$N3Pass'","channel":"'$N3Class'","ifautologin":"0","pagesign":"secondauth","usripadd":"'$CUIP'"}')
fi

# Judge whether the login is successful.
if [ $ISP != 0 ]; then
    echo "${NStat}"
    NCode=$(echo "${NStat}" | grep "code" | cut -d " " -f 2 | cut -d "," -f 1)
    if [ $NCode != 200 ]; then
        # Login failed.
    	vth1ISP=0
    fi
fi

# ============ vth2 ============
ISP=$vth2ISP
# Open only one mwan interface.
awk -F "'" -v section="config interface 'wan0'" '
    $0==section{flag=1;print;next}
    /config/{flag=0}
    flag==1&&/option enabled/{gsub(1,0)}
    {print}' $FILEDIR 1<>$FILEDIR
awk -F "'" -v section="config interface 'wan1'" '
    $0==section{flag=1;print;next}
    /config/{flag=0}
    flag==1&&/option enabled/{gsub(1,0)}
    {print}' $FILEDIR 1<>$FILEDIR
awk -F "'" -v section="config interface 'wan2'" '
    $0==section{flag=1;print;next}
    /config/{flag=0}
    flag==1&&/option enabled/{gsub(0,1)}
    {print}' $FILEDIR 1<>$FILEDIR
awk -F "'" -v section="config interface 'wan3'" '
    $0==section{flag=1;print;next}
    /config/{flag=0}
    flag==1&&/option enabled/{gsub(1,0)}
    {print}' $FILEDIR 1<>$FILEDIR
awk -F "'" -v section="config interface 'wan4'" '
    $0==section{flag=1;print;next}
    /config/{flag=0}
    flag==1&&/option enabled/{gsub(1,0)}
    {print}' $FILEDIR 1<>$FILEDIR
awk -F "'" -v section="config interface 'wan5'" '
    $0==section{flag=1;print;next}
    /config/{flag=0}
    flag==1&&/option enabled/{gsub(1,0)}
    {print}' $FILEDIR 1<>$FILEDIR
awk -F "'" -v section="config interface 'wan6'" '
    $0==section{flag=1;print;next}
    /config/{flag=0}
    flag==1&&/option enabled/{gsub(1,0)}
    {print}' $FILEDIR 1<>$FILEDIR
awk -F "'" -v section="config interface 'wan7'" '
    $0==section{flag=1;print;next}
    /config/{flag=0}
    flag==1&&/option enabled/{gsub(1,0)}
    {print}' $FILEDIR 1<>$FILEDIR
/etc/init.d/mwan3 restart

# Get the IP address of the current network card.
CUIP=$(ifconfig vth2 |grep 'inet addr:'|cut -d ' ' -f 12|cut -d ':' -f 2)

# Log in through curl. This part is the general code of vth0 ~ vth7.
if [ $ISP == 1 ]; then
    NStat=$(curl 'http://10.255.255.34/api/v1/login' \
      -H 'Connection: keep-alive' \
      -H 'Accept: application/json, text/plain, */*' \
      -H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.82 Safari/537.36' \
      -H 'Content-Type: application/json;charset=UTF-8' \
      -H 'Origin: http://10.255.255.34' \
      -H 'Referer: http://10.255.255.34/authentication/login' \
      -H 'Accept-Language: zh-CN,zh;q=0.9' \
      --data-raw '{"username":"'$N1User'","password":"'$N1Pass'","channel":"'$N1Class'","ifautologin":"0","pagesign":"secondauth","usripadd":"'$CUIP'"}')
elif [ $ISP == 2 ]; then
    NStat=$(curl 'http://10.255.255.34/api/v1/login' \
      -H 'Connection: keep-alive' \
      -H 'Accept: application/json, text/plain, */*' \
      -H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.82 Safari/537.36' \
      -H 'Content-Type: application/json;charset=UTF-8' \
      -H 'Origin: http://10.255.255.34' \
      -H 'Referer: http://10.255.255.34/authentication/login' \
      -H 'Accept-Language: zh-CN,zh;q=0.9' \
      --data-raw '{"username":"'$N2User'","password":"'$N2Pass'","channel":"'$N2Class'","ifautologin":"0","pagesign":"secondauth","usripadd":"'$CUIP'"}')
elif [ $ISP == 3 ]; then
   NStat=$(curl 'http://10.255.255.34/api/v1/login' \
      -H 'Connection: keep-alive' \
      -H 'Accept: application/json, text/plain, */*' \
      -H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.82 Safari/537.36' \
      -H 'Content-Type: application/json;charset=UTF-8' \
      -H 'Origin: http://10.255.255.34' \
      -H 'Referer: http://10.255.255.34/authentication/login' \
      -H 'Accept-Language: zh-CN,zh;q=0.9' \
      --data-raw '{"username":"'$N3User'","password":"'$N3Pass'","channel":"'$N3Class'","ifautologin":"0","pagesign":"secondauth","usripadd":"'$CUIP'"}')
fi

# Judge whether the login is successful.
if [ $ISP != 0 ]; then
    echo "${NStat}"
    NCode=$(echo "${NStat}" | grep "code" | cut -d " " -f 2 | cut -d "," -f 1)
    if [ $NCode != 200 ]; then
        # Login failed.
    	vth2ISP=0
    fi
fi

# ============ vth3 ============
ISP=$vth3ISP
# Open only one mwan interface.
awk -F "'" -v section="config interface 'wan0'" '
    $0==section{flag=1;print;next}
    /config/{flag=0}
    flag==1&&/option enabled/{gsub(1,0)}
    {print}' $FILEDIR 1<>$FILEDIR
awk -F "'" -v section="config interface 'wan1'" '
    $0==section{flag=1;print;next}
    /config/{flag=0}
    flag==1&&/option enabled/{gsub(1,0)}
    {print}' $FILEDIR 1<>$FILEDIR
awk -F "'" -v section="config interface 'wan2'" '
    $0==section{flag=1;print;next}
    /config/{flag=0}
    flag==1&&/option enabled/{gsub(1,0)}
    {print}' $FILEDIR 1<>$FILEDIR
awk -F "'" -v section="config interface 'wan3'" '
    $0==section{flag=1;print;next}
    /config/{flag=0}
    flag==1&&/option enabled/{gsub(0,1)}
    {print}' $FILEDIR 1<>$FILEDIR
awk -F "'" -v section="config interface 'wan4'" '
    $0==section{flag=1;print;next}
    /config/{flag=0}
    flag==1&&/option enabled/{gsub(1,0)}
    {print}' $FILEDIR 1<>$FILEDIR
awk -F "'" -v section="config interface 'wan5'" '
    $0==section{flag=1;print;next}
    /config/{flag=0}
    flag==1&&/option enabled/{gsub(1,0)}
    {print}' $FILEDIR 1<>$FILEDIR
awk -F "'" -v section="config interface 'wan6'" '
    $0==section{flag=1;print;next}
    /config/{flag=0}
    flag==1&&/option enabled/{gsub(1,0)}
    {print}' $FILEDIR 1<>$FILEDIR
awk -F "'" -v section="config interface 'wan7'" '
    $0==section{flag=1;print;next}
    /config/{flag=0}
    flag==1&&/option enabled/{gsub(1,0)}
    {print}' $FILEDIR 1<>$FILEDIR
/etc/init.d/mwan3 restart

# Get the IP address of the current network card.
CUIP=$(ifconfig vth3 |grep 'inet addr:'|cut -d ' ' -f 12|cut -d ':' -f 2)

# Log in through curl. This part is the general code of vth0 ~ vth7.
if [ $ISP == 1 ]; then
    NStat=$(curl 'http://10.255.255.34/api/v1/login' \
      -H 'Connection: keep-alive' \
      -H 'Accept: application/json, text/plain, */*' \
      -H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.82 Safari/537.36' \
      -H 'Content-Type: application/json;charset=UTF-8' \
      -H 'Origin: http://10.255.255.34' \
      -H 'Referer: http://10.255.255.34/authentication/login' \
      -H 'Accept-Language: zh-CN,zh;q=0.9' \
      --data-raw '{"username":"'$N1User'","password":"'$N1Pass'","channel":"'$N1Class'","ifautologin":"0","pagesign":"secondauth","usripadd":"'$CUIP'"}')
elif [ $ISP == 2 ]; then
    NStat=$(curl 'http://10.255.255.34/api/v1/login' \
      -H 'Connection: keep-alive' \
      -H 'Accept: application/json, text/plain, */*' \
      -H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.82 Safari/537.36' \
      -H 'Content-Type: application/json;charset=UTF-8' \
      -H 'Origin: http://10.255.255.34' \
      -H 'Referer: http://10.255.255.34/authentication/login' \
      -H 'Accept-Language: zh-CN,zh;q=0.9' \
      --data-raw '{"username":"'$N2User'","password":"'$N2Pass'","channel":"'$N2Class'","ifautologin":"0","pagesign":"secondauth","usripadd":"'$CUIP'"}')
elif [ $ISP == 3 ]; then
   NStat=$(curl 'http://10.255.255.34/api/v1/login' \
      -H 'Connection: keep-alive' \
      -H 'Accept: application/json, text/plain, */*' \
      -H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.82 Safari/537.36' \
      -H 'Content-Type: application/json;charset=UTF-8' \
      -H 'Origin: http://10.255.255.34' \
      -H 'Referer: http://10.255.255.34/authentication/login' \
      -H 'Accept-Language: zh-CN,zh;q=0.9' \
      --data-raw '{"username":"'$N3User'","password":"'$N3Pass'","channel":"'$N3Class'","ifautologin":"0","pagesign":"secondauth","usripadd":"'$CUIP'"}')
fi

# Judge whether the login is successful.
if [ $ISP != 0 ]; then
    echo "${NStat}"
    NCode=$(echo "${NStat}" | grep "code" | cut -d " " -f 2 | cut -d "," -f 1)
    if [ $NCode != 200 ]; then
        # Login failed.
    	vth3ISP=0
    fi
fi

# ============ vth4 ============
ISP=$vth4ISP
# Open only one mwan interface.
awk -F "'" -v section="config interface 'wan0'" '
    $0==section{flag=1;print;next}
    /config/{flag=0}
    flag==1&&/option enabled/{gsub(1,0)}
    {print}' $FILEDIR 1<>$FILEDIR
awk -F "'" -v section="config interface 'wan1'" '
    $0==section{flag=1;print;next}
    /config/{flag=0}
    flag==1&&/option enabled/{gsub(1,0)}
    {print}' $FILEDIR 1<>$FILEDIR
awk -F "'" -v section="config interface 'wan2'" '
    $0==section{flag=1;print;next}
    /config/{flag=0}
    flag==1&&/option enabled/{gsub(1,0)}
    {print}' $FILEDIR 1<>$FILEDIR
awk -F "'" -v section="config interface 'wan3'" '
    $0==section{flag=1;print;next}
    /config/{flag=0}
    flag==1&&/option enabled/{gsub(1,0)}
    {print}' $FILEDIR 1<>$FILEDIR
awk -F "'" -v section="config interface 'wan4'" '
    $0==section{flag=1;print;next}
    /config/{flag=0}
    flag==1&&/option enabled/{gsub(0,1)}
    {print}' $FILEDIR 1<>$FILEDIR
awk -F "'" -v section="config interface 'wan5'" '
    $0==section{flag=1;print;next}
    /config/{flag=0}
    flag==1&&/option enabled/{gsub(1,0)}
    {print}' $FILEDIR 1<>$FILEDIR
awk -F "'" -v section="config interface 'wan6'" '
    $0==section{flag=1;print;next}
    /config/{flag=0}
    flag==1&&/option enabled/{gsub(1,0)}
    {print}' $FILEDIR 1<>$FILEDIR
awk -F "'" -v section="config interface 'wan7'" '
    $0==section{flag=1;print;next}
    /config/{flag=0}
    flag==1&&/option enabled/{gsub(1,0)}
    {print}' $FILEDIR 1<>$FILEDIR
/etc/init.d/mwan3 restart

# Get the IP address of the current network card.
CUIP=$(ifconfig vth4 |grep 'inet addr:'|cut -d ' ' -f 12|cut -d ':' -f 2)

# Log in through curl. This part is the general code of vth0 ~ vth7.
if [ $ISP == 1 ]; then
    NStat=$(curl 'http://10.255.255.34/api/v1/login' \
      -H 'Connection: keep-alive' \
      -H 'Accept: application/json, text/plain, */*' \
      -H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.82 Safari/537.36' \
      -H 'Content-Type: application/json;charset=UTF-8' \
      -H 'Origin: http://10.255.255.34' \
      -H 'Referer: http://10.255.255.34/authentication/login' \
      -H 'Accept-Language: zh-CN,zh;q=0.9' \
      --data-raw '{"username":"'$N1User'","password":"'$N1Pass'","channel":"'$N1Class'","ifautologin":"0","pagesign":"secondauth","usripadd":"'$CUIP'"}')
elif [ $ISP == 2 ]; then
    NStat=$(curl 'http://10.255.255.34/api/v1/login' \
      -H 'Connection: keep-alive' \
      -H 'Accept: application/json, text/plain, */*' \
      -H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.82 Safari/537.36' \
      -H 'Content-Type: application/json;charset=UTF-8' \
      -H 'Origin: http://10.255.255.34' \
      -H 'Referer: http://10.255.255.34/authentication/login' \
      -H 'Accept-Language: zh-CN,zh;q=0.9' \
      --data-raw '{"username":"'$N2User'","password":"'$N2Pass'","channel":"'$N2Class'","ifautologin":"0","pagesign":"secondauth","usripadd":"'$CUIP'"}')
elif [ $ISP == 3 ]; then
   NStat=$(curl 'http://10.255.255.34/api/v1/login' \
      -H 'Connection: keep-alive' \
      -H 'Accept: application/json, text/plain, */*' \
      -H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.82 Safari/537.36' \
      -H 'Content-Type: application/json;charset=UTF-8' \
      -H 'Origin: http://10.255.255.34' \
      -H 'Referer: http://10.255.255.34/authentication/login' \
      -H 'Accept-Language: zh-CN,zh;q=0.9' \
      --data-raw '{"username":"'$N3User'","password":"'$N3Pass'","channel":"'$N3Class'","ifautologin":"0","pagesign":"secondauth","usripadd":"'$CUIP'"}')
fi

# Judge whether the login is successful.
if [ $ISP != 0 ]; then
    echo "${NStat}"
    NCode=$(echo "${NStat}" | grep "code" | cut -d " " -f 2 | cut -d "," -f 1)
    if [ $NCode != 200 ]; then
        # Login failed.
    	vth4ISP=0
    fi
fi

# ============ vth5 ============
ISP=$vth5ISP
# Open only one mwan interface.
awk -F "'" -v section="config interface 'wan0'" '
    $0==section{flag=1;print;next}
    /config/{flag=0}
    flag==1&&/option enabled/{gsub(1,0)}
    {print}' $FILEDIR 1<>$FILEDIR
awk -F "'" -v section="config interface 'wan1'" '
    $0==section{flag=1;print;next}
    /config/{flag=0}
    flag==1&&/option enabled/{gsub(1,0)}
    {print}' $FILEDIR 1<>$FILEDIR
awk -F "'" -v section="config interface 'wan2'" '
    $0==section{flag=1;print;next}
    /config/{flag=0}
    flag==1&&/option enabled/{gsub(1,0)}
    {print}' $FILEDIR 1<>$FILEDIR
awk -F "'" -v section="config interface 'wan3'" '
    $0==section{flag=1;print;next}
    /config/{flag=0}
    flag==1&&/option enabled/{gsub(1,0)}
    {print}' $FILEDIR 1<>$FILEDIR
awk -F "'" -v section="config interface 'wan4'" '
    $0==section{flag=1;print;next}
    /config/{flag=0}
    flag==1&&/option enabled/{gsub(1,0)}
    {print}' $FILEDIR 1<>$FILEDIR
awk -F "'" -v section="config interface 'wan5'" '
    $0==section{flag=1;print;next}
    /config/{flag=0}
    flag==1&&/option enabled/{gsub(0,1)}
    {print}' $FILEDIR 1<>$FILEDIR
awk -F "'" -v section="config interface 'wan6'" '
    $0==section{flag=1;print;next}
    /config/{flag=0}
    flag==1&&/option enabled/{gsub(1,0)}
    {print}' $FILEDIR 1<>$FILEDIR
awk -F "'" -v section="config interface 'wan7'" '
    $0==section{flag=1;print;next}
    /config/{flag=0}
    flag==1&&/option enabled/{gsub(1,0)}
    {print}' $FILEDIR 1<>$FILEDIR
/etc/init.d/mwan3 restart

# Get the IP address of the current network card.
CUIP=$(ifconfig vth5 |grep 'inet addr:'|cut -d ' ' -f 12|cut -d ':' -f 2)

# Log in through curl. This part is the general code of vth0 ~ vth7.
if [ $ISP == 1 ]; then
    NStat=$(curl 'http://10.255.255.34/api/v1/login' \
      -H 'Connection: keep-alive' \
      -H 'Accept: application/json, text/plain, */*' \
      -H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.82 Safari/537.36' \
      -H 'Content-Type: application/json;charset=UTF-8' \
      -H 'Origin: http://10.255.255.34' \
      -H 'Referer: http://10.255.255.34/authentication/login' \
      -H 'Accept-Language: zh-CN,zh;q=0.9' \
      --data-raw '{"username":"'$N1User'","password":"'$N1Pass'","channel":"'$N1Class'","ifautologin":"0","pagesign":"secondauth","usripadd":"'$CUIP'"}')
elif [ $ISP == 2 ]; then
    NStat=$(curl 'http://10.255.255.34/api/v1/login' \
      -H 'Connection: keep-alive' \
      -H 'Accept: application/json, text/plain, */*' \
      -H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.82 Safari/537.36' \
      -H 'Content-Type: application/json;charset=UTF-8' \
      -H 'Origin: http://10.255.255.34' \
      -H 'Referer: http://10.255.255.34/authentication/login' \
      -H 'Accept-Language: zh-CN,zh;q=0.9' \
      --data-raw '{"username":"'$N2User'","password":"'$N2Pass'","channel":"'$N2Class'","ifautologin":"0","pagesign":"secondauth","usripadd":"'$CUIP'"}')
elif [ $ISP == 3 ]; then
   NStat=$(curl 'http://10.255.255.34/api/v1/login' \
      -H 'Connection: keep-alive' \
      -H 'Accept: application/json, text/plain, */*' \
      -H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.82 Safari/537.36' \
      -H 'Content-Type: application/json;charset=UTF-8' \
      -H 'Origin: http://10.255.255.34' \
      -H 'Referer: http://10.255.255.34/authentication/login' \
      -H 'Accept-Language: zh-CN,zh;q=0.9' \
      --data-raw '{"username":"'$N3User'","password":"'$N3Pass'","channel":"'$N3Class'","ifautologin":"0","pagesign":"secondauth","usripadd":"'$CUIP'"}')
fi

# Judge whether the login is successful.
if [ $ISP != 0 ]; then
    echo "${NStat}"
    NCode=$(echo "${NStat}" | grep "code" | cut -d " " -f 2 | cut -d "," -f 1)
    if [ $NCode != 200 ]; then
        # Login failed.
    	vth5ISP=0
    fi
fi

# ============ vth6 ============
ISP=$vth6ISP
# Open only one mwan interface.
awk -F "'" -v section="config interface 'wan0'" '
    $0==section{flag=1;print;next}
    /config/{flag=0}
    flag==1&&/option enabled/{gsub(1,0)}
    {print}' $FILEDIR 1<>$FILEDIR
awk -F "'" -v section="config interface 'wan1'" '
    $0==section{flag=1;print;next}
    /config/{flag=0}
    flag==1&&/option enabled/{gsub(1,0)}
    {print}' $FILEDIR 1<>$FILEDIR
awk -F "'" -v section="config interface 'wan2'" '
    $0==section{flag=1;print;next}
    /config/{flag=0}
    flag==1&&/option enabled/{gsub(1,0)}
    {print}' $FILEDIR 1<>$FILEDIR
awk -F "'" -v section="config interface 'wan3'" '
    $0==section{flag=1;print;next}
    /config/{flag=0}
    flag==1&&/option enabled/{gsub(1,0)}
    {print}' $FILEDIR 1<>$FILEDIR
awk -F "'" -v section="config interface 'wan4'" '
    $0==section{flag=1;print;next}
    /config/{flag=0}
    flag==1&&/option enabled/{gsub(1,0)}
    {print}' $FILEDIR 1<>$FILEDIR
awk -F "'" -v section="config interface 'wan5'" '
    $0==section{flag=1;print;next}
    /config/{flag=0}
    flag==1&&/option enabled/{gsub(1,0)}
    {print}' $FILEDIR 1<>$FILEDIR
awk -F "'" -v section="config interface 'wan6'" '
    $0==section{flag=1;print;next}
    /config/{flag=0}
    flag==1&&/option enabled/{gsub(0,1)}
    {print}' $FILEDIR 1<>$FILEDIR
awk -F "'" -v section="config interface 'wan7'" '
    $0==section{flag=1;print;next}
    /config/{flag=0}
    flag==1&&/option enabled/{gsub(1,0)}
    {print}' $FILEDIR 1<>$FILEDIR
/etc/init.d/mwan3 restart

# Get the IP address of the current network card.
CUIP=$(ifconfig vth6 |grep 'inet addr:'|cut -d ' ' -f 12|cut -d ':' -f 2)

# Log in through curl. This part is the general code of vth0 ~ vth7.
if [ $ISP == 1 ]; then
    NStat=$(curl 'http://10.255.255.34/api/v1/login' \
      -H 'Connection: keep-alive' \
      -H 'Accept: application/json, text/plain, */*' \
      -H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.82 Safari/537.36' \
      -H 'Content-Type: application/json;charset=UTF-8' \
      -H 'Origin: http://10.255.255.34' \
      -H 'Referer: http://10.255.255.34/authentication/login' \
      -H 'Accept-Language: zh-CN,zh;q=0.9' \
      --data-raw '{"username":"'$N1User'","password":"'$N1Pass'","channel":"'$N1Class'","ifautologin":"0","pagesign":"secondauth","usripadd":"'$CUIP'"}')
elif [ $ISP == 2 ]; then
    NStat=$(curl 'http://10.255.255.34/api/v1/login' \
      -H 'Connection: keep-alive' \
      -H 'Accept: application/json, text/plain, */*' \
      -H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.82 Safari/537.36' \
      -H 'Content-Type: application/json;charset=UTF-8' \
      -H 'Origin: http://10.255.255.34' \
      -H 'Referer: http://10.255.255.34/authentication/login' \
      -H 'Accept-Language: zh-CN,zh;q=0.9' \
      --data-raw '{"username":"'$N2User'","password":"'$N2Pass'","channel":"'$N2Class'","ifautologin":"0","pagesign":"secondauth","usripadd":"'$CUIP'"}')
elif [ $ISP == 3 ]; then
   NStat=$(curl 'http://10.255.255.34/api/v1/login' \
      -H 'Connection: keep-alive' \
      -H 'Accept: application/json, text/plain, */*' \
      -H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.82 Safari/537.36' \
      -H 'Content-Type: application/json;charset=UTF-8' \
      -H 'Origin: http://10.255.255.34' \
      -H 'Referer: http://10.255.255.34/authentication/login' \
      -H 'Accept-Language: zh-CN,zh;q=0.9' \
      --data-raw '{"username":"'$N3User'","password":"'$N3Pass'","channel":"'$N3Class'","ifautologin":"0","pagesign":"secondauth","usripadd":"'$CUIP'"}')
fi

# Judge whether the login is successful.
if [ $ISP != 0 ]; then
    echo "${NStat}"
    NCode=$(echo "${NStat}" | grep "code" | cut -d " " -f 2 | cut -d "," -f 1)
    if [ $NCode != 200 ]; then
        # Login failed.
    	vth6ISP=0
    fi
fi

# ============ vth7 ============
ISP=$vth7ISP
# Open only one mwan interface.
awk -F "'" -v section="config interface 'wan0'" '
    $0==section{flag=1;print;next}
    /config/{flag=0}
    flag==1&&/option enabled/{gsub(1,0)}
    {print}' $FILEDIR 1<>$FILEDIR
awk -F "'" -v section="config interface 'wan1'" '
    $0==section{flag=1;print;next}
    /config/{flag=0}
    flag==1&&/option enabled/{gsub(1,0)}
    {print}' $FILEDIR 1<>$FILEDIR
awk -F "'" -v section="config interface 'wan2'" '
    $0==section{flag=1;print;next}
    /config/{flag=0}
    flag==1&&/option enabled/{gsub(1,0)}
    {print}' $FILEDIR 1<>$FILEDIR
awk -F "'" -v section="config interface 'wan3'" '
    $0==section{flag=1;print;next}
    /config/{flag=0}
    flag==1&&/option enabled/{gsub(1,0)}
    {print}' $FILEDIR 1<>$FILEDIR
awk -F "'" -v section="config interface 'wan4'" '
    $0==section{flag=1;print;next}
    /config/{flag=0}
    flag==1&&/option enabled/{gsub(1,0)}
    {print}' $FILEDIR 1<>$FILEDIR
awk -F "'" -v section="config interface 'wan5'" '
    $0==section{flag=1;print;next}
    /config/{flag=0}
    flag==1&&/option enabled/{gsub(1,0)}
    {print}' $FILEDIR 1<>$FILEDIR
awk -F "'" -v section="config interface 'wan6'" '
    $0==section{flag=1;print;next}
    /config/{flag=0}
    flag==1&&/option enabled/{gsub(1,0)}
    {print}' $FILEDIR 1<>$FILEDIR
awk -F "'" -v section="config interface 'wan7'" '
    $0==section{flag=1;print;next}
    /config/{flag=0}
    flag==1&&/option enabled/{gsub(0,1)}
    {print}' $FILEDIR 1<>$FILEDIR
/etc/init.d/mwan3 restart

# Get the IP address of the current network card.
CUIP=$(ifconfig vth7 |grep 'inet addr:'|cut -d ' ' -f 12|cut -d ':' -f 2)

# Log in through curl. This part is the general code of vth0 ~ vth7.
if [ $ISP == 1 ]; then
    NStat=$(curl 'http://10.255.255.34/api/v1/login' \
      -H 'Connection: keep-alive' \
      -H 'Accept: application/json, text/plain, */*' \
      -H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.82 Safari/537.36' \
      -H 'Content-Type: application/json;charset=UTF-8' \
      -H 'Origin: http://10.255.255.34' \
      -H 'Referer: http://10.255.255.34/authentication/login' \
      -H 'Accept-Language: zh-CN,zh;q=0.9' \
      --data-raw '{"username":"'$N1User'","password":"'$N1Pass'","channel":"'$N1Class'","ifautologin":"0","pagesign":"secondauth","usripadd":"'$CUIP'"}')
elif [ $ISP == 2 ]; then
    NStat=$(curl 'http://10.255.255.34/api/v1/login' \
      -H 'Connection: keep-alive' \
      -H 'Accept: application/json, text/plain, */*' \
      -H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.82 Safari/537.36' \
      -H 'Content-Type: application/json;charset=UTF-8' \
      -H 'Origin: http://10.255.255.34' \
      -H 'Referer: http://10.255.255.34/authentication/login' \
      -H 'Accept-Language: zh-CN,zh;q=0.9' \
      --data-raw '{"username":"'$N2User'","password":"'$N2Pass'","channel":"'$N2Class'","ifautologin":"0","pagesign":"secondauth","usripadd":"'$CUIP'"}')
elif [ $ISP == 3 ]; then
   NStat=$(curl 'http://10.255.255.34/api/v1/login' \
      -H 'Connection: keep-alive' \
      -H 'Accept: application/json, text/plain, */*' \
      -H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.82 Safari/537.36' \
      -H 'Content-Type: application/json;charset=UTF-8' \
      -H 'Origin: http://10.255.255.34' \
      -H 'Referer: http://10.255.255.34/authentication/login' \
      -H 'Accept-Language: zh-CN,zh;q=0.9' \
      --data-raw '{"username":"'$N3User'","password":"'$N3Pass'","channel":"'$N3Class'","ifautologin":"0","pagesign":"secondauth","usripadd":"'$CUIP'"}')
fi

# Judge whether the login is successful.
if [ $ISP != 0 ]; then
    echo "${NStat}"
    NCode=$(echo "${NStat}" | grep "code" | cut -d " " -f 2 | cut -d "," -f 1)
    if [ $NCode != 200 ]; then
        # Login failed.
    	vth7ISP=0
    fi
fi


# Start all mwan interface.
if [ $vth0ISP != 0 ]; then
    awk -F "'" -v section="config interface 'wan0'" '
	    $0==section{flag=1;print;next}
		/config/{flag=0}
		flag==1&&/option enabled/{gsub(0,1)}
		{print}' $FILEDIR 1<>$FILEDIR
else
	awk -F "'" -v section="config interface 'wan0'" '
	    $0==section{flag=1;print;next}
		/config/{flag=0}
		flag==1&&/option enabled/{gsub(1,0)}
		{print}' $FILEDIR 1<>$FILEDIR
fi
if [ $vth1ISP != 0 ]; then
    awk -F "'" -v section="config interface 'wan1'" '
	    $0==section{flag=1;print;next}
		/config/{flag=0}
		flag==1&&/option enabled/{gsub(0,1)}
		{print}' $FILEDIR 1<>$FILEDIR
else
	awk -F "'" -v section="config interface 'wan1'" '
	    $0==section{flag=1;print;next}
		/config/{flag=0}
		flag==1&&/option enabled/{gsub(1,0)}
		{print}' $FILEDIR 1<>$FILEDIR
fi
if [ $vth2ISP != 0 ]; then
    awk -F "'" -v section="config interface 'wan2'" '
	    $0==section{flag=1;print;next}
		/config/{flag=0}
		flag==1&&/option enabled/{gsub(0,1)}
		{print}' $FILEDIR 1<>$FILEDIR
else
	awk -F "'" -v section="config interface 'wan2'" '
	    $0==section{flag=1;print;next}
		/config/{flag=0}
		flag==1&&/option enabled/{gsub(1,0)}
		{print}' $FILEDIR 1<>$FILEDIR
fi
if [ $vth3ISP != 0 ]; then
    awk -F "'" -v section="config interface 'wan3'" '
	    $0==section{flag=1;print;next}
		/config/{flag=0}
		flag==1&&/option enabled/{gsub(0,1)}
		{print}' $FILEDIR 1<>$FILEDIR
else
	awk -F "'" -v section="config interface 'wan3'" '
	    $0==section{flag=1;print;next}
		/config/{flag=0}
		flag==1&&/option enabled/{gsub(1,0)}
		{print}' $FILEDIR 1<>$FILEDIR
fi
if [ $vth4ISP != 0 ]; then
    awk -F "'" -v section="config interface 'wan4'" '
	    $0==section{flag=1;print;next}
		/config/{flag=0}
		flag==1&&/option enabled/{gsub(0,1)}
		{print}' $FILEDIR 1<>$FILEDIR
else
	awk -F "'" -v section="config interface 'wan4'" '
	    $0==section{flag=1;print;next}
		/config/{flag=0}
		flag==1&&/option enabled/{gsub(1,0)}
		{print}' $FILEDIR 1<>$FILEDIR
fi
if [ $vth5ISP != 0 ]; then
    awk -F "'" -v section="config interface 'wan5'" '
	    $0==section{flag=1;print;next}
		/config/{flag=0}
		flag==1&&/option enabled/{gsub(0,1)}
		{print}' $FILEDIR 1<>$FILEDIR
else
	awk -F "'" -v section="config interface 'wan5'" '
	    $0==section{flag=1;print;next}
		/config/{flag=0}
		flag==1&&/option enabled/{gsub(1,0)}
		{print}' $FILEDIR 1<>$FILEDIR
fi
if [ $vth6ISP != 0 ]; then
    awk -F "'" -v section="config interface 'wan6'" '
	    $0==section{flag=1;print;next}
		/config/{flag=0}
		flag==1&&/option enabled/{gsub(0,1)}
		{print}' $FILEDIR 1<>$FILEDIR
else
	awk -F "'" -v section="config interface 'wan6'" '
	    $0==section{flag=1;print;next}
		/config/{flag=0}
		flag==1&&/option enabled/{gsub(1,0)}
		{print}' $FILEDIR 1<>$FILEDIR
fi
if [ $vth7ISP != 0 ]; then
    awk -F "'" -v section="config interface 'wan7'" '
	    $0==section{flag=1;print;next}
		/config/{flag=0}
		flag==1&&/option enabled/{gsub(0,1)}
		{print}' $FILEDIR 1<>$FILEDIR
else
	awk -F "'" -v section="config interface 'wan7'" '
	    $0==section{flag=1;print;next}
		/config/{flag=0}
		flag==1&&/option enabled/{gsub(1,0)}
		{print}' $FILEDIR 1<>$FILEDIR
fi
/etc/init.d/mwan3 restart

猜你喜欢

转载自blog.csdn.net/Cx2008Lxl/article/details/123116995