What is the best way to invoke the os.system command and assign it to a variable without printing the output ?

Example :

ifName  = os.system('snmpwalk -v 1 -c <community> %s ifName.%s' % (switch,port))

Also with the output of the snmpwalk I will be wanting to only assign the last column. I know I could do with this with regex (re) but wondered what the most elegant way to achieve this was (i.e string formatting etc)...

Thanks,

Recommended Answers

All 6 Replies

Use subprocess with Gribouillis Command class: http://www.daniweb.com/software-development/python/code/257449

For your extraction of last column I do not understand as you give no sample of output and what you want from that. The man page of Linux http://linux.die.net/man/1/snmpwalk gives example output like this:
sysDescr.0 = STRING: "SunOS zeus.net.cmu.edu 4.1.3_U1 1 sun4m"
sysObjectID.0 = OID: enterprises.hp.nm.hpsystem.10.1.1
sysUpTime.0 = Timeticks: (155274552) 17 days, 23:19:05
sysContact.0 = STRING: ""
sysName.0 = STRING: "zeus.net.cmu.edu"
sysLocation.0 = STRING: ""
sysServices.0 = INTEGER: 72

It has no columns.

Thanks Ill have a look at the lnk shortly. Relating to the column part, I may of worded it incorrectly.

Based on the output you added I would want to capture (as a variable) anything after the :.

Such as :
SunOS zeus.net.cmu.edu 4.1.3_U1 1 sun4m
(155274552) 17 days, 23:19:05
zeus.net.cmu.edu

etc etc

Thanks for your help...

Partition is one way (split is another):

Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> data = """sysDescr.0 = STRING: "SunOS zeus.net.cmu.edu 4.1.3_U1 1 sun4m"
sysObjectID.0 = OID: enterprises.hp.nm.hpsystem.10.1.1
sysUpTime.0 = Timeticks: (155274552) 17 days, 23:19:05
sysContact.0 = STRING: ""
sysName.0 = STRING: "zeus.net.cmu.edu"
sysLocation.0 = STRING: ""
sysServices.0 = INTEGER: 72"""
>>> for info in data.splitlines():
        print info.partition(':')[-1]

        
 "SunOS zeus.net.cmu.edu 4.1.3_U1 1 sun4m"
 enterprises.hp.nm.hpsystem.10.1.1
 (155274552) 17 days, 23:19:05
 ""
 "zeus.net.cmu.edu"
 ""
 72
>>>

Is there not an easier way to get the output from a binary passed into a variable.
Seems like having to create a class etc etc seems pretty long winded.

Thanks,

Is there not an easier way to get the output from a binary passed into a variable.
Seems like having to create a class etc etc seems pretty long winded.

Thanks,

Output from a binary?

The binary being snmpwalk...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.