2017-08-08 192 views
0

我想执行一个bash命令作为我的curl语句的一部分。 bash命令假设执行一个显示一些消息的python文件。我该如何去实现这一目标?这里是我的尝试,但不能正常工作,并显示该命令的字符串,而不是实际执行bash命令的:在curl语句中运行bash命令

curl -X POST --data-urlencode 'payload={"channel": "#pxc-wsrep-test", "username": "webhookbot", "text": python test2.py, "icon_emoji": ":ghost:"}' https://hooks.slack.com/services/F065ZJS3N/B4JT9K2TM/5KWZRYFIGhLL0MimiHAW6Gbz 

下面是我的python脚本,test2.py,我要执行:

from subprocess import Popen, PIPE, STDOUT 

cmd = 'mysql -uroot -psecret -e"show status like \'wsrep_local_state_comment\'\G"' 
p = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True) 

output = p.stdout.read() 
print output 

所以我试图运行python脚本内卷曲,如下面所示: 从子进口POPEN,PIPE,STDOUT 导入请求

paylod={"text": 'mysql -uroot -psecret -e"show status like \'wsrep_local_state_comment\'\G"'} 
r = request.post('https://hooks.slack.com/services/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', data=payload) 
print(r.test) 
{ 
    "form": { 
     "test": 'mysql -uroot -psecret -e"show status like \'wsrep_local_state_comment\'\G"' 
    }, 
} 

但即使安装pip和请求后,我仍然收到以下错误。

Traceback (most recent call last): 
    File "test.py", line 5, in <module> 
    r = request.post('https://hooks.slack.com/services/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', data=payload) 
NameError: name 'request' is not defined 
+0

的反应,我认为你有一个向后的那样简单。如何将curl语句包含在bash脚本中? – dawg

+0

你可以给我一个例子吗?因为卷曲,我只是想发布一条消息,这是执行我的bash命令的结果。 –

+0

你可以直接从Python运行curl命令。只需选择您想要使用的胶水:Bash或Python。 curl不是一种编程语言。 – dawg

回答

-1

您可以使用反引号或$命令来运行另一个命令

例如

echo hi`echo bye` 

OR

echo hi$(echo bye) 
0

我不认为我们需要的东西太复杂这里很多。如果我们可以访问示例中的python脚本,我们可以使用requestspycurl来完成python中的工作。

它可以像使用POST方法与数据字典中提到here

编辑基于我们最新的留言谈话

import requests # make sure we have requests installed or else use urllib 
payload={"text": 'mysql -uroot -psecret -e"show status like \'wsrep_local_state_comment\'\G"'} 
r = requests.post('https://hooks.slack.com/services/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', data=payload) 
print(r.status_code) 
print(r.content) 
+0

因此,我尝试使用您的建议改变张贴上面,但似乎没有工作。 –

+0

您是否安装了请求?使用pip来安装请求或使用内置的urllib mofules。请求是您需要导入请求的模块。 – Addy