Quản lý các mạng Windows dùng script - Phần 6: Những bước đầu tiên về scripting remote

12 7 0
Quản lý các mạng Windows dùng script - Phần 6: Những bước đầu tiên về scripting remote

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Tìm hiểu kỹ thuật scripting remote (viết kịch bản chạy trên các máy từ xa) sử dụng lớp WMI Win32_NetworkAdapterConfiguration đã được giới thiệu ở phần trước. Trở lại với script ChangeIPAddress.vbs mà chúng ta đã phát triển ở các phần trước để thay đổi địa chỉ IP của một bộ điều hợp mạng: Option Explicit Dim objWMIService Dim objNetAdapter Dim strComputer Dim strAddress Dim arrIPAddress Dim arrSubnetMask Dim colNetAdapters Dim errEnableStatic If WScript.Arguments.Count = 0 Then Wscript.Echo Usage: ChangeIPAddress.vbs new_IP_address WScript.Quit End If strComputer = . strAddress = Wscript.Arguments.Item(0) arrIPAddress = Array(strAddress) arrSubnetMask = Array(255.255.255.0) Set objWMIService = GetObject(winmgmts:\...

Quản lý mạng Windows dùng script - Phần 6: Những bước scripting remote Tìm hiểu kỹ thuật scripting remote (viết kịch chạy máy từ xa) sử dụng lớp WMI Win32_NetworkAdapterConfiguration giới thiệu phần trước Trở lại với script ChangeIPAddress.vbs mà phát triển phần trước để thay đổi địa IP điều hợp mạng: Option Explicit Dim objWMIService Dim objNetAdapter Dim strComputer Dim strAddress Dim arrIPAddress Dim arrSubnetMask Dim colNetAdapters Dim errEnableStatic If WScript.Arguments.Count = Then Wscript.Echo "Usage: ChangeIPAddress.vbs new_IP_address" WScript.Quit End If strComputer = "." strAddress = Wscript.Arguments.Item(0) arrIPAddress = Array(strAddress) arrSubnetMask = Array("255.255.255.0") Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") Set colNetAdapters = objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE") For Each objNetAdapter in colNetAdapters errEnableStatic = objNetAdapter.EnableStatic(arrIPAddress, arrSubnetMask) Next Chú ý thích loại bỏ phần mã cuối hiển thị kết Điểm qua lại chút hoạt động script này: Kết nối tới namespace root\cimv2 máy cục Sử dụng lệnh SELECT để trả tập hợp cấu hình điều hợp mạng có đường bao TCP/IP phép sử dụng Thay đổi địa IP điều hợp thành giá trị mơ tả tham số dịng lệnh Chúng ta ghi lại script thư mục C:\localtest máy Windows XP có địa IP tĩnh 172.16.11.43 Sau đó, mở sổ dịng lệnh Command Prompt với vai trò admin máy sử dụng script để thay đổi địa IP máy thành 172.16.11.54: C:\locatest>ipconfig Windows IP Configuration Ethernet adapter Local Area Connection: Connection-specific DNS Suffix : IP Address : 172.16.11.43 Subnet Mask : 255.255.255.0 Default Gateway : 172.16.11.1 C:\locatest>ChangeIPAddress.vbs 172.16.11.54 Microsoft (R) Windows Script Host Version 5.6 Copyright (C) Microsoft Corporation 1996-2001 All rights reserved C:\locatest>ipconfig Windows IP Configuration Ethernet adapter Local Area Connection: Connection-specific DNS Suffix : IP Address : 172.16.11.54 Subnet Mask : 255.255.255.0 Default Gateway : 172.16.11.1 C:\locatest> Chú ý 1: Nên nhớ rằng, để thay đổi địa IP máy Windows XP đòi hỏi phải sử dụng đặc quyền quản trị cục Do đó, bạn đăng nhập vào máy với vai trị người dùng miền bình thường, cần phải mở cửa sổ Command Prompt, sau gõ lệnh: runas /user:administrator cmd.exe để mở cửa sổ Command Prompt thứ hai chạy ngữ cảnh vai trò quản trị cục cuối chạy script từ cửa sổ Command Prompt thứ hai Nhưng muốn chạy script máy sử dụng để thay đổi địa IP máy khác sao? Nói cách khác, muốn chạy script từ xa máy Windows XP xa phải làm nào? Cố gắng Chúng ta bắt đầu việc đăng nhập vào trạm làm việc admin xp.contoso.com, sử dụng thông tin thẩm định tài khoản admin có tên Mary Jones Chúng ta cần thực điều admin miền có đặc quyền quản trị cục tất máy miền Do đó, chạy script từ trạm admin máy từ xa, script hoạt động Bây có script ChangeIPAddress.vbs thư mục C:\tools trạm admin xp.contoso.com Mở cửa sổ lệnh Command Prompt máy gõ vào hai dòng sau: C:\Documents and Settings\mjones>cd \tools C:\tools>notepad ChangeIPAddress.vbs Script mở Notepad, thay đổi dòng bên dưới: strComputer = "." thành: strComputer = "xp2 " Sau vào File/Save để ghi lại thay đổi đóng Notepad Bây giờ, chạy script: C:\tools>ChangeIPAddress.vbs 172.16.11.65 Microsoft (R) Windows Script Host Version 5.6 Copyright (C) Microsoft Corporation 1996-2001 All rights reserved C:\tools\ChangeIPAddress.vbs(20, 1) Microsoft VBScript runtime error: The remote server machine does not exist or is unavailable: 'GetObject' C:\tools> Chú ý phải lúc trước thông báo lỗi xuất Nhưng script có làm việc không? Nếu đăng nhập vào máy từ xa xp2.contoso.com, mở cửa sổ Command Prompt gõ ipconfig, kết thu là: C:\locatest>ipconfig Windows IP Configuration Ethernet adapter Local Area Connection: Connection-specific DNS Suffix : IP Address : 172.16.11.43 Subnet Mask : 255.255.255.0 Default Gateway : 172.16.11.1 C:\locatest> Bạn thấy địa máy 172.16.11.43 Tức script không hoạt động Vậy, sai điểm nào? Thông báo lỗi có vấn đề với dịng 20 script Dịng 20 có dạng: Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") Dường script kết nối với dịch vụ WMI máy từ xa Nguyên nhân đâu? Cố gắng thứ hai Chúng ta làm điều với Windows Firewall máy từ xa Nên nhớ rằng, Windows XP SP2 có tường lửa loại bỏ hầu hết lưu lượng đến ngoại trừ lưu lượng cấu hình riêng Cách đơn giản để kiểm tra điều tắt Windows Firewall máy tính đích Đăng nhập vào xp2.contoso.com với vai trò admin, mở ứng dụng Windows Firewall Control Panel chọn thiết lập Off tab General Bây chạy lại script từ trạm admin: C:\tools>ChangeIPAddress.vbs 172.16.11.65Microsoft (R) Windows Script Host Version 5.6 Copyright (C) Microsoft Corporation 1996-2001 All rights reserved C:\tools\ChangeIPAddress.vbs(23, 6) SWbemObjectEx: The remote procedure call failed C:\tools> Lại lỗi khác, lại phải lâu thông báo lỗi xuất Nhưng lỗi khác ghi nhận dòng 23: errEnableStatic = objNetAdapter.EnableStatic(arrIPAddress, arrSubnetMask) Bây giờ, gõ lệnh ipconfig cửa sổ Command Prompt máy từ xa, ta có kết sau: C:\locatest>ipconfig Windows IP Configuration Ethernet adapter Local Area Connection: Connection-specific DNS Suffix : IP Address : 172.16.11.65 Subnet Mask : 255.255.255.0 Default Gateway : 172.16.11.1 C:\locatest> Có vẻ script hoạt động! Nhưng để lại hai vấn đề: Chúng ta không muốn tắt chức Windows Firewall máy từ xa chạy script chúng Vậy, liệu có cần phải thiết lập ngoại lệ để máy cho phép chạy script từ xa mở Windows Firewall Còn lỗi RPC trên? Script hoạt động trả lỗi Tại lại vậy? Ngoại lệ cho scripting từ xa Mở lại Windows Firewall máy từ xa, script không chạy Bây giờ, từ cửa sổ Command Prompt mức admin máy, gõ lệnh gpedit.msc để mở Local Group Policy đặt thiết lập sách sau (xem Hình 1): Computer Configuration\Administrative Templates\Network\Network Connections\Domain Profile\Windows Firewall: Allow remote administration exception Hình 1: Thiết lập sách Windows Firewall phép quản trị từ xa Kích đúp lên thiết lập sách cho phép mạng cục (Hình 2) Nhớ trạm admin phải nằm subnet với máy tính đích Hình 2: Cho phép ngoại lệ quản trị từ xa Chú ý 2: Tất nhiên, bạn muốn thực điều khác chút cấu hình thiết lập sách Group Policy Object (GPO) miền thay cục Nhưng bạn cho phép ngoại lệ máy cục để chạy tường lửa với script Bây giờ, chạy lại script từ trạm admin cố gắng thay đổi địa IP máy từ xa từ 172.16.11.65 thành 172.16.11.66: C:\tools>ChangeIPAddress.vbs 172.16.11.66 Microsoft (R) Windows Script Host Version 5.6 Copyright (C) Microsoft Corporation 1996-2001 All rights reserved C:\tools\ChangeIPAddress.vbs(23, 6) SWbemObjectEx: The remote procedure call failed C:\tools> Lại lỗi xuất hiện, gõ lệnh ipconfig vào cửa sổ Command Prompt máy từ xa, ta có kết sau: C:\locatest>ipconfig Windows IP Configuration Ethernet adapter Local Area Connection: Connection-specific DNS Suffix : IP Address : 172.16.11.66 Subnet Mask : 255.255.255.0 Default Gateway : 172.16.11.1 C:\locatest> Nó làm việc! Như vậy, mở Windows Firewall máy từ xa đồng thời dùng Group Policy để mở ngoại lệ cho quản trị từ xa tường lửa thay đổi từ xa địa IP máy cách chạy script từ trạm admin Chúng ta giải vấn đề thứ nhất, vấn đề thứ hai sao? Bí mật có sức hấp dẫn riêng Vì thế, tạm thời để vấn đề thứ hai sang phần sau ... hiểu kỹ thuật scripting remote (viết kịch chạy máy từ xa) sử dụng lớp WMI Win32_NetworkAdapterConfiguration giới thiệu phần trước Trở lại với script ChangeIPAddress.vbs mà phát triển phần trước... cảnh vai trò quản trị cục cuối chạy script từ cửa sổ Command Prompt thứ hai Nhưng muốn chạy script máy sử dụng để thay đổi địa IP máy khác sao? Nói cách khác, muốn chạy script từ xa máy Windows XP... cho phép chạy script từ xa mở Windows Firewall Còn lỗi RPC trên? Script hoạt động trả lỗi Tại lại vậy? Ngoại lệ cho scripting từ xa Mở lại Windows Firewall máy từ xa, script không chạy Bây giờ,

Ngày đăng: 11/05/2021, 03:19

Mục lục

  • Quản lý các mạng Windows dùng script - Phần 6: Những bước đầu tiên về scripting remote

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan