6 Replies Latest reply: Jun 11, 2018 12:29 PM by Leandro Edmundo de Sousa RSS

Python script doesn't throw a syntax error while it does when run interactively.

Ross Tanner

Interactive python returns a syntax error when a "bad" command is sent, but running from a script returns nothing. What am I doing wrong?

 

I am using SteelScript and Python to run through a list of configuration commands. I have the mode set to  config, using "sh.cli.default_mode = CLIMode.CONFIG" as per the tutorial.  When I run the command with incorrect syntax interactively, (using "sh.cli.exec_command("dud command") ), I get an error; when I run it using a script, it "works' - I get no indication from the script that there is anything wrong.

 

Code is below ...

---------------------------------------------------------------

#! /usr/bin/env python


from steelscript.common.service import UserAuth

from steelscript.steelhead.core import steelhead

from steelscript.cmdline.cli import CLIMode

import steelscript.cmdline.exceptions


import steelscript

import getpass


def cyclethrough(file):

    with open(file) as f:

        for line in f:

            print "*****************************"

            print line

            output = sh.cli.exec_command(line)

            print output


user = "username"

password = "secret"

host = "10.10.20.30"


auth = UserAuth(username=user, password=password )


sh = steelhead.SteelHead(host = host, auth = auth)


cyclethrough("riverbedcommands.txt")


sh.cli.default_mode = CLIMode.CONFIG


with open("configcommands.txt") as f:

    for line in f:

       try:

           sh.cli.exec_command(line,error_expected=True)

       except steelscript.cmdline.exceptions.CLIError:

           print "Syntax Error")

 

sh.cli.default_mode = CLIMode.ENABLE


cyclethrough("moreshowcommands.txt")