-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathssh_client.py
More file actions
53 lines (45 loc) · 1.48 KB
/
Copy pathssh_client.py
File metadata and controls
53 lines (45 loc) · 1.48 KB
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
import time
import paramiko
from collections import defaultdict
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
debug = False
def debug_print(*args):
if not debug:
return
for arg in args:
print arg,
print '\n',
def get_ssh_output(channel):
result = ""
receive_chunk_length = 100
received_bytes = receive_chunk_length
chunk = ""
while not chunk.endswith(">"):
debug_print('chunk received: len = ', received_bytes)
debug_print('chunk:', chunk + '|')
chunk = channel.recv(receive_chunk_length)
received_bytes = len(chunk)
result = result + chunk
chunk = chunk.strip()
return result
def ssh_command(hostname, command, username, password, quirks=None):
ssh.connect(hostname, username=username, password=password, look_for_keys=False, allow_agent=False)
chan = ssh.invoke_shell()
prompt = get_ssh_output(chan)
if quirks == 'brocade':
chan.send('terminal length 0\n')
prompt = get_ssh_output(chan)
elif quirks == 'juniper':
chan.send('set cli complete-on-space off\n')
prompt = get_ssh_output(chan)
chan.send('set cli screen-length 0\n')
prompt = get_ssh_output(chan)
chan.send('set cli screen-width 0\n')
prompt = get_ssh_output(chan)
debug_print(prompt)
chan.send(command + '\n')
debug_print('command sent')
output = get_ssh_output(chan)
ssh.close()
return output