0

My program uses subprocess and optparse modules to read terminal output and print it.

While Python 2 (2.7.14+) result is fine, in Python 3(3.6.7) it's a single line, that I can't read.

I've tried to convert to list to string

print(str(ifconfig_result))

I've tried looping

for result in ifconfig_result :
     print(result)

to no avail.

This is my code

import subprocess    
import optparse

  def get_arguments():     
      parser = optparse.OptionParser()
      parser.add_option("-i","--interface",dest="interface",help="interface to change its MAC address")
      parser.add_option("-m","--mac",dest="new_mac",help="new MAC address")
      (options,arguments) = parser.parse_args() 
      if not options.interface:
             parser.error("[-] Plase specify an interface, use --help for more info.")
      elif not options.new_mac:
           parser.error("[-] Plase specify a mac, use --help for more info.")
      return options 
  def change_mac(interface, new_mac):
      print("[+] Changing MAC address for " + interface + " to "+ new_mac)
      subprocess.run("ifconfig "+interface+" down", shell=True)
      subprocess.run("ifconfig "+interface+" hw ether "+new_mac, shell=True)
      subprocess.run("ifconfig "+interface+" up",shell=True)
  options= get_arguments()

  ifconfig_result = subprocess.check_output(["ifconfig", options.interface])
  print(ifconfig_result)

Python2 Output:

eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.0.2.4  netmask 255.255.255.0  broadcast 10.0.2.255
        inet6 AAAAA   prefixlen 64  scopeid 0x20<link>
        ether AAAAA  txqueuelen 1000  (Ethernet)
        RX packets 12176  bytes 17869942 (17.0 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 3507  bytes 213850 (208.8 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

but my output is showing

b'eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500\n        inet 10.0.2.4  netmask 255.255.255.0  broadcast 10.0.2.255\n        inet6 AAAAA prefixlen 64  scopeid 0x20<link>\n        ether AAAAA  txqueuelen 1000  (Ethernet)\n        RX packets 12179  bytes 17870182 (17.0 MiB)\n        RX errors 0  dropped 0  overruns 0  frame 0\n        TX packets 3510  bytes 214090 (209.0 KiB)\n        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0\n\n'
gboffi
  • 22,939
  • 8
  • 54
  • 85
sentil
  • 13
  • 1
  • 7
  • The answer you are getting is in bytes. You need to convert it to string. https://stackoverflow.com/questions/606191/convert-bytes-to-a-string – Sumit S Chawla Jan 15 '19 at 06:54

1 Answers1

0

In python 3 you've got not just vanilla string, but a binary string which stores escape sequences, '\n' is stands for Line Feed. To work with this string as normal decode it. Minimal example:

>>> string = b"Hello \n World"
>>> print(string)
>>> b'Hello \n World'
>>> string = string.decode('ascii')  # sring decoding
>>> print(string)
    Hello 

    World
>>> 

Also take a look at this question: How to convert 'binary string' to normal string in Python3?

funnydman
  • 9,083
  • 4
  • 40
  • 55