簡體   English   中英

Windows注冊表DWORD修改

[英]Windows Registry DWORD modification

在我正在處理的腳本上尋找一些幫助。

該腳本的目標是自動更改“tftpserver1”的 REG_DWORD 子項,這適用於應用程序 Cisco IP 通信器。

REG_DWORD 采用十六進制或十進制格式的 IP 地址。 一個小問題是需要反轉 IP 地址的值。 EX) 10.11.12.13,需要反轉為 13.12.11.10,然后轉換為 HEX 或十進制並在 REG_DWORD 中更新。

我使用示例 IP 地址 10.11.12.13 看到的問題 - 轉換為二進制並反轉后,返回 IP 地址的十進制值顯示為 31.21.11.1,而不是所需的 31.21.11。

使用單獨的變量作為字符串進行測試:變量 testip。 示例 IP 地址 11.12.13.14 - 轉換為二進制並反轉后,返回 IP 地址的十進制值顯示為 41.31.21.11,這是所需的。

我不確定為什么要去除結尾的零。 這是一個問題,因為如果它被輸入到注冊表中

如屏幕截圖所示,如果在注冊表中配置了 iptoint 變量的十進制值,則為 CIPC(Cisco IP Communicator)設置的 IP 為 1.12.13.14 而不是 10.12.13.14 的值,如果 Ipto2 的值配置為 MANUALLY變量,這將是正確的 11.12.13.14。

CIPC TFTP 服務器截圖

#Modules imported
import configparser
import winreg
import ipaddress

def regkey(): 
    config = configparser.ConfigParser()                                                                                          # Variable for configparser as config
    config.read(r'<ACTUAL DIRECTORY REDRACTED>IP_VARS.ini')          #REMOVED DIRECTORY FOR THIS POST                                               # Variable to read .INI file
    tftp1 = config['IP_VAR_1']['tftp_server1']                                                                                    # Variable to pull 'tftp_server' IP from .INI
    testip = "11.12.13.14"                                                                                                        # Variable for second test IP as a string
    iptoint =  int(ipaddress.IPv4Address(tftp1[::-1]))                                                                            # Variable to take tftp1 as string turn into decimal(integer)
    iptoint2 = int(ipaddress.IPv4Address(testip[::-1]))                                                                           # Variable to take testip as string, turn into decimal(integer)

    tshoot = ipaddress.IPv4Address(iptoint)
    tshoot2 = ipaddress.IPv4Address(iptoint2)

    with winreg.ConnectRegistry(None, winreg.HKEY_LOCAL_MACHINE) as hkey:                                                         # Connect to registry as hkey
        with winreg.OpenKey(hkey, r"SOFTWARE\WOW6432Node\Cisco Systems, Inc.\Communicator", 0, winreg.KEY_ALL_ACCESS) as sub_key: # Open hkey for CIPC as sub_key
            current_key_value,current_key_type = winreg.QueryValueEx(sub_key, "TftpServer1")                                      # Show current sub_key value



        print("This is the Current Decimal Value of the Tftpserver1 DWORD:" + "\t" + f"{current_key_value}")
        print("This is the Type of the DWORD:" +  "\t" + f"{current_key_type}")
        print()
        print("The below value is the selected TFTP server IP, converted to decimal and reversed.")
        print()
        print(iptoint)
        print("This is testip variable converted to decimal and reversed")
        print()
        print(iptoint2)
        print()
        print(tshoot)
        print(tshoot2)

結果

This is the Current Decimal Value of the Tftpserver1 DWORD: 689902859
This is the Type of the DWORD:  4

The below value is the selected TFTP server IP, converted to decimal and reversed.

521472769
This is testip variable converted to decimal and reversed

689902859

31.21.11.1
41.31.21.11

Process finished with exit code 0

作為免責聲明,我對編碼非常陌生。 隨時跟我說話,就像我 5 歲一樣。 任何援助將不勝感激。 這是我第一次發布腳本幫助,所以如果我需要解釋有關我的代碼/問題的任何其他內容,請告訴我。

最終解決這樣的問題:

import configparser
    def reverse(ip):
        if len(ip) <= 1:
            return ip
        return '.'.join(ip.split('.')[::-1])
    config = configparser.ConfigParser()                                                                                          # Variable for configparser as config
    config.read(r'C:\Users\Gerni\Desktop\JarvisGerni Script\IP_VARS.ini')                                                         # Variable to read .INI file
    tftp1 = config['IP_VAR_1']['tftp_server1']                                                                                    # Variable to pull 'tftp_server' IP from .INI
    
    print(reverse(tftp1))

結果:

  13.12.11.10

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM