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..."
    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(["./"], shell=False, stdout=subprocess.PIPE)
originip =
print 'Public IP Address is', originip
child = pexpect.spawn ('./ -arg1')
child.expect ('')
child.expect ('(?i)Enter Aut Username:')
child.sendline ('myusername')
child.expect ('(?i)Enter Auth Password:')
child.sendline ('mypassword')
print "Establishing connection..."
p = subprocess.Popen(["./"], shell=False, stdout=subprocess.PIPE)
newip =
print "The New IP is ",newip
print "the old IP was ", originip
if newip and originip == originip:
    print "there was an error, resetting..."
    print "You are now connected successfully"
print "sleeping for ",secdelay," seconds"
if child.isalive():
if child.isalive():
    print 'Child did not exit gracefully.'
    print 'Child exited gracefully.'

Finnaly, here is the code I added to my "" script. It's just a simple wget:

#!/usr/bin/env bash
wget -q -O -|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
>>> newip = ''
>>> newip and originip == originip
>>> newip = 'bar'
>>> newip and originip == originip

Did you mean:

if newip and newip == originip:


Need Your Help

Is Agile the best practice for software development?


As nowadays the main focus of software developers is to deliver the functionality to the satisfaction of the customer in very much short time that requires a short but hybrid team well communicating

How to present a recursive collection in JSP

jsp user-interface view collections jstl

I have a backend service which is returning me an Info object. This Info object has a list of FolderGroup objects which in turn has list of FolderGroup objects and so on.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.