1

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:
2
  • originip == originip this is always true. Commented Dec 28, 2012 at 14:31
  • if newip and originip == originip: I think you meant something else here... or your condition always reduce to True :-) Commented Dec 28, 2012 at 14:31

3 Answers 3

2

Try this instead:

if newip == originip:
    print "there was an error, resetting..."
Sign up to request clarification or add additional context in comments.

Comments

2

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:

instead?

2 Comments

Martijn thanks for that information, I see how the logic true and false applies now. 'if foo and bar == bar:' is what I used to fix it but I'll try 'if newip and newip == originip:' and see what happens
oops I mean if newip == originip: is what I used to fix it
1
>if newip and originip == originip:

You're testing the "boolean" value of newip then you are testing if originip is equal to itself, which is always true.

You probably mean:

if newip == originip:

1 Comment

exactly. This script just connects a VPN client, and tests to see if my internet facing IP is the same as it was before I connected to the VPN. Depending upon the condition met, the program can then take various actions

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.