分类 网络 下的文章

windows使用ikev2遇到的坑,及批处理batch脚本和powershell脚本

2020.7.6更新:添加坑0,mtu问题

给公司测试ikev2 vpn,mac和ios一切正常,但是发现windows手动添加vpn有不少坑,总结一下,并附上批处理脚本(实际调用powershell)
以下在windows10下测试,ikev2服务器使用ubuntu下strongswan,代理所有路由,ikev2服务器安装脚本见之前文章https://www.willnet.net/index.php/archives/100/

坑0,windows的vpn虚拟网卡默认mtu是1400,
现象:如果服务端的mtu小于1400的话,会出现能ping通地址,但是网页不能打开的问题
解决办法:修改vpn虚拟网卡mtu值
连接vpn后,以管理员身份执行以下命令

netsh interface ipv4 set subinterface "vpn名称" mtu=1350  store=persistent

具体详见ikev2 客户端mtu引起的网络故障

坑1:
windows默认不支持DH2048_AES256协商协议
现象:提示策略匹配错误
解决办法:添加注册表项,不需要重启生效

HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\Rasman\Parameters里新建类型为DWORD的NegotiateDH2048_AES256 键,值设置为1

0 禁用AES-256-CBC和MODP-2048
1 启用AES-256-CBC和MODP-2048
2 强制使用AES-256-CBC和MODP-2048

坑2:
windows默认不支持Symmetric NAT类型路由器后的ikev2服务器
现象:提示809错误,或者提示路由器不支持nat
解决办法:添加注册表项,并需要重启生效

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\PolicyAgent里新建类型为DWORD的AssumeUDPEncapsulationContextOnSendRule 键,值设置为2

0
A value of 0 (zero) configures Windows so that it cannot establish security associations with servers that are located behind NAT devices. This is the default value.
1
A value of 1 configures Windows so that it can establish security associations with servers that are located behind NAT devices.
2
A value of 2 configures Windows so that it can establish security associations when both the server and the Windows Vista-based or Windows Server 2008-based VPN client computer are behind NAT devices.

坑3:
windows手动添加的vpn默认没有开启默认路由
现象:连接成功,但是对外ip没有改变
解决办法:手动添加修改或者使用powershell新建vpn连接,手动路径:控制面板\网络和 Internet\网络连接,vpn网卡,属性,网络,ipv4,属性,高级,勾选在远程网络上使用默认网关

坑4:
windows10默认开启了smart multi-homed name resolution功能
现象:有线网络下连接vpn后不使用vpn的dns
解决办法:修改vpn网卡的跃点数,小于有线网卡即可。脚本自动解决待更新

批处理脚本如下,实际是使用批处理调用powershell命令,由于需要修改注册表,需要管理员权限运行

install.cmd

@echo off&PUSHD %~DP0 &TITLE ikev2 VPN安装/卸载程序
mode con cols=160 lines=50
set TempFile_Name=%SystemRoot%\System32\BatTestUACin_SysRt%Random%.batemp
( echo "BAT Test UAC in Temp" >%TempFile_Name% ) 1>nul 2>nul
if exist %TempFile_Name% (
del %TempFile_Name% 1>nul 2>nul&&goto :address
) else (
echo;请以管理员身份运行,按任意键退出 &&goto :end
)

:address
cls
echo;请输入办公地点:
echo;1:北京
echo;2:成都
echo;3:重庆
set/p choose1=请输入选项并按回车:
echo %choose1%|findstr /i "[123]">nul&&goto :install
goto :address

:install
cls
echo;请选择安装或者卸载VPN(ikev2):
echo;i:安装
echo;u:卸载
echo;q:退出
set/p choose2=请输入选项并按回车:
echo %choose2%|findstr /i "[iuq]">nul&&goto :%choose1%%choose2%
goto :install

:1i
cls
echo;安装北京VPN
echo;===========================
echo;***步骤1:删除同名VPN***
(powershell -Command "& {Remove-VpnConnection -Name "北京办公室" -Force -PassThru;}") 1>nul 2>nul
echo;成功
echo;
echo;***步骤2:安装新的VPN***
powershell -Command "& {Add-VpnConnection -Name "北京办公室" -ServerAddress "beijing.example.com" -AuthenticationMethod "Eap" -EncryptionLevel "Maximum" -RememberCredential -TunnelType "Ikev2" -PassThru;}"
echo;成功
echo;
echo;***步骤3:清理注册表***
(powershell -Command "& {Remove-ItemProperty HKLM:\System\CurrentControlSet\Services\Rasman\Parameters -name "NegotiateDH2048_AES256"; Remove-ItemProperty HKLM:\System\CurrentControlSet\Services\PolicyAgent -name "AssumeUDPEncapsulationContextOnSendRule ";}") 1>nul 2>nul
echo;成功
echo;
echo;***步骤4:添加注册表***
(powershell -Command "& {New-ItemProperty HKLM:\System\CurrentControlSet\Services\Rasman\Parameters -name "NegotiateDH2048_AES256" -value 1 -propertyType dword; New-ItemProperty HKLM:\System\CurrentControlSet\Services\PolicyAgent -name "AssumeUDPEncapsulationContextOnSendRule " -value 2 -propertyType dword;}") 1>nul 2>nul
echo;成功
echo;
echo;***步骤5:设置注册表***
powershell -Command "& {Set-ItemProperty HKLM:\System\CurrentControlSet\Services\Rasman\Parameters -name "NegotiateDH2048_AES256" -value 1; Set-ItemProperty HKLM:\System\CurrentControlSet\Services\PolicyAgent -name "AssumeUDPEncapsulationContextOnSendRule " -value 2;}"
echo;成功
echo;
echo;***步骤6:重启电脑***
echo;按任意键重启电脑,或按右上角 X 退出后手动重启电脑
pause>nul
shutdown -r -t 0
goto :end

:1u
cls
echo;卸载北京VPN
echo;===========================
(powershell -Command "& {Remove-VpnConnection -Name "北京办公室" -Force -PassThru;}") 1>nul 2>nul
echo;成功,按任意键退出...
goto :end

:2i
cls
echo;安装成都VPN
echo;===========================
echo;***步骤1:删除同名VPN***
(powershell -Command "& {Remove-VpnConnection -Name "成都办公室" -Force -PassThru;}") 1>nul 2>nul
echo;成功
echo;
echo;***步骤2:安装新的VPN***
powershell -Command "& {Add-VpnConnection -Name "成都办公室" -ServerAddress "chengdu.example.com" -AuthenticationMethod "Eap" -EncryptionLevel "Maximum" -RememberCredential -TunnelType "Ikev2" -PassThru;}"
echo;成功
echo;
echo;***步骤3:清理注册表***
(powershell -Command "& {Remove-ItemProperty HKLM:\System\CurrentControlSet\Services\Rasman\Parameters -name "NegotiateDH2048_AES256"; Remove-ItemProperty HKLM:\System\CurrentControlSet\Services\PolicyAgent -name "AssumeUDPEncapsulationContextOnSendRule ";}") 1>nul 2>nul
echo;成功
echo;
echo;***步骤4:添加注册表***
(powershell -Command "& {New-ItemProperty HKLM:\System\CurrentControlSet\Services\Rasman\Parameters -name "NegotiateDH2048_AES256" -value 1 -propertyType dword; New-ItemProperty HKLM:\System\CurrentControlSet\Services\PolicyAgent -name "AssumeUDPEncapsulationContextOnSendRule " -value 2 -propertyType dword;}") 1>nul 2>nul
echo;成功
echo;
echo;***步骤5:设置注册表***
powershell -Command "& {Set-ItemProperty HKLM:\System\CurrentControlSet\Services\Rasman\Parameters -name "NegotiateDH2048_AES256" -value 1; Set-ItemProperty HKLM:\System\CurrentControlSet\Services\PolicyAgent -name "AssumeUDPEncapsulationContextOnSendRule " -value 2;}"
echo;成功
echo;
echo;***步骤6:重启电脑***
echo;按任意键重启电脑,或按右上角 X 退出后手动重启电脑
pause>nul
shutdown -r -t 0
goto :end

:2u
cls
echo;卸载成都VPN
echo;===========================
(powershell -Command "& {Remove-VpnConnection -Name "成都办公室" -Force -PassThru;}") 1>nul 2>nul
echo;成功,按任意键退出...
goto :end

:3i
cls
echo;安装重庆VPN
echo;===========================
echo;***步骤1:删除同名VPN***
(powershell -Command "& {Remove-VpnConnection -Name "重庆办公室" -Force -PassThru;}") 1>nul 2>nul
echo;成功
echo;
echo;***步骤2:安装新的VPN***
powershell -Command "& {Add-VpnConnection -Name "重庆办公室" -ServerAddress "chongqing.example.com" -AuthenticationMethod "Eap" -EncryptionLevel "Maximum" -RememberCredential -TunnelType "Ikev2" -PassThru;}"
echo;成功
echo;
echo;***步骤3:清理注册表***
(powershell -Command "& {Remove-ItemProperty HKLM:\System\CurrentControlSet\Services\Rasman\Parameters -name "NegotiateDH2048_AES256"; Remove-ItemProperty HKLM:\System\CurrentControlSet\Services\PolicyAgent -name "AssumeUDPEncapsulationContextOnSendRule ";}") 1>nul 2>nul
echo;成功
echo;
echo;***步骤4:添加注册表***
(powershell -Command "& {New-ItemProperty HKLM:\System\CurrentControlSet\Services\Rasman\Parameters -name "NegotiateDH2048_AES256" -value 1 -propertyType dword; New-ItemProperty HKLM:\System\CurrentControlSet\Services\PolicyAgent -name "AssumeUDPEncapsulationContextOnSendRule " -value 2 -propertyType dword;}") 1>nul 2>nul
echo;成功
echo;
echo;***步骤5:设置注册表***
powershell -Command "& {Set-ItemProperty HKLM:\System\CurrentControlSet\Services\Rasman\Parameters -name "NegotiateDH2048_AES256" -value 1; Set-ItemProperty HKLM:\System\CurrentControlSet\Services\PolicyAgent -name "AssumeUDPEncapsulationContextOnSendRule " -value 2;}"
echo;成功
echo;
echo;***步骤6:重启电脑***
echo;按任意键重启电脑,或按右上角 X 退出后手动重启电脑
pause>nul
shutdown -r -t 0
goto :end

:3u
cls
echo;卸载重庆VPN
echo;===========================
(powershell -Command "& {Remove-VpnConnection -Name "重庆办公室" -Force -PassThru;}") 1>nul 2>nul
echo;成功,按任意键退出...
goto :end

:end
pause>nul

powershell脚本,需要以管理员身份运行powershell然后执行install.ps1
install.ps1

Remove-VpnConnection -Name "成都办公室" -Force

Add-VpnConnection -Name "成都办公室" -ServerAddress "chengdu.example.com" -AuthenticationMethod "Eap" -EncryptionLevel "Maximum" -RememberCredential -TunnelType "Ikev2"

Remove-ItemProperty HKLM:\System\CurrentControlSet\Services\Rasman\Parameters -name "NegotiateDH2048_AES256"

Remove-ItemProperty HKLM:\System\CurrentControlSet\Services\PolicyAgent -name "AssumeUDPEncapsulationContextOnSendRule"

New-ItemProperty HKLM:\System\CurrentControlSet\Services\Rasman\Parameters -name "NegotiateDH2048_AES256" -value 1 -propertyType dword

New-ItemProperty HKLM:\System\CurrentControlSet\Services\PolicyAgent -name "AssumeUDPEncapsulationContextOnSendRule" -value 2 -propertyType dword

Set-ItemProperty HKLM:\System\CurrentControlSet\Services\Rasman\Parameters -name "NegotiateDH2048_AES256" -value 1

Set-ItemProperty HKLM:\System\CurrentControlSet\Services\PolicyAgent -name "AssumeUDPEncapsulationContextOnSendRule " -value 2

cisco交换机与netgear交换机配置链路聚合

以思科WS-C3560X-24P和网件GS116Ev2为例,由于GS116Ev2只支持static lag,所以以下为trunk的静态链路聚合。trunk可以理解为传递vlan的链接。如果只是某个vlan的链路聚合的话修改trunk为access。
思科配置

ssh -oHostKeyAlgorithms=+ssh-dss -oKexAlgorithms=+diffie-hellman-group1-sha1 cisco@ip
Password: 
Core_Switch>enable
Password: 
Core_Switch#configure terminal 
Enter configuration commands, one per line.  End with CNTL/Z.
Core_Switch(config)#interface Port-Channel 4
Core_Switch(config-if)#description To_GS116Ev2
Core_Switch(config-if)#switchport trunk encapsulation dot1q
Core_Switch(config-if)#switchport mode trunk
Core_Switch(config-if)#interface GigabitEthernet0/1
Core_Switch(config-if)#switchport trunk encapsulation dot1q
Core_Switch(config-if)#switchport mode trunk
Core_Switch(config-if)#channel-group 4 mode on
Core_Switch(config-if)#interface GigabitEthernet0/2
Core_Switch(config-if)#switchport trunk encapsulation dot1q
Core_Switch(config-if)#switchport mode trunk
Core_Switch(config-if)#channel-group 4 mode on
Core_Switch(config-if)#exit
Core_Switch(config)#exit
Core_Switch#

网件配置
1,选择需要聚合的端口,按图勾选配置,然后apply
网件1
2,启用聚合,如图,然后点击apply
网件2

至此,两台交换机配置完毕,连接两根网线,等待几分钟后完成。
思科交换机查看链路聚合状态方法

Core_Switch#show etherchannel summary 
Flags:  D - down        P - bundled in port-channel
        I - stand-alone s - suspended
        H - Hot-standby (LACP only)
        R - Layer3      S - Layer2
        U - in use      f - failed to allocate aggregator

        M - not in use, minimum links not met
        u - unsuitable for bundling
        w - waiting to be aggregated
        d - default port


Number of channel-groups in use: 4
Number of aggregators:           4

Group  Port-channel  Protocol    Ports
------+-------------+-----------+-----------------------------------------------
1      Po1(SU)          -        Gi0/21(P)   Gi0/22(P)   
2      Po2(SU)          -        Gi0/19(P)   Gi0/20(P)   
3      Po3(SU)          -        Gi0/17(P)   Gi0/18(P)   
4      Po4(SD)          -        Gi0/11(D)   Gi0/12(D)   

网件由于是简单网管交换机,没有状态显示。需要注意的是网件启用lag的admin后需要等几分钟才会生效。

unifi的cloud key 替换https证书及遇到的坑

20191023更新:最新版cloudkey系统keytool软连接到了错误位置

/usr/lib/jvm/java-8-openjdk-armhf/jre/bin/keytool -importkeystore -srckeystore unifi.p12 -srcstoretype PKCS12 -srcstorepass aircontrolenterprise -destkeystore unifi.keystore.jks -storepass aircontrolenterprise

此外新版开机证书检测脚本把cert.tar的目录改到了/root/etc/ssl/private,需要注意。更正打包命令,删除cloudkey.crt cloudkey.key unifi.keystore.jks的路径。

unifi的cloud key的web页面默认使用的是自签名的证书,打开时浏览器会提示不信任的页面,从阿里云或者其他地方免费获取证书后替换就能显示小绿锁了。想着替换一个证书而已,没想到这中间还有坑。
首先简单介绍一下cloud key的web架构
首先是80端口,请求http后会返回301跳转到https,然后打开入口页面,一个是unifi controller,一个是cloud key管理
80端口和443端口是nginx监听的,cloud key 管理页面是443端口,但是unifi controller使用的是8443端口,这个是java的
检查nginx配置发现ssl证书在/etc/ssl/private目录下,有cloudkey.crt和cloudkey.key,nginx证书替换好办,直接把新申请的域名证书内容覆盖cloudkey.crt和cloudkey.key内容即可。
麻烦的是java使用的证书,在网上找到办法,需要根据cloudkey.crt和cloudkey.key生产p12格式证书,在用工具生成Java用的证书unifi.keystore.jks,方法如下

cd /etc/ssl/private
openssl pkcs12 -export -in cloudkey.crt -inkey cloudkey.key -out unifi.p12 -name unifi -password pass:aircontrolenterprise
keytool -importkeystore -srckeystore unifi.p12 -srcstoretype PKCS12 -srcstorepass aircontrolenterprise -destkeystore unifi.keystore.jks -storepass aircontrolenterprise
service nginx restart
service unifi restart

现在使用域名打开unifi controller页面,小绿锁出现了。
然后就遇到坑了,重启发现证书又变成了自签名,这就尴尬了,重启不能保存这则么能行。google后有说cloud key重启会重置文件系统的,但是查看df -h,和经过测试,发现并不会重置,那就是有开机脚本操作了。再搜索,ubnt的英文论坛里说/usr/share/initramfs-tools/scripts/ubnt-bottom/configure-sslcert这个脚本每次开机执行,检查/etc/ssl/private/cert.tar这个压缩包里的证书是否和/etc/ssl/private里的一致。分析脚本发现,只要这个tar包只要是非0字节就会解压到临时目录,对比/etc/ssl/private里证书,如果不一致则情况重新生成,并更新cert.tar。
到此,只要保持cert.tar里面到三个文件和外面到一致就可以了。注意,打包时cloudkey.crt cloudkey.key unifi.keystore.jks三个文件不要带路径。

cd /etc/ssl/private && tar cvf /root/etc/ssl/private/cert.tar cloudkey.crt cloudkey.key unifi.keystore.jks

重启,小绿锁还在。

转一个脚本

#!/usr/bin/env bash

###################################################
# Instructions to replace self-signed certificate #
###################################################
# 1. Save this script file to your cloud key
# 2. Use chmod to make this script executable:
#    chmod u+x cloudkeycert.sh
# 3. Create a certificate signing request (CSR) and private key
#    including the Subject Alternate Name (SAN) field.
#    Chrome will complain if the SAN field is missing.
#    There are many tools out there to create the CSR and
#    key file:
#    * - XCA (https://sourceforge.net/projects/xca/)
#    * - Digicert util (https://www.digicert.com/util/)
#    * - etc.
# 4. Have the CSR signed.
# 5. Ensure the signed certificate and key are in PEM (Base64) format
# 6. In the root home directory (/root), copy the signed cert
#    and key using the following names:
#    * - Certificate -> cloudkey.crt
#    * - Private Key -> cloudkey.key
# 7. Then run this script.  It will backup the existing files
#    in case you need to back out, copy the new files into place,
#    and update the keystore file.  It will stop/start the 
#    web server (NGINX) and the Unifi services.  You may need
#    to close your browser as it may not pick up on the fact that
#    the certificate changed...
#
# OpenSSL and Keytool options copied from script:
#    /usr/share/initramfs-tools/scripts/ubnt-bottom/configure-sslcert
# the only added option to the keytool was -noprompt to force
# overwriting the unifi alias rather than requiring the user
# to answer the prompt

HOSTCERTDIR=/etc/ssl/private
HOSTCERTTAR=cert.tar
HOSTCERTS="cloudkey.crt cloudkey.key unifi.keystore.jks"
BACKUPDIR="${HOSTCERTDIR}/`/bin/date +%Y%m%d`"
BACKUPFILE="`/bin/date +%Y%m%d%H%M`-cert.tar"

echo "Stopping the unifi service"
/usr/sbin/service unifi stop

echo "Stopping the NGINX web server"
/usr/sbin/service nginx stop

# Change to certificate directory
cd ${HOSTCERTDIR}

# Create a backup of the existing bits just in case
if [ ! -d ${BACKUPDIR} ]; then
        echo "Making directory ${BACKUPDIR}"
        mkdir ${BACKUPDIR}
fi

if [ ! -f ${HOSTCERTDIR}/${BACKUPFILE} ]; then
        echo "Creating tar ${BACKUPDIR}/${BACKUPFILE}"
        /bin/tar -cf ${BACKUPDIR}/${BACKUPFILE} ${HOSTCERTS}
fi

# Copy the certificate files from /root
/bin/cp /root/cloudkey.key ${HOSTCERTDIR}/
/bin/cp /root/cloudkey.crt ${HOSTCERTDIR}/

# Build the keystore file from the cloudkey.key and cloudkey.crt
/usr/bin/openssl pkcs12 -export -in ${HOSTCERTDIR}/cloudkey.crt -inkey ${HOSTCERTDIR}/cloudkey.key \
        -out /tmp/keystore.p12 -name unifi -password pass:'' && \
/usr/bin/keytool -importkeystore -deststorepass aircontrolenterprise \
        -destkeypass aircontrolenterprise -destkeystore ${HOSTCERTDIR}/unifi.keystore.jks \
        -srckeystore /tmp/keystore.p12 -srcstoretype PKCS12 -srcstorepass '' -alias unifi -noprompt

# Create the tar file after the keystore file is created
cd ${HOSTCERTDIR}
/bin/tar -cf ${HOSTCERTTAR} ${HOSTCERTS}


echo "Starting the NGINX web server"
/usr/sbin/service nginx start

echo "Starting the unifi service"
/usr/sbin/service unifi start

echo "You may need to restart your browser so it will notice the updated certificate"

家用万兆网络的降级和升级(201807)

去年分享了我家的万兆网络拓扑 https://www.willnet.net/index.php/archives/88/
今年有降级有升级,如图
拓扑图
1.主要的降级是核心交换机从 netgear 的 m4300-24x 降级为 xs716t,降级原因功耗太高,并且噪音稍高,降级后功耗减少一半,噪音基本听不到了
2.其次降级的是路由器,由之前多个独立路由器切换到 esxi 上的 routeros,功耗降低,方便统一管理,esxi 上自建 dns,自建 ikev2,自建 ss,esxi 是研凌的 7200u+32g 内存,安装 esxi6.7
3.升级的是接入交换机,gs108tv2 更换成两个 gs110emx,两个 10g 电口,带 vlan 和链路聚合,功能满足需求
4.其次升级是无线,原来全套 cisco 更换成 unifi 的,两个 uap-ac-shd,一个 key 控制器
5.还有升级是联通家用 500m 更换成联通光快线,1 个固定公网 ip
6.其他变化还有,联通电视盒子撤了,经常影音不同步;加了一个 dnet 的专机;小米盒子国际版换成了 appletv ; nas 由 716+换成 ds3018xs+intel 的 x550 万兆网卡,局域网上传下载在 700m 左右; gen8 吃灰有几个月了;雷电 3 万兆网卡换成了 akitio 的 thunder3 network adapter,passive cooling,静音。

总体是朝着静音&全万兆升级。 ps:都是 rj45 电口,网线是超五类。

pps:xs716t 自带风扇有高频噪音,已更换猫扇A4x20 PWM,温度升高10度,但是噪音几乎没有了,日常功率约45w-50w,比m4300的90w降低近一半。
2018年9月更新,更换猫扇的xs716t截图
更换猫扇的xs716t截图

青云主机安装routeros,附一键安装routeros脚本

20201111更新:routeros chr版本,mount命令offset。

在阿里云安装过routeros,用相同脚本在青云就遇到了坑。
阿里云ecs没有cpu选项,而青云有,创建主机的时候cup高级选项,CPU 体系架构,默认值启动routeros后会cpu100%,选择haswell架构就没有问题,broadwell也不行,再往下的架构没有测试。
附一键安装routeros脚本

wget https://download.mikrotik.com/routeros/6.47.7/chr-6.47.7.img.zip -O chr.img.zip && \
gunzip -c chr.img.zip > chr.img && \
mount -o loop,offset=512 chr.img /mnt && \
ADDRESS0=`ip addr show eth0 | grep global | cut -d' ' -f 6 | head -n 1` && \
GATEWAY0=`ip route list | grep default | cut -d' ' -f 3` && \
echo "/ip address add address=$ADDRESS0 interface=[/interface ethernet find where name=ether1]
/ip route add gateway=$GATEWAY0
" > /mnt/rw/autorun.scr && \
umount /mnt && \
echo u > /proc/sysrq-trigger && \
dd if=chr.img bs=1024 of=/dev/vda && \
reboot