-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLab-Commands.rtf
173 lines (111 loc) · 3.62 KB
/
Lab-Commands.rtf
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
1. CRAWL
------------------------------------------------
SSH, Bash, the Guest Shell, and Python Scripting
------------------------------------------------
# Change directory (cd) to our workshop directory (if you are at Cisco Live)
cd /home/devnet/workshop/DEVWKS-2037
# Create and activate a virtual environment
python3 -m venv <CHOOSE A NAME>
source <NAME YOU CHOSE>/bin/activate
# Clone repo with files (not necessary if you are at Cisco Live)
git clone https://github.com/xanderstevenson/nxos-device-level-apis.git
cd nxos-device-level-apis
ls
# SSH into DevNet sandbox
yes
Admin_1234!
terminal length 0
show version
show hostname
show ip interface brief
# Run Bash
run bash
whoami
pwd
exit
# Run Guestshell
guestshell
dohost "show cdp global" "show ssh server"
exit
# Python interpreter
python
# Python code
# Retrieve the interface list in JSON format
from cli import *
import json
intflist = json.loads(clid('show interface brief'))
# Loop through the interfaces and print those that are 'up'
i = 0
while i < len(intflist['TABLE_interface']['ROW_interface']):
intf = intflist['TABLE_interface']['ROW_interface'][i]
# Check if 'state' key exists in the intf dictionary
if 'state' in intf and intf['state'] == 'up':
print(intf['interface'])
i += 1
exit()
exit
2. WALK
---------------------------
NETCONF, RESTCONF, and gRPC
---------------------------
# Install NCCLient and run code in Python interpreter (this step may not work at Cisco Live)
pip install ncclient
python
from ncclient import manager
nxv = manager.connect(host="131.226.217.151", port=830, username="admin", password="Admin_1234!", device_params={'name':'nexus'}, hostkey_verify=False)
print(nxv.get_config(source='running', filter="<filter><System xmlns='http://cisco.com/ns/yang/cisco-nx-os-device'></System></filter>").data_xml)
exit()
3. JOG
------------------------------------------------------------------
Using Ansible to Abstract Away SSH, Bash, and Guest Shell Commands
------------------------------------------------------------------
# Install paramiko and run Ansible playbook
pip install paramiko
ansible-playbook ansible_playbooks/nxos_commands.yml -i hosts
4. RUN
-------------------------------------------
NX-API: Device-level RESTful API Automation
-------------------------------------------
# SSH into device and check nxapi feature
Admin_1234!
show feature | include nxapi
exit
5. FLY
--------------------------------
Interact with NX-OS using Python
--------------------------------
# Install Requests and run Python file
pip install requests
python get_sys_info.py
# Add this to the end of python get_sys_info.py
# Define USR_URL and USR_PAYLOAD variables
USR_URL = "https://sbx-nxos-mgmt.cisco.com/api/node/mo/sys/userext.json"
USR_PAYLOAD = {
"aaaUser": {
"attributes": {
"pwdEncryptType": "0",
"name": "temp_user", # <--- Add your unique username here
"pwd": "temp_password",
"expires": "yes",
"expiration": "2030-07-31",
}
}
}
# Create a user with REST API's POST method
usr_create = session.post(USR_URL, json=USR_PAYLOAD)
# Check if creation was successful and if yes print such message
if usr_create.ok:
print("USER WAS SUCCESSFULLY CREATED!")
# Run Python file again to add User
python get_sys_info.py
# SSH into device to make sure User was added
Admin_1234!
terminal length 0
show user-account
exit
***If you care at Cisco Live, please deactivate and delete your virtual environment***
deactivate
rm -r <NAME YOU CHOSE>