Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import socket
- import string
- import paramiko
- class bcolors:
- HEADER = '\033[95m'
- OKBLUE = '\033[94m'
- OKGREEN = '\033[92m'
- WARNING = '\033[93m'
- FAIL = '\033[91m'
- ENDC = '\033[0m'
- BOLD = '\033[1m'
- UNDERLINE = '\033[4m'
- class Operation(object):
- server = None
- def __init__(self):
- self.result = 0
- print "First run have fun"
- def getfilename(self):
- nazwa = raw_input("nazwa pliku: ")
- return nazwa
- def getserverinfo(self):
- server = raw_input("enter hostname: ")
- username = raw_input("enter username: ")
- password = raw_input("enter password: ")
- cmd = raw_input("enter command to run: ")
- return server, username, password, cmd
- def openfile(self):
- nazwa = self.getfilename()
- try:
- with open(nazwa, 'r') as file:
- print 'You typed', nazwa
- for line in file:
- array = string.split(line.rstrip('\n'), )
- print "Server: ", array[0], " Username: ", array[1], " Password: ", array[2], " Commands to run: ", array[3]
- except ZeroDivisionError as ex:
- print bcolors.FAIL + "Input data error:", ex + bcolors.ENDC
- except IOError as ex:
- print bcolors.FAIL + "BLAD: Plik nie istnieje"+ bcolors.ENDC
- print "\n"
- def add_server(self):
- serverinfo = self.getserverinfo()
- nazwa = self.getfilename()
- data = ' '.join(serverinfo)
- with open(nazwa, 'a') as save:
- save.write(data+'\n')
- def search(self):
- nazwa = self.getfilename()
- szukaj = raw_input("Search: ")
- with open(nazwa, 'r') as search:
- searchlines = search.readlines()
- for i, line in enumerate(searchlines):
- if szukaj in line:
- for l in searchlines[i:i+1]: print l
- print
- def connectiontest(self):
- nazwa = self.getfilename()
- try:
- with open(nazwa, 'r') as file:
- print 'You typed', nazwa
- for line in file:
- array = string.split(line.rstrip('\n'), )
- print "Server: ", array[0], " Username: ", array[1]
- s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- port = 22
- try:
- s.connect((array[0], port))
- print(s.recv(1024))
- s.close
- except socket.gaierror:
- print bcolors.FAIL + "Cannot connect to: ", array[0] + bcolors.ENDC
- #print "Cannot connect to: ", array[0]
- pass
- except ZeroDivisionError as ex:
- print bcolors.FAIL + "Input data error:", ex + bcolors.ENDC
- except IOError as ex:
- print bcolors.FAIL + "BLAD: Plik nie istnieje" + bcolors.ENDC
- print "\n"
- def runremotecommand(self):
- nazwa = self.getfilename()
- try:
- with open(nazwa, 'r') as file:
- print 'You typed', nazwa
- time_out = 20 # Number of seconds for timeout
- port = 22
- ssh = paramiko.SSHClient()
- ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
- for line in file:
- array = string.split(line.rstrip('\n'), )
- try:
- ssh.connect(array[0], port, array[1], array[2], timeout=time_out)
- print bcolors.OKGREEN + "Success!!" + bcolors.ENDC + " -- Server: ", array[0], " User: ", array[1]
- except paramiko.AuthenticationException:
- print bcolors.UNDERLINE + "Authentication problem" + bcolors.ENDC + " -- Server: ", array[0], " User: ", array[1]
- continue
- except socket.error, e:
- print bcolors.FAIL + "Comunication problem" + bcolors.ENDC + " -- Server: ", array[0], " User: ", array[1]
- continue
- stdin, stdout, stderr = ssh.exec_command(array[3])
- for line in stdout:
- print('... ' + line.strip('\n'))
- ssh.close()
- #file.close()
- except ZeroDivisionError as ex:
- print bcolors.FAIL + "Input data error:", ex + bcolors.ENDC
- except IOError as ex:
- print bcolors.FAIL + "BLAD: Plik nie istnieje" + bcolors.ENDC
- print "\n"
- answer = ""
- op = Operation()
- while answer != "quit":
- dic = {
- '\\': b'\xe2\x95\x9a',
- '-': b'\xe2\x95\x90',
- '/': b'\xe2\x95\x9d',
- '|': b'\xe2\x95\x91',
- '+': b'\xe2\x95\x94',
- '%': b'\xe2\x95\x97',
- }
- def decode(x):
- return (''.join(dic.get(i, i.encode('utf-8')).decode('utf-8') for i in x))
- C = "\x1B["
- reset = C + "m"
- print(decode(C+'6;30;47m' +'+-------------------------Type what You want--------------------------%' + C + '0m'))
- print(decode(C+'6;30;47m' +'|' + C + '0m'' ' + C + '6;30;47m' +'|' + C + '0m'))
- 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'))
- 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'))
- 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'))
- 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'))
- print(decode(C+'6;30;47m' +'|' + C + '0m'' ' + C + '6;30;41m'+'quit' + C + '0m'' - closes the program ' + C + '6;30;47m' +'|' + C + '0m'))
- print(decode(C+'6;30;47m' +'|' + C + '0m'' ' + C + '6;30;47m' +'|' + C + '0m'))
- print(decode(C+'6;30;47m' + '\\------------------------------------------------------------------------/' + C + '0m'))
- answer = raw_input("Please select an operation: ")
- if answer.lower() in ("1", "show"):
- op.openfile()
- elif answer.lower() in ("2", "add"):
- op.add_server()
- elif answer.lower() in ("3", "search"):
- op.search()
- elif answer.lower() in ("4", "test"):
- op.connectiontest()
- elif answer.lower() in ("conn"):
- op.runremotecommand()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement