我正在尝试在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]它通过snmp获取数据,然后通过curl将数据发送到本地服务器。它在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我可以在DB中看到数据。但是来自cron:
0-55/5 * * * * /root/python/w.py我在logread中看到:
Dec 20 23:30:01 OpenWrt cron.err crond[1039]: USER root pid 16141 cmd /root/python/w.py但在DB中没有数据:(在httpd access.log中也没有数据:(为什么?
发布于 2010-12-21 06:45:13
是否snmpget或curl不在cron的路径中?
发布于 2010-12-21 16:48:33
我用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 ", detailhttps://stackoverflow.com/questions/4494716
复制相似问题