/
paramiko_test.py
49 lines (45 loc) · 1.56 KB
/
paramiko_test.py
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
import paramiko
from paramiko_expect import SSHClientInteraction
import sys
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
ssh.connect(hostname='hq-cucm-pub.abc.inc', username='administrator', password='ciscopsdt', timeout=30, look_for_keys=False)
except Exception as e:
pass
cli_responses = [{
'expect': 'Continue \(y/n\)\?',
'response': 'y',
'timeout': 5,
'newline': False
}]
cli_command = 'set cli session timeout 30'
try:
interact = SSHClientInteraction(ssh, display=False)
interact.expect('admin:')
interact.send(cli_command)
output = interact.current_output
if cli_responses is not None:
for index,item in enumerate(cli_responses):
try:
expect = item['expect']
response = item['response']
timeout = item.get('timeout')
newline = '\n' if item.get('newline', True) else ''
except KeyError as e:
print(f'Error: key {e} missing in cli_responses[{index}]')
sys.exit(1)
interact.expect(expect, timeout=timeout)
interact.send(response, newline=newline)
output += interact.current_output
interact.expect('admin:')
output += interact.current_output
tokens = output.splitlines()
tokens = tokens[tokens.index(f'admin:{cli_command}')+1:-1]
tokens[:]=[line.split() for line in tokens]
ssh.close()
except Exception as e:
print(f'Error executing CLI command: {e}')
sys.exit(1)
print(output)
print(tokens)