开发者

Python OpenWRT crontab

Im trying to run python script on OpenWrt box:

#!/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).commun开发者_运维知识库icate()[0]

It's geting data over snmp, then post data by curl to local server. Its working ok from 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. but from cron:

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

I see in 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?


Could it be that snmpget or curl are not in the path of cron?


I replace curl with python urllib request and now its working! :)

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜