Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating a script for CLI based device admin 1

Status
Not open for further replies.

atascoman

Technical User
Oct 10, 2003
868
0
0
US
Hello,

New to Python. I am working on learning the code, but I have a question to see what I am up against. I had written a few scripts in VBS that we use for automating device configurations, but unfortunately they won't work for the new guys that use MACs. The terminal software will only open Python scripts.

My goal is to pull data from a tab delimited text file, assign each field to an array variable and then insert that variable into a string that is pushed to the CLI prompt as it comes up. I know the following code is not correct, just trying to show my intent.

filename:config.txt

array(0)=ipaddr1
array(1)=ipmask
array(2)=ipgatew


VSP9000(config#)> ip adress [ipaddress1]
VSP9000(config#)> ip mask [ipmask]
VSP9000(config#)> ip default-gateway [ipgatew]


I also want to recreate the ability to have the script access multiple lines of data from the text file and loop each time the series of commands is completed and then grab the next line from the text file. Any help or advice is greatly appreciated.
 
Without writing the code I would suggest you investigate the CSV module {field delimiters are configurable so tab separation should not be an issue) and the os module
os.popen should enable you to execute commands at the command line level (no need to actual open a CLI.)




A Maintenance contract is essential, not a Luxury.
Do things on the cheap & it will cost you dear
 
I think for an application this simple, you don't really gain anything from using the CSV module. Look at the "split()" built-in function, particularly, "split("\t")". Also, in Python, unless you use a heavy math adjunct like "numpy" or "scipy", there aren't any array's per se. Python uses lists, tuples, and more generally, "sequences". The only real difference for your application is that the indices are square bracketed: "". So when you read a line from your text file, first you would get a string and strip off the linefeed:
Code:
for line in <file object name>:
    strA=line.strip('\n')
Then you would make a list by splitting on the tabs:
Code:
lstA=strA.split('\t')

Now, if you have a list of values of some particular field, you append a new value to that list:
Code:
fieldN.append(lstA[n])

_________________
Bob Rashkin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top