基于PXE启动的WINDOWS 2000/XP的多播克隆(3)

类别:软件工程 点击:0 评论:0 推荐:

基于PXE启动的WINDOWS 2000/XP的多播克隆(3)

 

修改计算机名程序

 

    修改计算机名程序是用Visual Basic 6.0编写的一个小程序,其目的是为了使目标机的计算机名按机房管理员的要求进行设置,而非由系统自动随机产生。其原理是根据每台机器的网络MAC地址固定且不会相同,按机器的MAC地址来设置计算机名称。因此必须先生成一张MAC地址——计算机名映射表,程序根据表中的数据查找和机器MAC地址相同的记录,再用记录中的计算机名修改机器名称。

    MAC地址——计算机名映射表是一个纯文本文件,其名为netname.dat,数据格式如下:

       计算机名,MAC地址

如:    winxp1, 000AEB1D6460

winxp2, 00E04CE43C61

aaaaaa, 00E04CE455E2

bbbbbb, 00E04C44365E

cccccc, 00E04C500555

………, ………………

    该文本文件必须和生成的应用程序执行文件setcpn.exe在同一个目录中,执行文件setcpn.exe的运行命令是写在Sysprep.inf文件的[GuiRunOnce]段内的,请参见前述的制作应答文件内容。该程序执行后会自动删除netname.dat文件,以避免重复执行,并重新启动计算机,使修改后的计算机名生效。

    该程序也可用于WINDOWS 9X操作系统在多播克隆时修改目标计算机名。但须在参考机的注册表的HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\RunServicesOnce下新建一个字符串键——SETCPN,键值为“C:\WINDOWS\SETCPN\setcpn.exe”,它的作用是当网络复制完成并重新启动后,系统自动运行setcpn.exe程序。上面假设setcpn.exe程序和netname.dat文件是存放在C:\WINDOWS\SETCPN目录下,如果不是在此目录下,上面的键值应作相应的修改。

    该程序调用了NETBIOS的API函数,因此在参考机上一定要安装NETBEUI协议。由于是用VB编写的程序,因此参考机上要有VB的运行时刻库。如果将程序生成安装包,系统会自动加入运行库。

程序中有一段代码是对五笔字型输入法的处理。使用Sysprep处理过系统后,原来安装的输入法(非Windows系统自带的)会从输入法托盘中消失,但输入法程序本身并没有被删除,只需在注册表中修改相关键值即可使输入法重新出现在输入法托盘中。但各种输入法键值不同,需区别对待。本程序中的键值只是两种五笔字形输入法的。

WINDOWS XP可使用“登录使用欢迎屏幕”设置,并将默认登录用户名设为:administrator,登录密码为空,这样每次计算机启动时就不需要输入密码了。由于administrator权限过高,为避免使用者(如学生)乱改系统设置,可在系统中安装一套虚拟还原软件,每次开机自动还原即可。

    WINDOWS XP下生成的安装包在WINDOWS 2000下有时不能安装,解决的办法是在WINDOWS 2000下重新生成一个安装包,用于WINDOWS 2000的安装。

 

附:程序清单

setcpn.vbp-setcomputername.bas

 

Option Explicit

 

 '安全关机需调用的API函数

Public Const EWX_LOGOFF = 0     '以其它用户名重新登录系统

Public Const EWX_SHUTDOWN = 1   '终止所有进程并关闭计算机

Public Const EWX_REBOOT = 2     '关掉在进程安全描述表中运行的所有进程,重起计算机

Public Const EWX_FORCE = 4      '强迫进程终止

Public Const EWX_POWEROFF = 8   '在NT下终止所有进程并关闭计算机

Declare Function ExitWindowsEx Lib "user32" (ByVal fuOptions As Long, ByVal dwReserved As Long) As Integer   '关闭系统函数

 

 'NT下关机需调用的安全机制API函数

Public Const TOKEN_ADJUST_PRIVILEGES = &H20

Public Const TOKEN_QUERY = &H8

Public Const SE_PRIVILEGE_ENABLED = &H2

Public Const ANYSIZE_ARRAY = 1

 

Type LUID

  lowpart As Long

  highpart As Long

End Type

 

Type LUID_AND_ATTRIBUTES

  pLuid As LUID

  Attributes As Long

End Type

 

Type TOKEN_PRIVILEGES

  PrivilegeCount As Long

  Privileges(ANYSIZE_ARRAY) As LUID_AND_ATTRIBUTES

End Type

 

Declare Function GetCurrentProcess Lib "kernel32" () As Long

Declare Function LookupPrivilegeValue Lib "advapi32.dll" Alias "LookupPrivilegeValueA" (ByVal lpSystemName As String, ByVal lpName As String, lpLuid As LUID) As Long

Declare Function AdjustTokenPrivileges Lib "advapi32.dll" (ByVal TokenHandle As Long, ByVal DisableAllPrivileges As Long, NewState As TOKEN_PRIVILEGES, ByVal BufferLength As Long, PreviousState As TOKEN_PRIVILEGES, ReturnLength As Long) As Long

Declare Function OpenProcessToken Lib "advapi32.dll" (ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, TokenHandle As Long) As Long

 

'注册表相关操作API函数定义

Public Const HKEY_CLASSES_ROOT = &H80000000

Public Const HKEY_CURRENT_USER = &H80000001

Public Const HKEY_LOCAL_MACHINE = &H80000002

Public Const HKEY_USERS = &H80000003

Public Const HKEY_PERFORMANCE_DATA = &H80000004

Public Const HKEY_CURRENT_CONFIG = &H80000005

Public Const HKEY_DYN_DATA = &H80000006

 

Public Const REG_MULTI_SZ = 7

Public Const ERROR_SUCCESS = 0&

Public Const READ_CONTROL = &H20000

Public Const REG_SZ = 1

Public Const REG_DWORD = 4

Public Const REG_EXPAND_SZ = 2

Public Const REG_BINARY = 3

Public Const REG_DWORD_BIG_ENDIAN = 5

Public Const REG_DWORD_LITTLE_ENDIAN = 4

Public Const REG_NONE = 0

 

Public Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" _

(ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long

 

Public Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" _

(ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, _

lpType As Long, lpData As Any, lpcbData As Long) As Long

 

Public Declare Function RegEnumKey Lib "advapi32.dll" Alias "RegEnumKeyA" _

    (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpName As String, _

     ByVal cbName As Long) As Long

 

Public Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" _

(ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long

 

Public Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" _

(ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, _

ByVal dwType As Long, lpData As Any, ByVal cbData As Long) As Long

 

Public Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long

 

'获取操作系统版本

Public Declare Function GetVersion Lib "kernel32" () As Long

 

'获取网卡MAC地址、更改计算机名相关操作API函数定义

Public Const NCBASTAT As Long = &H33

Public Const NCBNAMSZ As Long = 16

Public Const HEAP_ZERO_MEMORY As Long = &H8

Public Const HEAP_GENERATE_EXCEPTIONS As Long = &H4

Public Const NCBRESET As Long = &H32

 

Public Type NET_CONTROL_BLOCK    'NCB

   ncb_command    As Byte

   ncb_retcode    As Byte

   ncb_lsn        As Byte

   ncb_num        As Byte

   ncb_buffer     As Long

   ncb_length     As Integer

   ncb_callname   As String * NCBNAMSZ

   ncb_name       As String * NCBNAMSZ

   ncb_rto        As Byte

   ncb_sto        As Byte

   ncb_post       As Long

   ncb_lana_num   As Byte

   ncb_cmd_cplt   As Byte

   ncb_reserve(9) As Byte     '保留,必须为0

   ncb_event      As Long

End Type

 

Public Type ADAPTER_STATUS

   adapter_address(5) As Byte

   rev_major         As Byte

   reserved0         As Byte

   adapter_type      As Byte

   rev_minor         As Byte

   duration          As Integer

   frmr_recv         As Integer

   frmr_xmit         As Integer

   iframe_recv_err   As Integer

   xmit_aborts       As Integer

   xmit_success      As Long

   recv_success      As Long

   iframe_xmit_err   As Integer

   recv_buff_unavail As Integer

   t1_timeouts       As Integer

   ti_timeouts       As Integer

   Reserved1         As Long

   free_ncbs         As Integer

   max_cfg_ncbs      As Integer

   max_ncbs          As Integer

   xmit_buf_unavail  As Integer

   max_dgram_size    As Integer

   pending_sess      As Integer

   max_cfg_sess      As Integer

   max_sess          As Integer

   max_sess_pkt_size As Integer

   name_count        As Integer

End Type

  

Public Type NAME_BUFFER

   name        As String * NCBNAMSZ

   name_num    As Integer

   name_flags  As Integer

End Type

 

Public Type ASTAT

   adapt          As ADAPTER_STATUS

   NameBuff(30)   As NAME_BUFFER

End Type

 

Public Declare Function Netbios Lib "netapi32.dll" _

   (pncb As NET_CONTROL_BLOCK) As Byte

 

Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _

   (hpvDest As Any, ByVal hpvSource As Long, ByVal cbCopy As Long)

 

Public Declare Function GetProcessHeap Lib "kernel32" () As Long

 

Public Declare Function HeapAlloc Lib "kernel32" _

    (ByVal hHeap As Long, ByVal dwFlags As Long, ByVal dwBytes As Long) As Long

    

Public Declare Function HeapFree Lib "kernel32" _

    (ByVal hHeap As Long, ByVal dwFlags As Long, lpMem As Any) As Long

 

Public Declare Function SetComputerName Lib "kernel32" Alias "SetComputerNameA" _

    (ByVal lpComputerName As String) As Long

 

'NT下调用关机函数前需先调用的安全机制函数子过程

Public Sub AdjustToken()

  Dim hdlProcessHandle As Long

  Dim hdlTokenHandle As Long

  Dim tmpLuid As LUID

  Dim tkp As TOKEN_PRIVILEGES

  Dim tkpNewButIgnored As TOKEN_PRIVILEGES

  Dim lBufferNeeded As Long

  hdlProcessHandle = GetCurrentProcess()

  OpenProcessToken hdlProcessHandle, (TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY), _

hdlTokenHandle

  LookupPrivilegeValue "", "SeShutdownPrivilege", tmpLuid

  tkp.PrivilegeCount = 1

  tkp.Privileges(0).pLuid = tmpLuid

  tkp.Privileges(0).Attributes = SE_PRIVILEGE_ENABLED

  AdjustTokenPrivileges hdlTokenHandle, False, tkp, Len(tkpNewButIgnored), _

tkpNewButIgnored, lBufferNeeded

End Sub

 

Public Function GetMACAddress() As String

  '返回一个格式化的网络控制器的MAC地址

   Dim tmp As String

   Dim pASTAT As Long

   Dim NCB As NET_CONTROL_BLOCK

   Dim AST As ASTAT

  'IBM NetBIOS 3.0在NCBRESET命令中定义了4个基本NetBIOS环境变量,

  'Win32则用OS/2动态连接路由器(DLR)环境。这意味着应用程序使用

  '的第一个NCB必须是一个NCBRESET命令,这里有一个例外是NCBENUM命令。

  'Windows NT与IBM NetBIOS 3.0对NCB_CALLNAME字段的解释是不同的

   NCB.ncb_command = NCBRESET

   Call Netbios(NCB)

  '为获取以太网卡的媒体访问控制(MAC)地址,使用Netbios()、NCBASTAT命令,

  '并将NCB.ncb_CallName设置为"*         "(16个字符)。

   NCB.ncb_callname = "*              "

   NCB.ncb_command = NCBASTAT

  '对于有多个网卡的机器,需要执行枚举LANA数目,并对每个网卡执行NCBASTAT命令

  '即是机器中只有一个网卡,首先枚举有效的LANA数目,并在其中一个有效的LANA上执行

  'NCBASTAT命令也是非常不错的。在程序中用硬编码的方式设置LANA数目为0是不可取的。

   NCB.ncb_lana_num = 0

   NCB.ncb_length = Len(AST)

   pASTAT = HeapAlloc(GetProcessHeap(), HEAP_GENERATE_EXCEPTIONS _

            Or HEAP_ZERO_MEMORY, NCB.ncb_length)

   If pASTAT = 0 Then

      Debug.Print "内存分配失败!"

      Exit Function

   End If

   NCB.ncb_buffer = pASTAT

   Call Netbios(NCB)

   CopyMemory AST, NCB.ncb_buffer, Len(AST)

   tmp = ""

   For i = 0 To 5

     str1 = Hex$(AST.adapt.adapter_address(i))

     If Len(str1) < 2 Then

       tmp = tmp & "0" & str1

     Else

       tmp = tmp & str1

     End If

   Next

   HeapFree GetProcessHeap(), 0, pASTAT

   GetMACAddress = tmp

End Function

本文地址:http://com.8s8s.com/it/it36950.htm