我已经在批处理中编写了一些命令,以便在选择的基础上执行它们。我使用两个IP地址,每次在IP之间切换时,我都必须更改IPv4和DNS。
我已经完成了这段代码,如果我逐行执行,它可以正常工作,但在批处理中,它们会给出错误。
@ECHO OFF
SET /P no= Welcome dude so what are you up to press 1 for buzznet,2 for BSNL :
IF "%NO%"=="1" GOTO BUZZ
IF "%NO%"=="2" GOTO BSNL
:BUZZ
netsh interface ipv4 set address name="Ethernet" source=static ^
addr=192.168.22.19 mask=255.255.255.0 gateway=192.168.22.1
netsh interface ip add dns name="Ethernet" addr=192.168.18.1
netsh interface ip add dns name="Ethernet" addr=8.8.8.8 index=2
:BSNL
netsh interface ip set address "Ethernet" dhcp
netsh interface ip set dns “Ethernet” dhcp
pause

发布于 2015-10-10 01:21:13
正如注释中所述,您需要添加一些东西,以便在作业完成时停止脚本继续运行。(goto:EOF或exit /b 0)
@ECHO OFF
:retry
SET /P no= Welcome dude so what are you up to press 1 for buzznet,2 for BSNL :
IF "%no%"=="1" GOTO BUZZ
IF "%no%"=="2" GOTO BSNL
rem if %no% is not 1 nor 2 then exit or goto :retry.
exit /b 0
:BUZZ
netsh interface ipv4 set address name="Ethernet" source=static ^
addr=192.168.22.19 mask=255.255.255.0 gateway=192.168.22.1
netsh interface ip add dns name="Ethernet" addr=192.168.18.1
netsh interface ip add dns name="Ethernet" addr=8.8.8.8 index=2
rem job done, then exit with a pause before
pause
exit /b 0
:BSNL
netsh interface ip set address "Ethernet" dhcp
netsh interface ip set dns "Ethernet" dhcp
pause此外,最后一条命令的引号格式错误,“Ethernet”应为"Ethernet"
发布于 2020-09-29 04:05:11
在编写脚本在静态IP和动态IP之间切换之前,我检查了这一点,因此我考虑添加我的解决方案,以防有用。
@ECHO OFF cls ECHO仅当以管理员身份运行时,________________________________ ECHO才有效。回声。:菜单回显。回声。ECHO _______________________________ ECHO菜单ECHO ................................ECHO选择一个选项: ECHO。ECHO 1 IP静态192.X.X.X、255.X、X.X ECHO 2 IP动态ECHO 3检查当前IP ECHO 4检查网络接口状态ECHO 5退出ECHO ..................................回声。回声。设置/P M=Write 1、2、3、4或5并按ENTER: IF %M%==1 GOTO STATIC IF %M%==2 GOTO DYNAMIC IF %M%==3 GOTO CHECK IF %M%==4 GOTO CHECKIF IF %M%==5 GOTO EOF :STATIC ECHO ______________________________ ECHO。ECHO将ip更改为192.X.X.X并子掩码更改为255.X.X.X netsh接口ip set address name=“以太网”static 192.X.X.X 255.X.X.X TIMEOUT /T 2 /NOBREAK > NUL ECHO ____________________________ netsh interface IP show config name="Ethernet“TIMEOUT /T 2 /NOBREAK > NUL GOTO MENU :动态ECHO _______________________________回声。回声正在更改为动态IP...netsh interface ip set address "Ethernet“dhcp TIMEOUT /T 4 /NOBREAK > NUL ECHO _____________________________ netsh interface ip show config name="Ethernet”TIMEOUT /T 2 /NOBREAK > NUL GOTO MENU :检查ECHO __________________________________ ECHO显示适配器详细信息...netsh interface ip show config name="Ethernet“TIMEOUT /T 2 /NOBREAK > NUL GOTO MENU :CHECKIF ECHO ____________________________________ ECHO显示网络接口状态...netsh接口超时显示接口超时/T 2 /NOBREAK > NUL GOTO MENU
应该有一个错误处理,但我没有时间,所以这就是它。
另请注意: tbh,最后我删除了所有选项,并创建了一个更改为静态IP和另一个更改为动态IP的选项,因此现在我只需更快地运行它们。
https://stackoverflow.com/questions/33037811
复制相似问题