| 1 |
#!/usr/bin/env python |
| 2 |
from __future__ import print_function |
| 3 |
|
| 4 |
import getpass |
| 5 |
import sys |
| 6 |
import getopt |
| 7 |
PY3 = (sys.version_info[0] >= 3) |
| 8 |
if not PY3: |
| 9 |
input = raw_input |
| 10 |
|
| 11 |
ssh_usage = "usage: ssh [-2qV] [-c cipher_spec] [-l login_name]\r\n" \ |
| 12 |
+ " hostname [shell]" |
| 13 |
|
| 14 |
cipher_valid_list = ['aes128-ctr', 'aes192-ctr', 'aes256-ctr', 'arcfour256', 'arcfour128', \ |
| 15 |
'aes128-cbc','3des-cbc','blowfish-cbc','cast128-cbc','aes192-cbc', \ |
| 16 |
'aes256-cbc','arcfour'] |
| 17 |
|
| 18 |
try: |
| 19 |
if len(sys.argv) < 2: |
| 20 |
print(ssh_usage) |
| 21 |
sys.exit(1) |
| 22 |
|
| 23 |
cipher = '' |
| 24 |
cipher_list = [] |
| 25 |
fullCmdArguments = sys.argv |
| 26 |
argumentList = fullCmdArguments[1:] |
| 27 |
unixOptions = "2qVc:l:" |
| 28 |
arguments, values = getopt.getopt(argumentList, unixOptions) |
| 29 |
for currentArgument, currentValue in arguments: |
| 30 |
if currentArgument in ("-2"): |
| 31 |
pass |
| 32 |
elif currentArgument in ("-V"): |
| 33 |
print("Mock SSH client version 0.2") |
| 34 |
sys.exit(1) |
| 35 |
elif currentArgument in ("-c"): |
| 36 |
cipher = currentValue |
| 37 |
cipher_list = cipher.split(",") |
| 38 |
for cipher_item in cipher_list: |
| 39 |
if cipher_item not in cipher_valid_list: |
| 40 |
print("Unknown cipher type '" + str(cipher_item) + "'") |
| 41 |
sys.exit(1) |
| 42 |
|
| 43 |
server = values[0] |
| 44 |
if server == 'noserver': |
| 45 |
print('No route to host') |
| 46 |
sys.exit(1) |
| 47 |
|
| 48 |
shell = 'bash' |
| 49 |
if len(values) > 1: |
| 50 |
shell = values[1] |
| 51 |
|
| 52 |
except Exception as e: |
| 53 |
print(ssh_usage) |
| 54 |
print('error = ' + str(e)) |
| 55 |
sys.exit(1) |
| 56 |
|
| 57 |
print("Mock SSH client for tests. Do not enter real security info.") |
| 58 |
|
| 59 |
pw = getpass.getpass('password:') |
| 60 |
if pw != 's3cret': |
| 61 |
print('Permission denied!') |
| 62 |
sys.exit(1) |
| 63 |
|
| 64 |
prompt = "$" |
| 65 |
while True: |
| 66 |
cmd = input(prompt) |
| 67 |
if cmd.startswith('PS1='): |
| 68 |
if shell == 'bash': |
| 69 |
prompt = eval(cmd[4:]).replace(r'\$', '$') |
| 70 |
elif shell == 'zsh': |
| 71 |
prompt = eval(cmd[4:]).replace('%(!.#.$)', '$') |
| 72 |
elif cmd.startswith('set prompt='): |
| 73 |
if shell.endswith('csh'): |
| 74 |
prompt = eval(cmd[11:]).replace(r'\$', '$') |
| 75 |
elif cmd == 'ping': |
| 76 |
print('pong') |
| 77 |
elif cmd.startswith('ls'): |
| 78 |
print('file1.py', 'file2.html', sep='\t') |
| 79 |
elif cmd == 'echo $?': |
| 80 |
print(0) |
| 81 |
elif cmd in ('exit', 'logout'): |
| 82 |
print('Closed connection') |
| 83 |
break |