Simple SNMP bandwidth monitor

Allright I think I have this setup so that it works under my Ubuntu 17.10 + python venv installation:

bandwidth.php

sudo -s
apt-get install -y php-snmp snmp-mibs-downloader
cat << 'EOF' > /home/homeassistant/.homeassistant/bin/bandwidth.php
#!/usr/bin/php
<?php
  $router = "192.168.1.1";
  $interface = 2;
  $time = $argv[2];
  snmp_read_mib("/usr/share/snmp/mibs/ietf/IF-MIB");
  
  $A = (int) str_replace("Counter32: ", "", snmpget($router, "public", "if{$argv[1]}Octets.$interface"));
  sleep($time);
  $B = (int) str_replace("Counter32: ", "", snmpget($router, "public", "if{$argv[1]}Octets.$interface"));
  
  if ($A > $B) {
    print(round((((2**32 - $A) + $B) / $time) * 8 / 1024 / 1024, 2));
  } else {
    print(round((($B - $A) / $time) * 8 / 1024 / 1024, 2));
  }
?>
EOF
chmod +x /home/homeassistant/.homeassistant/bin/bandwidth.php
exit

configuration.yaml

sensor:
  - platform: command_line
    name: Internet Speed In
    command: '/home/homeassistant/.homeassistant/bin/bandwidth.php In 5'
    unit_of_measurement: mbps
    scan_interval: 10

  - platform: command_line
    name: Internet Speed Out
    command: '/home/homeassistant/.homeassistant/bin/bandwidth.php Out 5'
    unit_of_measurement: mbps
    scan_interval: 10