summaryrefslogtreecommitdiff
path: root/tests/fakessh/ssh
blob: 74ffe20c9145da22077e6a2b1eddde800b57c94c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env python
from __future__ import print_function

import getpass
import sys
import getopt
PY3 = (sys.version_info[0] >= 3)
if not PY3:
    input = raw_input

ssh_usage = "usage: ssh [-2qV] [-c cipher_spec] [-l login_name]\r\n" \
          + "           hostname"

cipher_valid_list = ['aes128-ctr', 'aes192-ctr', 'aes256-ctr', 'arcfour256', 'arcfour128', \
    'aes128-cbc','3des-cbc','blowfish-cbc','cast128-cbc','aes192-cbc', \
    'aes256-cbc','arcfour']

try:
    server = sys.argv[-1]
    if server == 'noserver':
        print('No route to host')
        sys.exit(1)

    elif len(sys.argv) < 2:
        print(ssh_usage)
        sys.exit(1)

    cipher = ''
    cipher_list = []
    fullCmdArguments = sys.argv
    argumentList = fullCmdArguments[1:]
    unixOptions = "2qVc:l"
    arguments, values = getopt.getopt(argumentList, unixOptions)
    for currentArgument, currentValue in arguments:
        if currentArgument in ("-2"):
            pass
        elif currentArgument in ("-V"):
            print("Mock SSH client version 0.2")
            sys.exit(1)
        elif currentArgument in ("-c"):
            cipher = currentValue
            cipher_list = cipher.split(",")
            for cipher_item in cipher_list:
                if cipher_item not in cipher_valid_list:
                    print("Unknown cipher type '" + str(cipher_item) + "'")
                    sys.exit(1)


except Exception as e:
    print(ssh_usage)
    print('error = ' + str(e))
    sys.exit(1)

print("Mock SSH client for tests. Do not enter real security info.")

pw = getpass.getpass('password:')
if pw != 's3cret':
    print('Permission denied!')
    sys.exit(1)

prompt = "$"
while True:
    cmd = input(prompt)
    if cmd.startswith('PS1='):
        prompt = eval(cmd[4:]).replace(r'\$', '$')
    elif cmd == 'ping':
        print('pong')
    elif cmd.startswith('ls'):
        print('file1.py', 'file2.html', sep='\t')
    elif cmd == 'echo $?':
        print(0)
    elif cmd in ('exit', 'logout'):
        print('Closed connection')
        break