寻找一些关于我正在工作的脚本的帮助。
脚本的目标是自动更改"tftpserver1“的REG_DWORD子键,这是针对应用程序Cisco IP communicator的。
REG_DWORD采用十六进制或十进制的IP地址。一个小问题是IP地址的值需要反转。例如) 10.11.12.13,需要反转到13.12.11.10,然后转换为十六进制或十进制,并在REG_DWORD中更新。
我在使用示例IP地址10.11.12.13时看到的问题-在转换为二进制并反转后,返回到IP地址的十进制值显示为31.21.11.1,而不是所需的31.21.11.10。
使用单独的变量作为字符串进行测试:变量testip。示例IP地址11.12.13.14 -转换为二进制并反转后,十进制值显示为41.31.21.11,这是所需的。
我不确定为什么末尾零被去掉了。这是一个问题,因为如果它被输入到注册表中
如截图所示,如果在注册表中配置了iptoint变量的十进制值,为CIPC(Cisco IP Communicator)设置的IP是1.12.13.14而不是10.12.13.14,如果我手动配置iptoint2变量的值,则应该是11.12.13.14,这是正确的。
#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岁。任何帮助都将不胜感激。这是我第一次发布脚本帮助,所以如果我需要解释关于我的代码/问题的任何其他事情,请告诉我。
发布于 2020-12-26 01:06:34
最终像这样解决问题:
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.10https://stackoverflow.com/questions/65445858
复制相似问题