Python将变量传递到Logstash [英] Python piping a variable into logstash

查看:390
本文介绍了Python将变量传递到Logstash的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用conf名称中的vars从python2.7调用logstash命令,我尝试运行的完整shell命令是:

I am trying to invoke a logstash command from python2.7 with vars in the conf names, the full shell command I am trying to run is:

cat test.txt | sudo /usr/share/logstash/bin/logstash -f test.conf 

这是我的Python代码:

and here is my Python code:

listname = test
b = '/tmp/%s.txt' % (listname)
c = '/tmp/%s.conf' % (listname)
first = subprocess.Popen(['/bin/echo', b], stdout=subprocess.PIPE)
Second = subprocess.Popen(['/usr/share/logstash/bin/logstash -f', c], stdin=first.stdout)

我在插入listname变量时遇到麻烦,如在代码中前面创建的一样,在调用logstash时也遇到麻烦.我应该使用shell/子进程来执行此操作还是有更好的方法?

I am having trouble inserting the listname variable as is created earlier in the code and I am also having trouble invoking logstash. Should I use the shell/subprocess to do this or is there a better way?

推荐答案

上面的代码有很多问题.主要问题可能是您的listname应该是一个字符串(如果test已经不包含一个字符串),并且第二个Popen调用的语法是错误的(每个参数应该是其自己的列表条目) ).

There are a number of issues with your code above. The main problems are likely that your listname should be a string (if test does not contain a string already) and that the syntax for your second Popen call is wrong (each argument should be a list entry of its own).

此外,如果您需要第二个过程来使用sudo,则还会遇到一些其他问题,具体取决于您是否需要添加密码.

Also, if you need the second process to use sudo there are some extra issues, depending on whether you need to add a password or not.

因此,根据如何处理这些问题,您的代码可能看起来像以下替代方法之一:

Thus, depending on how to handle these issues, your code may look like one of the below alternatives:

from subprocess import Popen, PIPE

listname = 'test'
b = '/tmp/{}.txt'.format(listname)
c = '/tmp/{}.conf'.format(listname)

first = Popen(['/bin/cat', b], stdout=PIPE)

# Alt 1, No sudo
second = Popen(['/usr/share/logstash/bin/logstash', '-f', c], stdin=first.stdout)

# Alt 2, sudo with no password
second = Popen(['sudo', '/usr/share/logstash/bin/logstash', '-f', c], stdin=first.stdout)

# Alt 3, add sudo password (most complex)

# Alt 3.1, store password in file (bad)
sudo_password = "clear_text_password"

#Alt 3.2, ask user for password:
from getpass import getpass, getuser
sudo_password = getpass("[sudo] password for {}: ".format(getuser()))

# Since we need to add password at stdin, we cannot directly pipe output from "first"
# instead we set stdin to PIPE, and write sudo password, followed by output from "first"
# sudo -kSp '' ensures that password is read from stdin without prompt being displayed
second = Popen(['sudo', '-kSp', '', '/usr/share/logstash/bin/logstash', '-f', c], stdin=PIPE)

second.stdin.write(sudo_password + '\n') # write password

# followed by output from "first".
for line in first.stdout:
    second.stdin.write(line)

second.stdin.close()

希望这将帮助您找到合适的解决方案.如果没有,请进一步说明您的问题.

Hopefully this will help you find a suitable solution. If not, please specify your issues further.

这篇关于Python将变量传递到Logstash的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆