这就是我要用我的代码完成的工作:
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'
最佳答案
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)
关于python - confparse说找不到IP地址作为文件或目录,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24746830/