Python variable conditional test fails
I have a python script I wrote that connects me to a VPN. Before it does so, it tests my external IP, and records the output as a variable, "originip" then it connects to my vpn, and runs the test again. It displays my originip, then my newip, then it runs a conditional if that tests if origin ip and newip are the same then say there was an error. I'll add additional logic there later. Currently, the program works fine but it always prints "there was an error, resetting" instead of going to the Else line and printing "you are now connected successfully"
I think there's something wrong with my if logic here:
if newip and originip == originip: print "there was an error, resetting..." else: print "You are now connected successfully"
So I've tested the above, and when my VPN connects ok, it reports the new and the old IP address as different, then prints "there was an error, resetting" If it connects, and displays both newip and originip as the same, it also goes to print "there was an error, resetting..."
I have not been able to get it to execute the else part of that above statement.
Here's the entire python side of the program
#!/usr/bin/env python import pexpect import sys import os import time import subprocess secdelay = int(raw_input("How many seconds before resetting? ")) p = subprocess.Popen(["./checkmyip.sh"], shell=False, stdout=subprocess.PIPE) originip = p.stdout.read() print 'Public IP Address is', originip child = pexpect.spawn ('./vpn.sh -arg1') child.expect ('') child.expect ('(?i)Enter Aut Username:') child.sendline ('myusername') child.expect ('(?i)Enter Auth Password:') child.sendline ('mypassword') print "Establishing connection..." time.sleep(10) p = subprocess.Popen(["./checkmyip.sh"], shell=False, stdout=subprocess.PIPE) newip = p.stdout.read() print "The New IP is ",newip print "the old IP was ", originip if newip and originip == originip: print "there was an error, resetting..." else: print "You are now connected successfully" print "sleeping for ",secdelay," seconds" time.sleep(secdelay) child.sendcontrol('c') if child.isalive(): child.sendcontrol('c') child.close() if child.isalive(): print 'Child did not exit gracefully.' else: print 'Child exited gracefully.'
Finnaly, here is the code I added to my "checkmyip.sh" script. It's just a simple wget:
#!/usr/bin/env bash wget -q -O - checkip.dyndns.org|sed -e 's/.*Current IP Address: //' -e 's/<.*$//'
So the script works fine, it's just this error-checking logic that is confusing me. Why my if x and y == x is not working tho, when x and y both enumerate different values in the print line directly above my if statement, I'm confused on.
Any suggestions or help would be extremely appreciated. Thanks for reading!
Thanks for the help everyone! The fixed code was this:
if newip and originip == originip:
was changed to
if newip == originip:
Try this instead:
if newip == originip: print "there was an error, resetting..."
Yup, the condition originip == originip is always going to be True.
Thus, if newip is not empty, the whole expression newip and originip == originip is also going to be True:
>>> originip = 'foo' >>> originip == originip True >>> newip = '' >>> newip and originip == originip False >>> newip = 'bar' >>> newip and originip == originip True
Did you mean:
if newip and newip == originip: