Guest User

Untitled

a guest
Feb 8th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. import telnetlib
  2. import time
  3. import getpass
  4. import argparse
  5.  
  6.  
  7. class PasswordPromptAction(argparse.Action):
  8. def __call__(self, parser, args, values, option_string):
  9. if values is None:
  10. values = getpass.getpass()
  11. setattr(args, self.dest, values)
  12.  
  13. parser = argparse.ArgumentParser()
  14.  
  15. parser.add_argument('-u', '--username', dest='username',
  16. help='Usuario para acesso via Telnet', default=getpass.getuser())
  17.  
  18. parser.add_argument('-p', '--password', dest='password',
  19. action=PasswordPromptAction, nargs='?', type=str, required=True)
  20.  
  21. parser.add_argument('-f', '--file', dest='file_host', default='hosts.txt',
  22. help='Arquivo de hosts, padrao "hosts.txt"')
  23.  
  24. parser.add_argument('-c', '--commands', dest='command_list', default='comandos.txt',
  25. help='Arquivo com comandos, padrao "comandos.txt"')
  26.  
  27. args = parser.parse_args()
  28.  
  29.  
  30. def get_file(file):
  31. try:
  32. with open(file, 'r') as f:
  33. line = [ line.strip() for line in f ]
  34. return line
  35. except IOError as err:
  36. print("Erro: {0}".format(err))
  37.  
  38. hostname = get_file(args.file_host)
  39. for host in hostname:
  40. print(host)
  41. conect = telnetlib.Telnet(host)
  42.  
  43. conect.read_until(b"username: ", 3)
  44. conect.write(args.username.encode('ascii') + b"\n")
  45. conect.read_until(b"password: ", 3)
  46. time.sleep(1)
  47. conect.write(args.password.encode('ascii') + b"\n")
  48.  
  49. comandos = get_file(args.command_list)
  50. for comando in comandos:
  51. conect.write((comando + '\r' + '\n').encode('ascii'))
  52. conect.write(("\n").encode('ascii'))
  53. time.sleep(2)
  54. conect.write(b" exit\n")
  55. print('Comandos executados.')
  56.  
  57. with open("output.txt", "a") as saida:
  58. print(conect.read_all().decode('ascii'), file=saida)
Add Comment
Please, Sign In to add comment