Windows 注册表 DWORD 修改

时间:2020-12-25 06:41:48

标签: python-3.x windows registry

正在寻找有关我正在编写的脚本的帮助。

该脚本的目标是自动更改“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.10。

使用单独的变量作为字符串进行测试:变量 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,如果我要手动配置该值iptoint2 变量,它应该是 11.12.13.14,这是正确的。

CIPC TFTP Server screenshot

#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 岁一样随意和我说话。任何援助将不胜感激。这是我第一次发布脚本帮助,所以如果我需要解释关于我的代码/问题的任何其他内容,请告诉我。

1 个答案:

答案 0 :(得分:0)

最终像这样解决问题:

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