Advertisement
Guest User

Untitled

a guest
May 16th, 2018
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.79 KB | None | 0 0
  1. import socket
  2. import string
  3. import paramiko
  4.  
  5.  
  6. class bcolors:
  7. HEADER = '\033[95m'
  8. OKBLUE = '\033[94m'
  9. OKGREEN = '\033[92m'
  10. WARNING = '\033[93m'
  11. FAIL = '\033[91m'
  12. ENDC = '\033[0m'
  13. BOLD = '\033[1m'
  14. UNDERLINE = '\033[4m'
  15.  
  16.  
  17. class Operation(object):
  18. server = None
  19.  
  20. def __init__(self):
  21. self.result = 0
  22. print "First run have fun"
  23.  
  24. def getfilename(self):
  25. nazwa = raw_input("nazwa pliku: ")
  26. return nazwa
  27.  
  28. def getserverinfo(self):
  29. server = raw_input("enter hostname: ")
  30. username = raw_input("enter username: ")
  31. password = raw_input("enter password: ")
  32. cmd = raw_input("enter command to run: ")
  33. return server, username, password, cmd
  34.  
  35. def openfile(self):
  36. nazwa = self.getfilename()
  37. try:
  38. with open(nazwa, 'r') as file:
  39. print 'You typed', nazwa
  40. for line in file:
  41. array = string.split(line.rstrip('\n'), )
  42. print "Server: ", array[0], " Username: ", array[1], " Password: ", array[2], " Commands to run: ", array[3]
  43. except ZeroDivisionError as ex:
  44. print bcolors.FAIL + "Input data error:", ex + bcolors.ENDC
  45. except IOError as ex:
  46. print bcolors.FAIL + "BLAD: Plik nie istnieje"+ bcolors.ENDC
  47. print "\n"
  48.  
  49. def add_server(self):
  50. serverinfo = self.getserverinfo()
  51. nazwa = self.getfilename()
  52. data = ' '.join(serverinfo)
  53. with open(nazwa, 'a') as save:
  54. save.write(data+'\n')
  55.  
  56. def search(self):
  57. nazwa = self.getfilename()
  58. szukaj = raw_input("Search: ")
  59. with open(nazwa, 'r') as search:
  60. searchlines = search.readlines()
  61. for i, line in enumerate(searchlines):
  62. if szukaj in line:
  63. for l in searchlines[i:i+1]: print l
  64. print
  65.  
  66. def connectiontest(self):
  67. nazwa = self.getfilename()
  68. try:
  69. with open(nazwa, 'r') as file:
  70. print 'You typed', nazwa
  71. for line in file:
  72. array = string.split(line.rstrip('\n'), )
  73. print "Server: ", array[0], " Username: ", array[1]
  74. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  75. port = 22
  76. try:
  77. s.connect((array[0], port))
  78. print(s.recv(1024))
  79. s.close
  80. except socket.gaierror:
  81. print bcolors.FAIL + "Cannot connect to: ", array[0] + bcolors.ENDC
  82. #print "Cannot connect to: ", array[0]
  83. pass
  84. except ZeroDivisionError as ex:
  85. print bcolors.FAIL + "Input data error:", ex + bcolors.ENDC
  86. except IOError as ex:
  87. print bcolors.FAIL + "BLAD: Plik nie istnieje" + bcolors.ENDC
  88. print "\n"
  89.  
  90. def runremotecommand(self):
  91. nazwa = self.getfilename()
  92. try:
  93. with open(nazwa, 'r') as file:
  94. print 'You typed', nazwa
  95. time_out = 20 # Number of seconds for timeout
  96. port = 22
  97. ssh = paramiko.SSHClient()
  98. ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  99.  
  100. for line in file:
  101. array = string.split(line.rstrip('\n'), )
  102. try:
  103. ssh.connect(array[0], port, array[1], array[2], timeout=time_out)
  104. print bcolors.OKGREEN + "Success!!" + bcolors.ENDC + " -- Server: ", array[0], " User: ", array[1]
  105. except paramiko.AuthenticationException:
  106. print bcolors.UNDERLINE + "Authentication problem" + bcolors.ENDC + " -- Server: ", array[0], " User: ", array[1]
  107. continue
  108. except socket.error, e:
  109. print bcolors.FAIL + "Comunication problem" + bcolors.ENDC + " -- Server: ", array[0], " User: ", array[1]
  110. continue
  111. stdin, stdout, stderr = ssh.exec_command(array[3])
  112. for line in stdout:
  113. print('... ' + line.strip('\n'))
  114. ssh.close()
  115.  
  116. #file.close()
  117.  
  118. except ZeroDivisionError as ex:
  119. print bcolors.FAIL + "Input data error:", ex + bcolors.ENDC
  120. except IOError as ex:
  121. print bcolors.FAIL + "BLAD: Plik nie istnieje" + bcolors.ENDC
  122. print "\n"
  123.  
  124. answer = ""
  125. op = Operation()
  126. while answer != "quit":
  127.  
  128. dic = {
  129. '\\': b'\xe2\x95\x9a',
  130. '-': b'\xe2\x95\x90',
  131. '/': b'\xe2\x95\x9d',
  132. '|': b'\xe2\x95\x91',
  133. '+': b'\xe2\x95\x94',
  134. '%': b'\xe2\x95\x97',
  135. }
  136.  
  137. def decode(x):
  138. return (''.join(dic.get(i, i.encode('utf-8')).decode('utf-8') for i in x))
  139. C = "\x1B["
  140. reset = C + "m"
  141.  
  142. print(decode(C+'6;30;47m' +'+-------------------------Type what You want--------------------------%' + C + '0m'))
  143. print(decode(C+'6;30;47m' +'|' + C + '0m'' ' + C + '6;30;47m' +'|' + C + '0m'))
  144. print(decode(C+'6;30;47m' +'|' + C + '0m'' ' + C + '6;30;41m'+'show' + C + '0m'' - shows all servers inside a config file of Your choose ' + C + '6;30;47m' +'|' + C + '0m'))
  145. print(decode(C+'6;30;47m' +'|' + C + '0m'' ' + C + '6;30;41m'+'add' + C + '0m'' - adds a server to config file of Your choose ' + C + '6;30;47m' +'|' + C + '0m'))
  146. print(decode(C+'6;30;47m' +'|' + C + '0m'' ' + C + '6;30;41m'+'test' + C + '0m'' - test connection to all server ' + C + '6;30;47m' +'|' + C + '0m'))
  147. print(decode(C + '6;30;47m' + '|' + C + '0m'' ' + C + '6;30;41m' + 'conn' + C + '0m'' - connect to server and run command ' + C + '6;30;47m' + '|' + C + '0m'))
  148. print(decode(C+'6;30;47m' +'|' + C + '0m'' ' + C + '6;30;41m'+'quit' + C + '0m'' - closes the program ' + C + '6;30;47m' +'|' + C + '0m'))
  149. print(decode(C+'6;30;47m' +'|' + C + '0m'' ' + C + '6;30;47m' +'|' + C + '0m'))
  150. print(decode(C+'6;30;47m' + '\\------------------------------------------------------------------------/' + C + '0m'))
  151.  
  152. answer = raw_input("Please select an operation: ")
  153. if answer.lower() in ("1", "show"):
  154. op.openfile()
  155. elif answer.lower() in ("2", "add"):
  156. op.add_server()
  157. elif answer.lower() in ("3", "search"):
  158. op.search()
  159. elif answer.lower() in ("4", "test"):
  160. op.connectiontest()
  161. elif answer.lower() in ("conn"):
  162. op.runremotecommand()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement