Enable the computer after the GHOST to automatically correct the computer name and IP address
I. Question proposal
One of the challenges after being the director of the lab center is data center management. The IDC is not big. There are only more than 60 computers, and the haoguang hard disk protection card deluxe edition has been installed, you can directly network the GHOST hard disk. Once a computer is installed, the 40 Gb hard disk information can be spread across every computer in about six hours.
Although it is a small experimental center, it also serves everyone on the machine, what level of examination, financial accounting, Foreign Trade simulation, software development everything is complete. In this way, six operating systems (DOS, Win 98, Win 2003 Server, Win 2000 Server, WinXP, and Win 2000 Server) are installed on a 40 Gb hard drive )). After struggling with patches and viruses, the remaining problem is to automatically update the IP address and computer name. Each machine in the IDC has a serial number, and the IP Address Allocation of the machine is also related to the serial number. Although the haiguang Blue Card provides the function of automatically modifying IP addresses, it does not work for the Server version of the operating system (after modification, the system is paralyzed), and does work for non-Server versions of the operating system, however, IP Address Allocation is random. If you want to customize the IP address, you must create a MAC ing table between the MAC address and IP address. In short, it is very inconvenient to use, not to mention the inability to modify the IP address of the NT Server. Call haiguang to contact us. The reply is: Your protection card does not have the function of modifying the IP address of the NT Server. Our latest product here is the MAX version, which can solve your problem. Isn't it about getting money from your pocket! Do it yourself.
Ii. Solution
In the beginning, I used DHCP to dynamically allocate IP addresses and reserve a fixed IP address for each machine. However, since all machines are cloned, The Machine names are identical, each time the host is started, it is reported that a computer with a duplicate name exists, which also affects the use of network neighbors. In the end, you can compile a program that automatically modifies the machine name and IP address.
You can use Windows Script or WinBatch to implement it. However, you need to install the software on the machine, which seems to be a little useful. Finally, you decided to use Delphi to write a program that automatically modifies the IP address. In this way, with the help of the automatic IP address modification function on the sea light blue card (that is, the automatic IP address modification. After my research, it is actually to unlock the hard disk protection and automatically restart each system. During the startup process, haiguang's own driver completes the modification .), Modify the IP address by yourself. The specific method is to stop installing the program that haiguang automatically modifies the IP address and change it to its own program, so that the system can automatically correct the IP address and computer name at the first startup and restart the machine.
First, store all the machine numbers and MAC ing tables as an Access database, and set the IP address to automatically obtain DHCP to prevent conflicts during server startup. Then write the following program in Delphi:
Unit UpdateIP;
Interface
Uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, NB30, StdCtrls, DB, ADODB;
Type
TfrmUpdateIPAddress = class (TForm)
AdoCntAccess: TADOConnection;
AdoDSMacAddress: TADODataSet;
Procedure adoCntAccessBeforeConnect (Sender: TObject );
Procedure FormShow (Sender: TObject );
Private
{Private declarations}
Public
{Public declarations}
End;
Var
FrmUpdateIPAddress: TfrmUpdateIPAddress;
Implementation
{$ R *. dfm}
// ================================================ ======================================
// Set the database path
// ================================================ ======================================
Procedure TfrmUpdateIPAddress. adoCntAccessBeforeConnect (Sender: TObject );
Begin
AdoCntAccess. ConnectionString: = 'provider = Microsoft. Jet. OLEDB.4.0; Password = ""; '+
'User ID = Admin; Data Source = '+ ExtractFilePath (Application. ExeName) +
'/MacData. mdb; Mode = Share Deny None; Extended Properties = ""';
End;
// ================================================ ======================================
// Obtain the MAC address of the computer
// ================================================ ======================================
Function NBGetAdapterAddress (a: Integer): string;
Var
NC: TNCB;
ADAPTE: TADAPTERSTATUS;
LANAENU: TLANAENUM;
IntId: Integer;
CR: Char;
StrTem: string;
Begin
Result: = '';
Try
ZeroMemory (@ NC, SizeOf (NC ));
NC. ncb_command: = Chr (NCBENUM );
CR: = NetBios (@ NC );
// Reissue enum command
NC. ncb_buffer: = @ LANAENU;
NC. ncb_length: = SizeOf (LANAENU );
CR: = NetBios (@ NC );
If Ord (cR) <> 0 then
Exit;
ZeroMemory (@ NC, SizeOf (NC ));
NC. ncb_command: = Chr (NCBRESET );
NC. ncb_lana_num: = LANAENU. lana [a];
CR: = NetBios (@ NC );
If Ord (cR) <> 0 then
Exit;
ZeroMemory (@ NC, SizeOf (NC ));
NC. ncb_command: = Chr (NCBASTAT );
NC. ncb_lana_num: = LANAENU. lana [a];
StrPCopy (NC. ncb_callname ,'*');
NC. ncb_buffer: = @ ADAPTE;
NC. ncb_length: = SizeOf (ADAPTE );
CR: = NetBios (@ NC );
StrTem: = '';
For intId: = 0 To 5 do
StrTem: = strTem + InttoHex (Integer (ADAPTE. adapter_address [intId]), 2 );
Result: = strTem;
Finally
End;
End;
// ================================================ ======================================
// Set the computer name
// ================================================ ======================================
Function SetComputerName (AComputerName: string): Boolean;
Var
ComputerName: array [0 .. MAX_COMPUTERNAME_LENGTH + 1] of Char;
Begin
StrPCopy (ComputerName, AComputerName );
Result: = windows. setcomputername (computername );
End;
// ================================================ ======================================
// Automatically modify the IP address and computer name at startup
// ================================================ ======================================
Procedure tfrmupdateipaddress. formshow (Sender: tobject );
VaR
SMAC, snum, scomputername, batchfilename: string;
Processinfo: tprocessinformation;
Startupinfo: tstartupinfo;
Begin
SMAC: = nbgetadapteraddress (0 );
Adocntaccess. Connected: = true;
Adodsmacaddress. close;
Adodsmacaddress. Parameters. parambyname ('mac'). Value: = SMAC;
Adodsmacaddress. open;
If adodsmacaddress. recordcount = 0 then
Application. Terminate;
Snum: = trim (adodsmacaddress. fieldbyname ('computerid'). value );
// Set the computer name
SComputerName: = 'stu _ '+ sNum;
If not SetComputerName (sComputerName) then
Begin
ShowMessage ('the computer name is not set successfully! ');
Application. Terminate;
End;
// Set IP addresses, DNS, etc.
BatchFileName: = ExtractFilePath (ParamStr (0) + 'autoupdate. bat' + sNum;
StartUpInfo. dwFlags: = STARTF_USESHOWWINDOW;
StartUpInfo. wShowWindow: = SW_Hide;
If CreateProcess (nil, PChar (BatchFileName), nil, nil,
False, IDLE_PRIORITY_CLASS, nil, nil, StartUpInfo, ProcessInfo) then
Begin
CloseHandle (ProcessInfo. hThread );
CloseHandle (ProcessInfo. hProcess );
End;
Application. Terminate;
End;
End.
The program first obtains the MAC address of the current machine, then retrieves the machine number from the Access Table, and generates the machine name "stu _ + machine number" based on the machine number ". After the machine name is modified, an external batch processing (a BAT file here) is automatically started to call the batch processing of parameters to modify the IP address. If someone asks why batch processing is called, I want to make the system a self-deleted system. After the program is executed, it will be done quietly without leaving any traces, this will be discussed later.
The content of the batch file is as follows:
@ Echo off
Rem modifies IP addresses, subnet masks, and gateways.
CMD/C netsh interface IP Set address name = "Local Connection" Source = static ADDR = 10.16.19.% 1 mask = 255.255.255.0 gateway = 10.16.19.254 gwmetric = 1
Rem modify DNS
CMD/C netsh interface IP Set DNS name = "Local Connection" Source = static ADDR = 210.31.198.65
Rem Delete shortcuts in the Startup Group
Del/Q "C:/Documents and Settings/all users/" start "menu/Program/start/AutoUpdate .*"
Rem calls the batch processing file to restart the server
Restart. bat
From the file, we can see that in order to allow the system to automatically modify the IP address at the first run, a shortcut is created in the Start menu to automatically call my program. After the program is executed, delete the shortcut in the Startup menu and restart. As for the Self-deletion function, I have not put it here. If you are interested, you can search for "Delphi self-deletion" in Google to find a lot of information.
Next, let's talk about restarting Windows 98 and Windows ME. It's easy to restart. You only need one command:
Rundll32.exe shell32.dll, SHExitWindowsEx n
The last n indicates:
0-LOGOFF
1-SHUTDOWN
2-REBOOT
4-FORCE
8-POWEROFF
Windows XP restarts differently from 98 and requires the shutdown command. For details, refer to the manual.
The most difficult thing is the restart of NT 2000. The above method does not work. It must be implemented using multiple commands. Next let's take a look at the batch processing files restarted by NT 2000:
@ Echo off & cd/d % temp % & echo [version]> {out}. inf
(Set inf = InstallHinfSection DefaultInstall)
Echo signature = $ chicago $ >>{ out}. inf
Echo [defainstall install] >>{ out}. inf
Rundll32 setupapi, % inf % 1% temp %/{out}. inf
Del {out}. inf
It's messy, but it works.
All the preparations are ready. The following is the implementation method:
1. First, you need to prepare a machine as the DHCP server and configure the IP address pool. However, this IP address pool should not be the same as the IP address you want to allocate, otherwise it will be a lot of trouble. For example, if you want to assign an IP address segment from 10.16.19.1 to 10.16.19.100 to the cloned machine, you can assign the IP address 192.168.1.1 to your DHCP server, in addition, the allocable address pool is configured to 192.168.1.2 to 192.168.1.101. It is recommended that you do not set the lease period to a long time. Otherwise, the IP address may not be allocated enough, or you can clear the lease during each operation.
2. Install the client machine.
3. Copy the compiled executable file, two batch files, and one Access database file to a directory;
4. In the directory "C: /Documents and Settings/All Users/Start Menu/Program/start/create a shortcut pointing to the executable file (the operating system is different, directories may also be different );
5. Set the IP address of the client machine to be automatically obtained.
6. Shut down the computer and clone the hard disk with a blue card;
7. Execute the "assign IP Address" function from the blue ocean card. All machines will be restarted automatically. When the machine starts up, an IP address will be automatically requested, for example, 192.168.1.2, And the IP addresses of all machines will not conflict, this prevents the problem that the MAC address cannot be obtained due to IP address conflicts. Then, a program compiled by myself will be called, and the process of automatically changing the IP address, setting the machine name, and restarting will be completed. In this way, the IP address and computer name are automatically corrected. (Users without a Blue Card can also manually restart the host to implement automatic correction)
Iii. Existing Problems
At present, the problem of cloning the NT Server has not been completely solved. For example, there will be a unique identifier in the NT, and it will also be copied during the cloning process, in addition, the above method cannot modify the AD Domain Name of the NT Server. To solve this problem, you must start with the installation. This solution is provided in the installation instructions of Windows NT 2000. If you need to keep improving, you can refer to this reference.