在交换机上,我运行ntpq -nc rv
并获得输出:
associd = 0 status = 0715 leap_none,sync_ntp,1个事件,clock_sync, version =" ntpd 4.2.6p3-RC10@1.2239-o Mon Mar 21 02:53:48 UTC 2016(1)", processor =" x86_64",system =" Linux / 3.4.43.Ar-3052562.4155M",leap = 00, stratum = 2,precision = -21,rootdelay = 23.062,rootdisp = 46.473, REFID = 17.253.24.125, reftime = dbf98d39.76cf93ad Mon,Dec 12 2016 20:55:21.464, clock = dbf9943.026ea63c Mon,Dec 12 2016 21:28:03.009,peer = 43497, tc = 10,mintc = 3,偏移= -0.114,频率= 27.326,sys_jitter = 0.151, clk_jitter = 0.162,clk_wander = 0.028
我正在尝试使用Python的子进程模块创建一个bash shell命令,只提取上面例子中" offset"或-0.114
的值
我注意到我可以使用子进程替换mod或sh来实现:
import sh
print(sh.grep(sh.ntpq("-nc rv"), 'offset'))
我得到了:
mintc=3, offset=-0.114, frequency=27.326, sys_jitter=0.151,
这是不正确的,因为我只想要' offset', - 0.114的值。
不知道我在这里做错了什么,无论是我的grep功能还是我没有正确使用sh模块。
答案 0 :(得分:1)
[hpierce@localhost ~]$ ping www.google.com -4
PING www.google.com (172.217.0.4) 56(84) bytes of data.
64 bytes from ord38s04-in-f4.1e100.net (172.217.0.4): icmp_seq=1 ttl=54 time=18.9 ms
64 bytes from ord38s04-in-f4.1e100.net (172.217.0.4): icmp_seq=2 ttl=54 time=16.9 ms
^C
--- www.google.com ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1001ms
rtt min/avg/max/mdev = 16.958/17.939/18.921/0.990 ms
逐行阅读;它返回与输入的任何部分匹配的每一行。但我认为grep
有点矫枉过正。获得shell输出后,只需在grep
之后搜索该内容:
output