Advertisement
Guest User

Untitled

a guest
Mar 8th, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.06 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. import sys
  5. import os
  6.  
  7. script_path = os.path.dirname(os.path.realpath(__file__))
  8. sys.path.insert(0, script_path+'/Lib/site-packages')
  9.  
  10. from pexpect import pxssh
  11.  
  12. # ------------------- START -------------------- #
  13. IP = ""
  14. PASS = ""
  15. PASS1 = "xxxxxx"
  16. PASS2 = "xxxxxx"
  17. PASS3 = "xxxxxx"
  18. PASS4 = "xxxxxx"
  19. OUTPUT = ""
  20. choice = ""
  21.  
  22. # Main definition - constants
  23. menu_actions  = {}  
  24.  
  25. # =======================
  26. #     MENUS FUNCTIONS
  27. # =======================
  28.  
  29. # Main menu
  30. def main_menu():
  31.     os.system('clear')
  32.     print "Result of last Query:"
  33.     print "--------------------"
  34.     print OUTPUT
  35.     print "\n"
  36.     print "Welcome to RAD Makro v0.1\n"
  37.     print "Please make a selection:"
  38.     print "1. Show Port Status"
  39.     print "2. Activate/Deactivate Ports"
  40.     print "\n0. Quit"
  41.     choice = raw_input(" >>  ")
  42.     exec_menu(choice)
  43.     return
  44.  
  45. # Execute menu
  46. def exec_menu(choice):
  47.     os.system('clear')
  48.     ch = choice.lower()
  49.     if ch == '':
  50.         menu_actions['main_menu']()
  51.     else:
  52.         try:
  53.             menu_actions[ch]()
  54.         except KeyError:
  55.             print "Invalid selection, please try again.\n"
  56.             menu_actions['main_menu']()
  57.     return
  58.  
  59.  
  60.  
  61.  
  62. # Menu 2
  63. def menu2():
  64.     print "Hello Menu 2 !\n"
  65.     print "9. Back"
  66.     print "0. Quit"
  67.     choice = raw_input(" >>  ")
  68.     exec_menu(choice)
  69.     return
  70.  
  71. # Back to main menu
  72. def back():
  73.     menu_actions['main_menu']()
  74.  
  75. # -------- Menu Tasks --------- #
  76.  
  77. # Show Port Status
  78. def shwPrt(SSH):
  79.     SSH.sendline("configure system clock domain 1 source 1")
  80.     SSH.sendline("show status")
  81.     SSH.prompt()
  82.     OUTPUT = SSH.before
  83.     exec_menu(choice)
  84.  
  85. def sshconnect (SSH):  
  86.     os.system('clear')
  87.     SSH = pxssh.pxssh()
  88.     USER = "su"
  89.    
  90.     while True:
  91.         try:
  92.             PASS = PASS1
  93.             print "Logging in...\n"
  94.             SSH.login(IP, USER, PASS, auto_prompt_reset=False)
  95.             break
  96.         except:
  97.             print "Trying another Password..."
  98.             PASS = PASS2
  99.             SSH.login(IP, USER, PASS, auto_prompt_reset=False)
  100.             print "The Password was %s. this should not be! Consider changing at as soon as possible!\n" % PASS
  101.             break
  102.    
  103.             try:
  104.                 PASS = PASS3
  105.                 print "Trying another Password..."
  106.                 SSH.login(IP, USER, PASS, auto_prompt_reset=False)
  107.                 print "The Password was %s. this should not be! Consider changing at as soon as possible!\n" % PASS
  108.                 break
  109.             except:
  110.                 PASS = PASS4
  111.                 print "Trying another Password. This is the last try!"
  112.                 SSH.login(IP, USER, PASS, auto_prompt_reset=False)
  113.                 print "The Password was %s. this should not be! Consider changing at as soon as possible!\n" % PASS
  114.                 break
  115.     SSH.PROMPT = ".*?(#) $"             #Alternative: "(#) $"          
  116.     #SSH.PROMPT = ".*?(#) $" # "(#) $"
  117.        
  118.         #ssh.sendline("configure system clock domain 1 source 1")
  119.         #ssh.prompt()
  120.         #ssh.sendline("show status")
  121.         #ssh.prompt()
  122.         #print("Commands executed")
  123.         #cmd_return = ssh.before
  124.         #print("Command returned: " + cmd_return)
  125.         #i = cmd_return.index('\n')
  126.         #print(cmd_return[i+i:])
  127.         #ssh.sendline("exit all")
  128.         #ssh.sendline("logout")
  129.         #ssh.logout()
  130.  
  131. # Exit program
  132. def exit():
  133.     sys.exit()
  134.  
  135. # =======================
  136. #    MENUS DEFINITIONS
  137. # =======================
  138.  
  139. # Menu definition
  140. menu_actions = {
  141.     'main_menu': main_menu,
  142.     '1': shwPrt(SSH),
  143.     '2': menu2,
  144.     '9': back,
  145.     '0': exit,
  146. }
  147.  
  148. # =======================
  149. #      MAIN PROGRAM
  150. # =======================
  151.  
  152. # Main Program
  153. os.system('clear')
  154. if __name__ == "__main__":
  155.    
  156.     # Validate IP and Launch Main Menu
  157.     print "Please enter the IP you want to connect to:"
  158.     IP = raw_input('>>  ')
  159.     a = IP.split('.')
  160.     if len(a) != 4:
  161.         print "Invalid IP address!\n"
  162.         sys.exit()
  163.     for x in a:
  164.         if not x.isdigit():
  165.             print "Invalid IP address!\n"
  166.             sys.exit()
  167.         i = int(x)
  168.         if i < 0 or i > 255:
  169.             print "Invalid IP address!\n"
  170.             sys.exit()
  171.    
  172.     print "Trying to reach host with IP %s..." % IP
  173.     HOST_UP = True if os.system("ping -c 3 " + IP) is 0 else False
  174.    
  175.     if HOST_UP == True:
  176.         sshconnect()
  177.         main_menu()
  178.     else:
  179.         print "Host with IP address %s is dead!" % IP
  180.         sys.exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement