-
Notifications
You must be signed in to change notification settings - Fork 3
/
SSH CLI Command Automation.py
37 lines (27 loc) · 1.18 KB
/
SSH CLI Command Automation.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
# Paramiko is a Python implementation of the SSHv2 protocol, providing both client and server functionality
import paramiko
#Define the variables required to SSH into a client
ssh = paramiko.SSHClient()
#Auto accepts unknown keys - do not use if connecting to an untrusted machine
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
#Example ssh.conect('10.255.55.100', username='admin', password='Netapp1!'
ssh.connect('$host_ip', username='$username', password='$password')
#Example cli_commands = ['cluster show', 'network interface show']
cli_commands = ['$cli_command_1',
'$cli_command_2',
'$etc']
#Function to execute commands
def run_command(command):
#Set Standard In, Standard Output, and Standard Error for SSH to the exeucted command
stdin,stdout,stderr = ssh.exec_command(command)
print('*** ' + command + ' ***')
print('')
#Read and print all lines from command output
for line in iter(stdout.readline, ''):
print line.rstrip()
print('')
#Run through all commands in the cli_commands list and execute in the command function
for commands in cli_commands:
run_command(commands)
#Close the SSH Session
ssh.close()