I would like to capture some characters following a word in a string. For example,
Pinging 10.1.1.1 with 32 bytes of data:
Reply from 10.1.1.1: bytes=32 time=39ms TTL=253
Reply from 10.1.1.1: bytes=32 time=17ms TTL=253
Reply from 10.1.1.1: bytes=32 time=17ms TTL=253
Reply from 10.1.1.1: bytes=32 time=17ms TTL=253
Ping statistics for 10.1.1.1:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss), Approximate round trip times in milli-seconds:
Minimum = 17ms, Maximum = 39ms, Average = 22ms
I would like to get characters following time= but stopping the space before TTL for the first instance of time=
I know I can do a split to time= and get the characters which follow but I don't know how to have it stop before the TTL (the number could be more than 2 digits for instance, so just getting the 4 which follow isn't an option)
Perhaps regex would also be an option? I've seen something like (?:time=).* would get the first instance but, again, i'm uncertain how to specify it to stop after the ms.
Edit - Added final code now that it is working. Thanks for all the help!
import os
import subprocess
import re
#Define Target
hostname = raw_input("Enter IP: ")
#Run ping and return output to stdout.
#subprocess.Popen runs cmdline ping, pipes the output to stdout. .stdout.read() then reads that stream data and assigns it to the ping_response variable
ping_response = subprocess.Popen(["ping", hostname, "-n", '1'], stdout=subprocess.PIPE).stdout.read()
word = "Received = 1"
latency = 1
p = re.compile(ur'(?<=time)\S+')
x = re.findall(p, ping_response)
if word in ping_response:
print "Online with latency of "+x[0]
else:
print "Offline"