0

if someone could help me with this little problem that I am having. Let me tell you: I'm trying to make a python script that extracts some values from the iptraf-ng command. But when I try to run it I get an error that I don't know what it means:

File "Main.py", line 7 subprocess.run(cmd.split(),check=True)
IndentationError: unexpected indent" 

This is the script that I am trying to implement, if anyone sees something in the code that can be corrected, I would appreciate it. This is the code:

import subprocess

def get_iptraf_values(ens160):
   cmd = "iptraf-ng -i ens160 -L /home/Aliexer/iptraf.log"
    subprocess.run(cmd.split(),check=True)

    with open('home/Aliexer/iptraf.log') as f:
        lines = f.readlines()

    values = {}
    for line in lines:
        if 'PktsIn' in line:
            values['PktsIn'] = int(line.split()[1])
        elif 'IP In' in line:
            values['IP In'] = int(line.split()[1])
        elif 'BytesIn' in line:
            values['BytesIn'] = int(line.split()[1])
        elif 'InRate' in line:
            values['InRate'] = float(line.split()[1])
        elif 'PktsOut' in line:
            values['PktsOut'] = int(line.split()[1])
        elif 'IP Out' in line:
            values['IP Out'] = int(line.split()[1])
        elif 'BytesOut' in line:
            values['BytesOut'] = int(line.split()[1])
        elif 'OutRate' in line:
            values['OutRate'] = float(line.split()[1])

    return values

values = get_iptraf_values('ens160')
print(values)

I expected it to print the values I asked for from the ens160 interface, but instead I got an error. PS: I'm new to this python thing, and I'd appreciate a hand.

Axe319
  • 4,255
  • 3
  • 15
  • 31
  • The `cmd = "iptraf-ng -i ens160 -L /home/Aliexer/iptraf.log"` line is indented 1 space less than it should be. It should line up with `subprocess.run(cmd.split(),check=True)`. – Axe319 Apr 11 '23 at 15:30
  • Does this answer your question? [I'm getting an IndentationError. How do I fix it?](https://stackoverflow.com/questions/45621722/im-getting-an-indentationerror-how-do-i-fix-it) – Axe319 Apr 11 '23 at 15:34

0 Answers0