Python-如何执行shell cmd'last | grep“已登录”'使用子进程。Popen

问题描述 投票:1回答:1

我想在Python中执行此Shell命令并获得返回结果,但是以下代码无法获得正确的结果。

command = 'last | grep "logged in"'
p = subprocess.Popen(shlex.split(command), stdout=subprocess.PIPE)
sto, err = p.communicate()

正确的结果应该这样

root pts/0 218.17.185.24 Tue Dec 10 15:34 still logged in

但是我得到的是这个

wtmp begins Wed Nov 27 18:30:26 2019

谁对此问题有想法?首先谢谢!!

我想在Python中执行此shell命令并获得返回结果,但是以下代码无法获得正确的结果。命令='最后| grep“已登录”'p = subprocess.Popen(shlex.split(...

python shell
1个回答
1
投票

您是否尝试过仅使用os.system

© www.soinside.com 2019 - 2024. All rights reserved.