欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  科技

bat 实现主机hostname的修改

程序员文章站 2022-05-26 23:31:27
主机实现hostname的修改原理: 修改注册表中的值: hklm\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName 下的 ComputerName hklm\SYSTEM\CurrentControlSet\Control\Com ......

主机实现hostname的修改原理:

  修改注册表中的值:

    hklm\system\currentcontrolset\control\computername\computername 下的 computername

    hklm\system\currentcontrolset\control\computername\activecomputername 下的 computername

    hklm\system\currentcontrolset\services\tcpip\parameters  下的 hostname

    hklm\system\currentcontrolset\services\tcpip\parameters 下的 nv hostname

 

bat脚本实现:

@echo off
if not "%os%"=="windows_nt" goto end
if "%1"=="" (
    set info=echo &&set skip=0&&set err=0
) else if "%1"=="/?" (
    set info=echo && set skip=1&&set err=0&&goto desc
) else  if "%1"=="-q" (
    set info=echo &&set err=1&&goto err
) else  if "%1"=="-r" (
    set info=echo &&set err=1&&goto err
) else (
    set name=%1 && goto change
)

%info% *******************************
%info% script:changehostname.bat
%info% creation date : 2019/3/14 16:13
%info% last modified : 2019/3/14 20:49 
%info% author: feiquan
%info% email: 2283320260@qq.com
:desc
%info% *********************************
%info% description: 
%info%        chang you hostname
%info% *********************************
%info% examples:
%info%         changehostname.bat  ===^> interact mode
%info%         changehostname.bat  /? ===^> help mode
%info%         changehostname.bat  "newhostname" ===^> commandline mode
%info%         changehostname.bat  "newhostname"  -q ===^> quiet mode
%info%         changehostname.bat  "newhostname"  -r ===^> restart sysetm after 60s
%info%         changehostname.bat  "newhostname"  -q -r ===^>quiet mode, restart sysetm after 60s
%info%         echo n ^| changehostname.bat "newhostname" -q ===^>quiet mode,  no restart sysetm
%info% *********************************
if "%skip%"=="1" goto end


:start
color 07

title change hostname
setlocal enabledelayedexpansion
for /f "skip=1 tokens=3" %%i in ('reg query "hklm\system\currentcontrolset\control\computername\computername" -v computername' ) do set nowhostname=%%i

echo your computer hostname : %nowhostname%

set /p n=please input new hostname :
set name="!n!"
echo.

:change
if "%2"=="-q" goto quiet
if "%3"=="-q" goto quiet

set /p sure=do you want to change hostname %name% ?(y/n)
if  /i not "%sure%"=="y" goto end

:quiet
reg add "hklm\system\currentcontrolset\control\computername\computername" -v computername -t reg_sz -d %name% -f

reg add "hklm\system\currentcontrolset\control\computername\activecomputername" -v computername -t reg_sz -d %name% -f

reg add "hklm\system\currentcontrolset\services\tcpip\parameters" -v hostname -t reg_sz -d %name% -f

reg add "hklm\system\currentcontrolset\services\tcpip\parameters" -v "nv hostname" -t reg_sz -d %name% -f

echo.
for /f "skip=1 tokens=3" %%i in ('reg query "hklm\system\currentcontrolset\control\computername\computername" -v computername' ) do set nowhostname=%%i

echo your computer hostname has changed : %nowhostname%
echo  after 60 seconds , restart your system....

if "%2"=="-r" goto resert
if "%3"=="-r" goto resert
set /p restart=do you want to restart your system after 60s ? (y/n)
if  /i not "%restart%"=="y" goto end
:resert
shutdown -r -t 60 -c "your computer hostname has changed : %nowhostname% . restart your system 60s ...."  -d p:2:4
goto end

:err
echo format error , please with "changehostname.bat  /? " to learn more ....

:end
echo exiting....

 

下载地址:

  链接:https://pan.baidu.com/s/1ik3c0juf4um1hu58p9sj9a
  提取码:p28f