这就是我想用我的代码完成的事情:
properties( IPADDR='192.168.0.1', NETMASK='255.255.255.0' ).apply_to( '/path/to/ifcfg-eth0' )
但是,它声明找不到IP地址作为文件或目录。但是从文档我们可以看出这是正确的格式。
参见:http://code.google.com/p/confparse/
以下代码:
def writestaticConf(nic, ipa, netm):
""" Write conf file """
whichnic = '/etc/sysconfig/network-scripts/ifcfg-' + nic
print whichnic
ip = "IPADDR=" + ipa
mask = "NETMASK=" + netm
print ip + " " + mask
properties(ip, mask).apply_to(whichnic)
以下跟踪
In [45]: writestaticConf('eth0','192.168.0.1', '255.255.255.0')
/etc/sysconfig/network-scripts/ifcfg-eth0
IPADDR=192.168.0.1 NETMASK=255.255.255.0
---------------------------------------------------------------------------
IOError Traceback (most recent call last)
<ipython-input-45-00917222fac3> in <module>()
----> 1 writestaticConf('eth0','192.168.0.1', '255.255.255.0')
<ipython-input-44-7af6f7537082> in writestaticConf(nic, ipa, netm)
6 mask = "NETMASK=" + netm
7 print ip + " " + mask
----> 8 properties(ip, mask).apply_to(whichnic)
9 #w.apply_to(whichnic)
/root/.virtualenvs/teknasportal/lib/python2.7/site-packages/confparse-1.0a1-py2.7.egg/confparse.py in __init__(self, _fileordict, _order, **kwargs)
123 if isinstance( _fileordict, str ) or isinstance( _fileordict, list):
124 self.template=_fileordict
--> 125 self.read( _fileordict )
126
127 elif hasattr( _fileordict, '__setitem__' ):
/root/.virtualenvs/teknasportal/lib/python2.7/site-packages/confparse-1.0a1-py2.7.egg/confparse.py in read(self, filenames)
309
310 if isinstance(filenames, basestring):
--> 311 self._read( file(filenames), filenames)
312 self.template=filenames
313
IOError: [Errno 2] No such file or directory: 'IPADDR=192.168.0.1'
答案 0 :(得分:2)
properties('IPADDR=192.168.0.1', 'NETMASK=255.255.255.0')
与
不同properties(IPADDR='192.168.0.1', NETMASK='255.255.255.0')
如果要使用关键字参数,请使用关键字参数!不要将'IPADDR='
添加到字符串本身。
properties(IPADDR=ip, NETMASK=mask)