简体   繁体   English

Python脚本将ubuntu终端命令发送到列表

[英]Python Script send a ubuntu terminal command to a list

I have this command in Ubuntu: 我在Ubuntu中有以下命令:

bgpq3 -4 AS-YAHOO-JP-2 -m 24 -l Google

the output is a list of prefixes: 输出是前缀列表:

ip prefix-list Google permit 14.137.224.0/19 ip前缀列表Google许可14.137.224.0/19

ip prefix-list Google permit 27.111.76.0/22 ip prefix-list Google许可27.111.76.0/22

ip prefix-list Google permit 27.121.128.0/17 ip前缀列表Google许可27.121.128.0/17

ip prefix-list Google permit 27.133.224.0/22 ip前缀列表Google许可27.133.224.0/22

ip prefix-list Google permit 27.133.240.0/22 ip prefix-list Google许可27.133.240.0/22

ip prefix-list Google permit 27.133.240.0/24 ip prefix-list Google许可27.133.240.0/24

ip prefix-list Google permit 27.133.241.0/24 ip prefix-list Google许可27.133.241.0/24

ip prefix-list Google permit 27.133.242.0/24 ip前缀列表Google许可27.133.242.0/24

ip prefix-list Google permit 27.133.243.0/24 ip前缀列表Google许可27.133.243.0/24

how can i put the output to a list and use each line (as example send them to a database) I dont want to save them first to a file.txt 我如何将输出放入列表并使用每一行(例如将它们发送到数据库),我不想先将它们保存到file.txt

i am using this command but is does'nt help because the results are stored in a text file. 我正在使用此命令,但没有帮助,因为结果存储在文本文件中。

os.system("bgpq3 -4 AS-YAHOO-JP-2 -m 24 -l Google > list.txt")

Please help me figure this out!!! 请帮我弄清楚!!!

I think, you could use subprocess module. 我认为,您可以使用子流程模块。 This module replaces some older modules and functions. 该模块替代了一些较旧的模块和功能。 Official document is here: https://docs.python.org/3/library/subprocess.html#module-subprocess 官方文档在这里: https : //docs.python.org/3/library/subprocess.html#module-subprocess

import subprocess
res = subprocess.Popen('bgpq3 -4 AS-YAHOO-JP-2 -m 24 -l Google', shell=True,
                       universal_newlines=True,
                       stdout=subprocess.PIPE).communicate()[0]

for line in res.split('\n'):
    print(line)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM