Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Python multiple IF statements

Posted on 2016-08-21
4
Medium Priority
?
207 Views
Last Modified: 2016-08-25
Hi Experts,

The script is currently only looking at the first IF statement and not corresponding IF statements when the condition does not match.

Hope someone can shed some light on the following script (Run in Securecrt) to log into the console port for a Cisco device and look for prompts and execute corresponding lines.

Thank you




# $language = "python"
# $interface = "1.0"

# Connect to an SSH server using the SSH2 protocol. Specify the
# username and password and hostname on the command line as well as
# some SSH2 protocol specific options.
# msgbox = crt.Dialog.MessageBox

def main():
# Prompt for a password instead of embedding it in a script...
#
# passwd = crt.Dialog.Prompt("Enter password for " + host, "Login", "", True)
      passwd = crt.Dialog.Prompt

# Build a command-line string to pass to the Connect method.
cmd = "/SERIAL COM6 /BAUD 9600 /NOCTS /DATA 8 /NODSR /PARITY NONE /NOXON"
crt.Session.Connect(cmd, False)
crt.Screen.Synchronous = True
# This line will need to be modified to wait for what the remote will send.
crt.Screen.Send(chr(13))

initial_1 = crt.Screen.WaitForString("outer")
initial_2 = crt.Screen.WaitForString("initial configuration dialog")
initial_3 = crt.Screen.WaitForString("con0")
initial_4 = crt.Screen.WaitForString("odified")
initial_5 = crt.Screen.WaitForString("Press RETURN to get started")


if initial_1 == True:
      crt.Screen.Send("en" + chr(13))
      crt.Screen.WaitForString("Password", 2)
      crt.Screen.Send("password" + chr(13))
      crt.Screen.WaitForString("#")
      crt.Screen.Send("erase startup-config" + chr(13))
      crt.Screen.WaitForString("Erasing")
      crt.Screen.Send(chr(13))
      crt.Screen.WaitForString("#")
      crt.Screen.Send("delete nvram:vlan.dat" + chr(13))
      crt.Screen.WaitForString("vlan.dat")
      crt.Screen.Send(chr(13))
      crt.Screen.WaitForString("confirm")
      crt.Screen.Send(chr(13))
      crt.Screen.Send(chr(13))
      crt.Screen.WaitForString("#")
      crt.Screen.Send("reload" + chr(13))
      crt.Screen.Send(chr(13))
      crt.Screen.WaitForString("System configuration has been modified", 2)
      crt.Screen.Send("no" + chr(13))
      crt.Screen.WaitForString("Proceed with reload", 2)
      crt.Screen.Send(chr(13))

if initial_2 == True:
      crt.Screen.Send("no" + chr(13))
      crt.Screen.Send(chr(13))
      crt.Screen.WaitForString("Would you like to terminate autoinstall", 3)
      crt.Screen.Send(chr(13))
      crt.Screen.Send(chr(13))

if initial_3 == True:
      crt.Screen.Send(chr(13))
      crt.Screen.WaitForString("rt1")
      crt.Screen.Send("en" + chr(13))
      crt.Screen.WaitForString("Password")
      crt.Screen.Send("password" + chr(13))
      crt.Screen.WaitForString("#")
      crt.Screen.Send("erase startup-config" + chr(13))
      crt.Screen.WaitForString("Erasing")
      crt.Screen.Send(chr(13))
      crt.Screen.WaitForString("#")
      crt.Screen.Send("delete nvram:vlan.dat" + chr(13))
      crt.Screen.WaitForString("vlan.dat")
      crt.Screen.Send(chr(13))
      crt.Screen.WaitForString("confirm")
      crt.Screen.Send(chr(13))
      crt.Screen.Send(chr(13))
      crt.Screen.WaitForString("#")
      crt.Screen.Send("reload" + chr(13))
      crt.Screen.Send(chr(13))

if initial_4 == True:
      crt.Screen.Send(chr(13))
      crt.Screen.WaitForString("confirm")
      crt.Screen.Send(chr(13))
      crt.Screen.Send(chr(13))
      
if initial_5 == True:
      crt.Screen.Send(chr(13))
      #
      crt.Screen.Send("en" + chr(13))
      #
      crt.Screen.Send("delete flash:vlan.dat" + chr(13))
      #
      crt.Screen.Send(chr(13))
      crt.Screen.Send(chr(13))
      crt.Screen.Send(chr(13))
      crt.Screen.WaitForString("#")
      #
      crt.Screen.Send("config t" + chr(13))
      crt.Screen.WaitForString("config")
      #
      crt.Screen.Send("interface Cellular0/1/0" + chr(13))
      crt.Screen.WaitForString("config")
      crt.Screen.Send("shut" + chr(13))
      crt.Screen.WaitForString("config")
      #
      crt.Screen.Send("end" + chr(13))
      crt.Screen.WaitForString("#")
0
Comment
Question by:synsynackack
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 20

Expert Comment

by:jmcg
ID: 41764780
Perhaps I'm misunderstanding your program's intent, but it looks to me like you evaluate all of the initial_n variables once at the beginning of the script and never go back. So initial_1 is True while all the others are False.

There's a language called "Expect" that started out in the TCL/TK world, but has versions for Perl and for Python (Pexpect). There quite a bit to learn, but once you've got the hang of it, it works much better for scripting interactions like this.
0
 

Author Comment

by:synsynackack
ID: 41764937
Sorry about that, please let me rephrase my question:

The script is suppose to log into the Cisco device (via console port), look for a matching IF statement and execute the corresponding code.

The script currently only attempts to match the first IF statement and ignores the rest.

How would it be possible for the script to search down the list of IF statements and execute the relevant one(IF True)?

Thank you
0
 
LVL 20

Accepted Solution

by:
jmcg earned 2000 total points
ID: 41765761
What's your basis for thinking the later if statements are not reached?

Before I started using Expect, I would write these dialogs with a loop around a single send/read statement and set up a state machine loop, where the received input plus current state would determine the next state. Waiting for a particular string to show up from the partner was too fragile.
0
 

Author Comment

by:synsynackack
ID: 41766318
Hi jmcg,

Managed to work out the issue, will post the script after some further testing.

Thanks for your advice.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of the companies I’ve worked with have embraced cloud solutions due to their desire to “get out of the datacenter business.” The ability to achieve better security and availability, and the speed with which they are able to deploy, is far grea…
On Feb. 28, Amazon’s Simple Storage Service (S3) went down after an employee issued the wrong command during a debugging exercise. Among those affected were big names like Netflix, Spotify and Expedia.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question