telnetopenerchild (2110B)
1 #!/usr/bin/env python3 2 # coding=utf-8 3 # 4 # Copy me if you can. 5 # by 20h 6 # 7 8 import sys 9 import os 10 import re 11 import getopt 12 import pexpect 13 from urllib.parse import urlparse 14 from subprocess import Popen 15 16 telnetcmd = "telnet" 17 #telnetscmd = "telnet-ssl" 18 telnetscmd = None 19 20 def usage(app): 21 app = os.path.basename(app) 22 sys.stderr.write("usage: %s [-dhp] URI\n" % (app)) 23 sys.exit(1) 24 25 def main(args): 26 global telnetcmd, telnetscmd 27 28 try: 29 opts, largs = getopt.getopt(args[1:], 'hdp') 30 except getopt.GetoptError as err: 31 print(str(err)) 32 usage(args[0]) 33 34 dodebug = False 35 dopretend = False 36 for o, a in opts: 37 if o == "-h": 38 usage(args[0]) 39 elif o == "-d": 40 dodebug = True 41 elif o == "-p": 42 dopretend = True 43 else: 44 assert False, "unhandled option" 45 46 cmd = telnetcmd 47 if len(largs) < 1: 48 return 1 49 50 uri = urlparse(" ".join(largs)) 51 user = None 52 password = None 53 port = "23" 54 host = uri.netloc 55 56 if "@" in uri.netloc: 57 (user, net) = uri.netloc.rsplit("@", 1) 58 if ":" in user: 59 (user, password) = user.split(":", 1) 60 else: 61 password = None 62 63 if ":" in net: 64 (host, port) = net.rsplit(":", 1) 65 else: 66 host = net 67 port = "23" 68 else: 69 if ":" in uri.netloc: 70 (host, port) = uri.netloc.rsplit(":", 1) 71 72 73 if host == None: 74 sys.stderr.write("No host given, bailing.\n") 75 return 1 76 77 if uri.scheme.startswith("telnets"): 78 if telnetscmd == None: 79 sys.stderr.write("No telnets command given.\n") 80 return 1 81 cmd = telnetscmd 82 83 if uri.scheme.endswith("6"): 84 cmd = "%s -6" % (cmd) 85 if uri.scheme.endswith("4"): 86 cmd = "%s -4" % (cmd) 87 88 if user != None: 89 cmd = "%s -l '%s'" % (cmd, user) 90 91 cmd = "%s '%s'" % (cmd, host) 92 if port != None: 93 cmd = "%s '%s'" % (cmd, port) 94 95 if dodebug == True: 96 print("Cmd: '%s'" % (cmd)) 97 98 99 p = Popen(cmd, shell=True, stdout=sys.stdout, stdin=sys.stdin, 100 stderr=sys.stderr) 101 p.wait() 102 """ 103 if dopretend == False: 104 child = pexpect.spawn(cmd) 105 if password != None: 106 child.expect('(?i)password') 107 child.sendline(password) 108 child.interact() 109 """ 110 111 input("Press Enter to close window.") 112 113 return 0 114 115 if __name__ == "__main__": 116 sys.exit(main(sys.argv)) 117