title: Find IP and Gateway using Python Script
body: <p>I'm new to python, I have crated following small script, I just need to improve code if possible simple instead complicated.</p>
  
  <pre><code>import subprocess
  import StringIO
  import re
  
  ip_r_l=subprocess.Popen("ip r l",shell=True,stdout=subprocess.PIPE).communicate()[0]
  s = StringIO.StringIO(ip_r_l)
  
  for line in s:
          if "default" in line:
                  gw = re.search(r'\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b',line).group(0)
  
          if "src" in line:
                  ip = re.search(r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3} $',line).group(0)
                  ip = ip.rstrip()
  
  
  print "Gatway: %r" % gw
  print "IPaddr: %r" % ip
  </code></pre>