puterguy01
puterguy01

Reputation: 135

"if" statement based on command?

I understand from Python's documentation that I can check conditions with the format:

if x < 0:
else:
    print('Else code')

I want to run a command and check whether that command is successful, but I get a "SyntaxError: invalid syntax" error. Help!

My format is roughly:

if myCommand(parameters):
    print("It worked")
else:
    print("It failed")
    if mySecondCommand(parameters):
        print("2nd command worked")
    else:
        print("2nd command failed")

And here's my actual code using the Netmiko library:

from netmiko import ConnectHandler
if net_connect = ConnectHandler(device_type='cisco_ios', ip='11.22.33.44', username='domain.login', password='DomainPassword'):
    print("Domain login succeeded.")
else:
    print("Domain login failed.")
    if net_connect = ConnectHandler(device_type='cisco_ios', ip='11.22.33.44', username='StandardUsername', password='StandardPassword'):
        print("Standard login worked")
    else:
        print("Standard login failed")

The output I get is:

if net_connect = ConnectHandler(device_type='cisco_ios', ip='11.22.33.444', username='domain.login', password='DomainPassword'):
               ^
SyntaxError: invalid syntax

Upvotes: 1

Views: 815

Answers (4)

kyriakosSt
kyriakosSt

Reputation: 1772

Considering the comments provided to this question, it seems that what you essentially need is how to know if the connection failed. Since a failed connection raises an exception, if-statements are not enough / needed to achieve what you want. Instead, you would like to do this:

from netmiko import ConnectHandler
try:
    net_connect = ConnectHandler(device_type='cisco_ios', ip='11.22.33.44', username='domain.login', password='DomainPassword')
    print("Domain login succeeded.")
except:
    print("Domain login failed.")

    try:
        net_connect = ConnectHandler(device_type='cisco_ios', ip='11.22.33.44',username='StandardUsername', password='StandardPassword')
        print("Standard login worked")
    except:
        print("Standard login failed")

If you have not come across "try-except" statements in Python, I suggest you looked them up.

Upvotes: 2

A. Smoliak
A. Smoliak

Reputation: 448

Your original snippet will raise a syntax error, you can't run this (thanks to KyrST):

if x < 0:
else:
    print('Else code')

you'd want this:

if not x < 0:
    print("Not X < 0 code")

The issue that caused the error lies here:

if net_connect = ConnectHandler(device_type='cisco_ios', ip='11.22.33.44', username='domain.login', password='DomainPassword'):

In Python, you cannot assign a value inside a condition, and even if you did, what would you compare it to? what you should do is instead of the above:

net_connect = ConnectHandler(device_type='cisco_ios', ip='11.22.33.44', username='domain.login', password='DomainPassword'):
if net_connect == some_check_here:

some_check_here being some value you can use to compare if something goes wrong, I'm not sure how netmiko handles errors, my guess is that you'll have to catch exceptions.

Upvotes: 1

ocelot
ocelot

Reputation: 1107

Unfortunately, Python can't have variable assignment inside an if statement - it can't be used as an expression.

with the = operator, you're attempting to assign the net_connect variable, rather than compare it.

the == operator is the correct operator for comparison, so:

if net_connect == ConnectHandler(device_type='cisco_ios', ip='11.22.33.44', username='domain.login', password='DomainPassword'): is correct, as you're comparing the two.

Your original, if net_connect = ConnectHandler(device_type='cisco_ios', ip='11.22.33.444', username='domain.login', password='DomainPassword'):, the ConnectHandler to net_connect.

Upvotes: 0

C. Braun
C. Braun

Reputation: 5201

You are trying to assign a variable within an if statement (not allowed in Python), instead of checking for equality.

Try:

 if net_connect == ConnectHandler(device_type='cisco_ios', ip='11.22.33.44', username='domain.login', password='DomainPassword'):
    ...

and notice the use of == instead of =.

Upvotes: 0

Related Questions