parses the given rhostport variable, looking like this: [username[:password]@]host[:port] if only host is given, can be a hostname, IPv4/v6 address or a ssh alias from ~/.ssh/config and returns a tuple (username, password, port, host)
(rhostport)
| 31 | |
| 32 | |
| 33 | def parse_hostport(rhostport): |
| 34 | """ |
| 35 | parses the given rhostport variable, looking like this: |
| 36 | |
| 37 | [username[:password]@]host[:port] |
| 38 | |
| 39 | if only host is given, can be a hostname, IPv4/v6 address or a ssh alias |
| 40 | from ~/.ssh/config |
| 41 | |
| 42 | and returns a tuple (username, password, port, host) |
| 43 | """ |
| 44 | # leave use of default port to ssh command to prevent overwriting |
| 45 | # ports configured in ~/.ssh/config when no port is given |
| 46 | if rhostport is None or len(rhostport) == 0: |
| 47 | return None, None, None, None |
| 48 | port = None |
| 49 | username = None |
| 50 | password = None |
| 51 | host = rhostport |
| 52 | |
| 53 | if "@" in host: |
| 54 | # split username (and possible password) from the host[:port] |
| 55 | username, host = host.rsplit("@", 1) |
| 56 | # Fix #410 bad username error detect |
| 57 | if ":" in username: |
| 58 | # this will even allow for the username to be empty |
| 59 | username, password = username.split(":", 1) |
| 60 | |
| 61 | if ":" in host: |
| 62 | # IPv6 address and/or got a port specified |
| 63 | |
| 64 | # If it is an IPv6 address with port specification, |
| 65 | # then it will look like: [::1]:22 |
| 66 | |
| 67 | try: |
| 68 | # try to parse host as an IP address, |
| 69 | # if that works it is an IPv6 address |
| 70 | host = str(ipaddress.ip_address(host)) |
| 71 | except ValueError: |
| 72 | # if that fails parse as URL to get the port |
| 73 | parsed = urlparse('//{}'.format(host)) |
| 74 | try: |
| 75 | host = str(ipaddress.ip_address(parsed.hostname)) |
| 76 | except ValueError: |
| 77 | # else if both fails, we have a hostname with port |
| 78 | host = parsed.hostname |
| 79 | port = parsed.port |
| 80 | |
| 81 | if password is None or len(password) == 0: |
| 82 | password = None |
| 83 | |
| 84 | return username, password, port, host |
| 85 | |
| 86 | |
| 87 | def connect(ssh_cmd, rhostport, python, stderr, add_cmd_delimiter, remote_shell, options): |
no outgoing calls