I'm considering collect server data and in those servers Python 2.6 are pre-installed. Now I wonder if there are Python library correspond to "facter" of Ruby, not t开发者_运维问答he Python "binding" for facter.
I googled about it but couldn't find any. Does anyone have any idea about this?
I can't see any reason why you wouldn't just use facter itself. The output format is easily consumable from within a python script.
import subprocess
import pprint
def facter2dict( lines ):
res = {}
for line in lines:
k, v = line.split(' => ')
res[k] = v
return res
def facter():
p = subprocess.Popen( ['facter'], stdout=subprocess.PIPE )
p.wait()
lines = p.stdout.readlines()
return facter2dict( lines )
if __name__ == "__main__":
pprint.pprint( facter() )
Salt implements a Facter replacement called Grains.
http://docs.saltstack.org/en/latest/ref/modules/index.html#grains-data
There is also an attempt to do this called Phacter
http://code.google.com/p/speed/wiki/Phacter
I haven't tried it, however I agree with the concept. One may not want/be able to install Ruby on their system but want the similar functionality.
Somewhat new http://github.com/hihellobolke/sillyfacter/
Install using
# Needs pip v1.5
pip install --upgrade --allow-all-external --allow-unverified netifaces sillyfacter
You may beed to upgrade pip too
# To upgrade pip
pip install --ugrade pip
Here is a more compressed version of @AndrewWalker's suggestion. It also ensures ' => ' is present before splitting and removes the trailing \n :
import subprocess
p = subprocess.Popen( ['facter'], stdout=subprocess.PIPE )
p.wait()
facts = p.stdout.readlines()
# strip removes the trailing \n
facts = dict(k.split(' => ') for k in [s.strip() for s in facts if ' => ' in s])
print facts["architecture"]
I think I am going for facterpy, though. pip install facterpy
, then:
import facter
facts = facter.Facter()
print facts["architecture"]
精彩评论