我想执行一个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
的反应,我认为你有一个向后的那样简单。如何将curl语句包含在bash脚本中? – dawg
你可以给我一个例子吗?因为卷曲,我只是想发布一条消息,这是执行我的bash命令的结果。 –
你可以直接从Python运行curl命令。只需选择您想要使用的胶水:Bash或Python。 curl不是一种编程语言。 – dawg