Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. import subprocess
  2.  
  3. class Interpreter(object):
  4. def __init__(self, text):
  5. self.text = text
  6. self.tokens = []
  7.  
  8. def error(self):
  9. raise Exception('Error parsing input')
  10.  
  11. def expr(self):
  12. self.tokens = self.text.split(" ")
  13. #return self.tokens
  14.  
  15. if self.tokens[0] == "show":
  16. if self.tokens[1] == "ip" and self.tokens[2] == "route":
  17. f = open('/proc/net/route', 'r')
  18. result = f.read()
  19. return result
  20. if self.tokens[1] == "interfaces":
  21. result = subprocess.call(["ifconfig"])
  22. return result
  23. if self.tokens[1] == "interface":
  24. result = subprocess.call(["ifconfig", self.tokens[2]])
  25. return result
  26. return self.error
  27.  
  28.  
  29. def main():
  30. while True:
  31. try:
  32. text = raw_input('cisco> ')
  33. except EOFError:
  34. break
  35. if not text:
  36. continue
  37. interpreter = Interpreter(text)
  38. result = interpreter.expr()
  39. print(result)
  40.  
  41. if __name__ == '__main__':
  42. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement