-1

我已经制作了这个脚本来更改 eth0 接口的 ip。它在控制台上工作正常,但不能从 cron 工作。即使脚本的 cron 上没有错误。请指导我完成这个

#!/usr/bin/python
import subprocess
import os
ip=['192.168.X.X','192.168.X.X','192.168.X.X']
gateway='192.168.X.1'
netmask='225.225.225.0'


currentip=os.popen("ifconfig eth0 | grep 'inet addr' | cut -d: -f2 | awk '{ print $1}'").read()
currentip=currentip.strip()
print currentip
lip=len(ip)

for item in ip:
    index=ip.index(item)
    if item==currentip:

        if index==lip-1:        
            index=0
            newip=ip[index]
            subprocess.call(['ifconfig','eth0',newip,'netmask',netmask,'up'])
            subprocess.call(['route','add','default','gw',gateway])
        else:
            index=index+1
            newip=ip[index]
            print newip
            subprocess.call(['ifconfig','eth0',newip,'netmask',netmask,'up'])
            subprocess.call(['route','add','default','gw',gateway])

Cron 代码是

06 12 * * * cd /root/system/; python ip.py
4

1 回答 1

0

对我来说,这似乎是一个“绝对路径”问题。请使用文件的绝对路径和 Python 编译器

06 12 * * * /usr/bin/python /root/system/ip.py

于 2013-10-17T11:19:16.727 回答