May 30, 2013

BlackBox office power manager command-line script


Hi,

I asked student to make command line interface to the http://www.black-box.at/en-at/fi/1574/12791/Power-Switch-Cabinet-NG/O2.

Device has ajax over http with digest auth interface.

I works just fine, but ...

Random testing on IP addresses is really horrible idea. Half of our platform was down this morning.



#! /usr/bin/python

import sys, os, re
import getopt
import shlex, subprocess


command = "curl -u \"admin:admin\" --noproxy 192.168.100.1 --digest http://192.168.100.1/user/control.cgi --data-binary CMD="

def output_print(command):
 args = shlex.split(command)        
        output, error = subprocess.Popen(args,stdout = subprocess.PIPE, stderr= subprocess.PIPE).communicate()

 match = re.findall( r'var data=(.*)\;', output, re.MULTILINE)
        status = None
        if match:
  try:
   true = 1
   false = 0 # eval context
                 status = eval(str(match[0]))
  except TypeError:
                        print ("Status eval error")
                        return 1
 [Name, p2, port_state] = status[1]
 print Name
        print "|--------------------------------------------"
        print "|  Port         | State             "
        print "|--------------------------------------------"

 for i in range(4):
  [Name, P0, State,P1,P2,P3] = port_state[i]
  print ">",Name, "          |", State
 return


def main(argv):
        global command
 if len(argv) == 0:
  output_print(command)


 elif len(argv) == 2:
                ports = ['1','2','3','4']
                switch = {'ON':0,'OFF':1}

  if not argv[0] in ports:
                        print argv[0] + " is not port number"
                        return -1

  if not argv[1].upper() in switch.keys():
                        print argv[1] + " switch nok"
                        return -1

                command = command + "0_%d_%s" % (int(argv[0]) -1, str(switch.get(argv[1].upper())))
               
  output_print(command)
 else:
  print "not correct number of arguments, should be two - Port number and new state on/off"


if __name__ == '__main__':
    main(sys.argv[1:])



No comments:

Post a Comment