简体   繁体   English

Python OpenWRT crontab

[英]Python OpenWRT crontab

Im trying to run python script on OpenWrt box: 我正在尝试在OpenWrt框上运行python脚本:

#!/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]

It's geting data over snmp, then post data by curl to local server. 它通过snmp获取数据,然后通过curl将数据发布到本地服务器。 Its working ok from shell: 从shell可以正常工作:

root@OpenWrt:~/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

I can see data in DB. 我可以在DB中看到数据。 but from cron: 但来自cron:

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

I see in logread: 我在logread中看到:

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

But no data in DB :( and nothing in httpd access.log :( why? 但是,DB :(中没有数据,而httpd access.log :(中却什么都没有?

snmpget或curl是否不在cron的路径中?

I replace curl with python urllib request and now its working! 我将curl替换为python urllib请求,现在可以正常工作了! :) :)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM