标题: [网络连接] BAT批处理怎样自动设置局域网IP? [打印本页]
作者: marya 时间: 2014-1-5 00:03 标题: BAT批处理怎样自动设置局域网IP?
局域网内不同的电脑设置指定IP和计算机名(操作者选择),批处理调用TXT 文本内容:- 张三 192.168.0.10
- 李四 192.168.0.11
- 王五 192.168.0.12
- (以下略...)
复制代码
批处理能适应XP或Win7等不同系统
作者: 522235677 时间: 2014-1-5 19:38
本帖最后由 522235677 于 2014-1-5 19:39 编辑
- @echo off & setlocal ENABLEDELAYEDEXPANSION
- set n=0
- echo 根据你的姓名输入编号并按下回车键
- for /f "tokens=1,2" %%a in (1.txt) do (
- set /a n+=1
- echo !n!.%%a
- )
- set n=0
- set /p choice=输入编号:
- for /f "tokens=1,2" %%a in (1.txt) do (
- set /a n+=1
- if "!n!"=="%choice%" (
- wmic computersystem where "caption='%ComputerName%'" call rename %%a
- netsh interface ipv4 set address "本地连接" static %%b 255.255.255.0 192.168.0.1
- )
- )
- pause
复制代码
作者: marya 时间: 2014-1-5 23:10
在win8运行效果(在虚拟机XP中也一样):
作者: 522235677 时间: 2014-1-6 09:37
@echo off & setlocal ENABLEDELAYEDEXPANSION
set n=0
echo 根据你的姓名输入编号并按下回车键
for /f "tokens=1,2" %%a in (1.txt) do (
set /a n+=1
echo !n!.%%a
)
set n=0
set /p choice=输入编号:
for /f "tokens=1,2" %%a in (1.txt) do (
set /a n+=1
if "!n!"=="%choice%" (
wmic computersystem where "caption='%ComputerName%'" call rename %%a
netsh interface ipv4 set address "本地连接" static %%b 255.255.255.0 192.168.0.1
)
)
pause
作者: marya 时间: 2014-1-6 22:59
回复 4# 522235677
虚拟机测试还是有问题:重命名成功,IP没有设置好
作者: marya 时间: 2014-1-7 01:48
参考别人的改动一下:- @echo off & setlocal ENABLEDELAYEDEXPANSION
- echo 根据你的姓名输入编号并按下回车键
- @set INTERFACE=本地连接
- set MASK=255.255.255.0
- set GATEWAY=192.168.0.1
- set DNS1=202.96.128.86
- set DNS2=202.96.134.33
- set n=0
- for /f "tokens=1,2" %%a in (name.txt) do (
- set /a n+=1
- echo !n!.%%a
- )
- set n=0
- set /p choice=输入编号:
- for /f "tokens=1,2" %%a in (name.txt) do (
- set /a n+=1
- if "!n!"=="%choice%" (
- wmic computersystem where "caption='%ComputerName%'" call rename %%a
- netsh interface ip set address "%INTERFACE%" static %%b %MASK% %GATEWAY% 0
- netsh interface ip set dns "%INTERFACE%" static %DNS1% PRIMARY
- netsh interface ip add dns "%INTERFACE%" %DNS2% index=2
- )
- )
- pause
复制代码
在虚拟机XP下成功,但反应较慢,也不满意。
但在Win8不成功
作者: 522235677 时间: 2014-1-8 20:14
给个代码就是给你参考,谁有时间帮你把代码写的好好的。要靠自己
作者: marya 时间: 2014-1-8 23:32
本帖最后由 marya 于 2014-1-8 23:35 编辑
回复 7# 522235677
谢谢帮助
参考你的与别人的成果,现在的代码基本满意!
找到一个不错的参考代码:- @echo off
- call:which dllcall.exe || (
- echo 沒有外部命令"dllcall.exe", 請在"http://vinoca.org/files/dllcall"下載此程序並放在"C:\Windows"下。
- pause &&exit)
- title IP切換&&color F0
- mode con: cols=48 lines=20
- setlocal enabledelayedexpansion
-
- dllcall IsUserAnAdmin,Shell32.dll-
- if '%errorlevel%' EQU '0' (
- echo 需管理員權限才能切換IP。
- dllcall ShellExecuteA,,"RunAs","%~0",,,10,shell32.dll
- exit)
-
- for /f "delims=:" %%i in ('findstr /NB "^:config context" "%~0"') do set config_label_num=%%i
- :loop
- cls&&echo.
- set opt_char=&&set option=
- call:colstr 0 0 "o" 0 2 0
- call:colstr 00d1 0 "按序號選擇" 0 0 2
- for /F "delims=" %%i in ('more +%config_label_num% "%~0" ^| findstr /B ":ip[0-9][0-9]*.*@.*"') do (
- for /F "tokens=1 delims=@" %%j in ("%%i") do set id=%%j &&set id=!id:~3,2!
- for /F "tokens=2 delims=@" %%j in ("%%i") do set name=%%j
- set opt_char=!opt_char! !id!
- call:colstr 0 0 "o" 0 2 0
- call:colstr 00FC 0 !id! 0 0 0
- echo !name!
- )
- echo.
- set /p option=輸入序號:
- for %%i in (%opt_char% h l q) do if "%%i" == "%option%" goto ip%option%
- cls
- color Fc
- echo.
- echo.
- echo.
- echo 请按提示输入。
- dllcall Sleep,700,kernel32.dll-
- color F0
- goto loop
-
- :doit
- if "%mode%" == "dhcp" (
- netsh interface ip set address %CARD% dhcp
- netsh interface ip set dnsservers %CARD% dhcp
- )
- if "%mode%" == "static" (
- netsh interface ip set address %CARD% static %LIP% %MSK% %GW%
- if not "%DNS%" == "" netsh interface ip set dnsservers %CARD% static %DNS% primary no
- )
-
- if "%mode%" == "IEProxy" (
- if not "%ProxyServer%" == "" reg add %REG_IE_PROXY_SET% /v ProxyServer /d "%ProxyServer%" /f
- if not "%ProxyOverride%" == "" reg add %REG_IE_PROXY_SET% /v ProxyOverride /t REG_SZ /d "%ProxyOverride%" /f
- set REG_IE_PROXY_SET="HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings"
- for /f "tokens=3 delims= " %%i in ('reg query !REG_IE_PROXY_SET! /v ProxyEnable') do (
- if "%%i" equ "0x0" (
- reg add !REG_IE_PROXY_SET! /v ProxyEnable /t REG_DWORD /d 1 /f
- call:msgBox 已启用代理
- )
- if "%%i" equ "0x1" (
- reg add !REG_IE_PROXY_SET! /v ProxyEnable /t REG_DWORD /d 0 /f
- call:msgBox 已停止代理
- )
- )
- dllcall InternetSetOptionA,,0x000025,,,wininet.dll;InternetSetOptionA,,0x000027,,,wininet.dll-
- exit
- )
- )
- call:msgBox "IP 設置完成"
- exit
-
- :iph
- cls
- echo.
- echo 可用參數:h,l
- echo.
- pause
- goto loop
-
- :ipl
- cls
- echo.
- echo 本機網卡列表:
- pause
- goto loop
- :ipq
- exit
-
-
- :: {{{
- rem /*--------- colstr 函数 -------------
- :Colstr <attr> <sp> <"str"> <bk> <sp> <enter>
- for %%a in (+%2 +%4 +%5 +%6) do (
- if "%%a"=="+" echo 控制参数不能为空&exit/b
- if %%a lss +0 echo 参数越界-&exit/b
- if %%a geq +a echo 参数越界+&exit/b)
- if %3 == "" echo 字符串不能为空&exit/b
- pushd %tmp%&setlocal ENABLEEXTENSIONS
- if exist "%~3?" del/a/q "%~3?">nul 2>nul
- if %2 gtr 0 call:%0_bs %2 sp " "&call set/p=%%sp%%<nul
- if %4 gtr 0 (call:%0_bs %4 bk "") else set "bk="
- call:%0_bs %5 sp " "
- set/p=%bk%%sp%<nul>"%~3"&findstr /a:%1 .* "%~3?" 2>nul
- if not %6 equ 0 for /l %%a in (1 1 %6)do echo.
- endlocal&popd&goto:eof
- :Colstr_bs
- set "p="&for /l %%a in (1 1 %1)do call set "p=%%p%%%~3"
- set "%2=%p%"&goto:eof
- rem ------------------------------------*/
-
- Rem 檢測命令是否存在
- :WHICH
- SETLOCAL&PATH %PATH%;%~dp0;%cd%
- if "%~$PATH:1"=="" exit /b 1
- exit /b 0
-
- Rem 顯示一個消息對話框
- :msgBox
- dllcall MessageBoxA,,%1,"提示",0x40,user32.dll-
- exit /b
-
- :: }}}
-
-
-
- :: 使用說明:
- :: 在 :config context 之後的行中填寫需要切換的IP地址項,
- :: :ip1 @ name 爲必填項
- :: goto doit 爲必填項
- :: 將 :ip1 @ name 中的 @ 替換爲其它符號即可隱藏此地址項。
- :: :ip1 @ name 中ip後面數字最多爲兩位
- :: 已預設4種切換模式(mode),
- :: 1. 動態(dhcp) 2. 静态(static) 3. 啓用/禁用IE代理(IEProxy)
-
- :config context
-
- :ip1 @ Wi-Fi set DHCP
- set mode=dhcp
- set card="Wi-Fi"
- goto doit
-
- :ip3 @ 公司網
- set mode=static
- set CARD="Wi-Fi"
- set LIP=10.10.10.100
- set MSK=255.255.240.0
- set GW=10.10.10.1
- set DNS=10.10.10.10
- :: 附加命令可直接寫這裏
- netsh wlan connect name=AP1 interface=%CARD%
- goto doit
-
- :ip6 ! 機房維護
- set mode=static
- set CARD="以太网"
- set LIP=10.228.3.3
- set MSK=255.255.255.192
- goto doit
-
- :ip7 ! 建立熱點
- ::不使用預設模式,自定義多個命令
- ::netsh wlan set hostednetwork mode=allow ssid=8 key=12345678
- netsh wlan start hostednetwork
- ::netsh wlan stop hostednetwork
- goto doit
-
- :ip8 @ 啓用/禁用IE代理
- set mode=IEProxy
- ::set ProxyServer=23.333.22.33:7080
- ::set ProxyOverride="192.168.*;*.xxx.com;<local>"
- goto doit
复制代码
欢迎光临 批处理之家 (http://www.bathome.net/) |
Powered by Discuz! 7.2 |