Looking for some assistance on a script I am working on.
The goal of the script is to automatically change the REG_DWORD subkey for "tftpserver1", this is for the application Cisco IP communicator.
The REG_DWORD takes an IP address in HEX or decimal. A slight hiccup is that the value for the IP address needs to be reversed. EX) 10.11.12.13, needs to be reversed to 13.12.11.10 and then converted to HEX or decimal and updated in the REG_DWORD.
The problem I see using the example IP address 10.11.12.13 - after converting to binary and reversing, the value of the decimal back to an IP address shows as 31.21.11.1, NOT the 31.21.11.10 as desired.
Testing with a separate variable as a string: variable testip. The example IP address 11.12.13.14 - after converting to binary and reversing, the value of the decimal back to an IP address shows as 41.31.21.11, which IS desired.
I'm unsure why the ending zero is being stripped. This is a problem because if it were entered into the registry
As shown in the screenshot if the decimal value of the iptoint variable is configured in the registry, the IP set for the CIPC(Cisco IP Communicator) is 1.12.13.14 NOT 10.12.13.14, If I were to MANUALLY configure the value of the iptoint2 variable, it would be 11.12.13.14 which would be correct.
#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)
Result
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
As a disclaimer, I am EXTREMELY new to coding. Feel free to talk to me like i'm 5 years old. Any assistance would be appreciated. This is my first time posting for scripting help, so please let me know if I need to explain anything else regarding my code/issue.
End up fixing the problem like So:
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))
Result:
13.12.11.10
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.