-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_ssh_command.py
More file actions
54 lines (41 loc) · 1.37 KB
/
Copy pathrun_ssh_command.py
File metadata and controls
54 lines (41 loc) · 1.37 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
54
#add netmiko to the local_requirements.txt
from django.utils.text import slugify
from netmiko import ConnectHandler
from django.forms import PasswordInput
from dcim.models import Device, Interface
from ipam.models import IPAddress
from extras.scripts import *
class RunCommand(Script):
class Meta:
name = "Run command on Cisco Devices via SSH"
description = "Run command on Cisco Devices via SSH"
field_order = ['device', 'input_username', 'input_pass', 'input_command']
device = ObjectVar(
model=Device,
query_params={
'status' : 'active'
}
)
input_username = StringVar(
description="Enter username:"
)
input_pass = StringVar(
widget=PasswordInput
)
input_command = StringVar(
description="Enter the SSH command:"
)
def run(self, data, commit):
address = data["device"].primary_ip
device_ip = str(address.address.ip)
cisco1 = {
"device_type": "cisco_ios",
"host": device_ip,
"username": data['input_username'],
"password": data['input_pass'],
}
command = data['input_command']
with ConnectHandler(**cisco1) as net_connect:
output = net_connect.send_command(command)
self.log_success("View the information bellow")
return '' .join(output)