Advertisement
commandlinekid

telnet into nat box and turn on and off radio

May 16th, 2021
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. import getpass
  2. import telnetlib
  3. import sys, getopt
  4.  
  5. # https://docs.python.org/3/library/telnetlib.html
  6.  
  7. def main(argv):
  8. action = ''
  9. actionoption = ''
  10. usage_text="test.py -a <action> -o <actionoption>"
  11. try:
  12. opts, args = getopt.getopt(argv,"ha:o:",["action=","actionoption="]) # options that require an argument need a colon. Second set there are optional long-options
  13. except getopt.GetoptError:
  14. print(usage_text)
  15. sys.exit(2)
  16. for opt, arg in opts:
  17. if opt == '-h':
  18. print('test.py -i <inputfile> -o <outputfile>')
  19. sys.exit()
  20. elif opt in ("-a", "--action"):
  21. action = arg
  22. elif opt in ("-o", "--actionoption"):
  23. actionoption = arg
  24.  
  25. if action and actionoption:
  26. print('Action is ', action)
  27. print('Action Option is ', actionoption)
  28. actionproc(action,actionoption)
  29. else:
  30. print(usage_text)
  31.  
  32. def actionproc(action, actionoption):
  33.  
  34. if(action=="natradio"):
  35. # SYR1 router
  36. host_syrnatbox = "192.168.1.1"
  37. loginprompt_syrnatbox = "MyRouterWhateverHeader login: "
  38. passprompt_syrnatbox = "Password: "
  39. user_syrnatbox = "root"
  40. pass_syrnatbox = "XXXXXXXXXXXXXXXXXXX"
  41. # SYR1 router
  42.  
  43.  
  44. # Assign whichever specific credentials you want to the main variables
  45. HOST = host_syrnatbox
  46. #user = input("enter username: ")
  47. user = user_syrnatbox
  48. #password = getpass.getpass()
  49. password = pass_syrnatbox
  50.  
  51.  
  52. tn = telnetlib.Telnet(HOST)
  53.  
  54. # tn.read_until(b"login: ")
  55. tn.read_until(str.encode(loginprompt_syrnatbox))
  56. tn.write(user.encode('ascii') + b"\n")
  57. if password:
  58. tn.read_until(str.encode(passprompt_syrnatbox))
  59. tn.write(password.encode('ascii') + b"\n")
  60.  
  61. tn.write(b"ls\n")
  62. tn.write(" /usr/sbin/iwconfig ath0 txpower ".encode('ascii')+actionoption.encode('ascii') + b"\n")
  63. tn.write(b"exit\n")
  64.  
  65. print(tn.read_all().decode('ascii'))
  66.  
  67. if __name__ == "__main__":
  68. main(sys.argv[1:])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement