2010-12-20 75 views
1

我試着去上的OpenWrt箱運行python腳本:的Python的OpenWRT的crontab

#!/root/system/usr/bin/python 
import subprocess 

p = subprocess.Popen([r"snmpget","-v","1","-c","public","-Oqv","-Ln", "192.168.1.1","1.3.6.1.2.1.2.2.1.10.7"], stdout=subprocess.PIPE).communicate()[0] 
data = [r"curl","-d","iface_id=1&content="+ str(p).rstrip() ,"http://192.168.1.5:8080/stat/add_istat/"] 
a = subprocess.Popen(data, stdout=subprocess.PIPE).communicate()[0] 

它歌廳通過SNMP數據,然後通過捲曲本地服務器上傳數據。 其從殼確定工作:

[email protected]:~/python# ./w.py 
    % Total % Received % Xferd Average Speed Time Time  Time Current 
           Dload Upload Total Spent Left Speed 
    0 34 0  6 0 28  31 146 --:--:-- --:--:-- --:--:--  0 

我可以在DB看到的數據。 但是從cron:

0-55/5 * * * * /root/python/w.py 

我看到logread:在DB :(

Dec 20 23:30:01 OpenWrt cron.err crond[1039]: USER root pid 16141 cmd /root/python/w.py 

但沒有數據和沒有在的httpd access.log的:(爲什麼

回答

1

難道是? snmpget或curl是不是在cron的路徑中?

+0

有了完整路徑SNMPGET和捲曲不是工作壓力太大:( 隨着.SH腳本: 根@的OpenWrt:〜#貓add.sh /根/系統的/ usr/bin中/蟒蛇/根/蟒蛇/ W .py 也不能工作:( – 2010-12-21 07:13:09

0

我用python urllib請求替換curl並且現在它的工作!:)

url = 'http://192.168.1.5:8080/stat/add_istat/"' # write ur URL here 

values = {'iface_id' : '1', #write ur specific key/value pair 
      'content' : str(p).rstrip(), 
     } 
try: 
    data = urllib.urlencode(values) 
    req = urllib2.Request(url, data) 
    response = urllib2.urlopen(req) 
    the_page = response.read() 
    print the_page 
except Exception, detail: 
    print "Err ", detail