Skip to content
Snippets Groups Projects
Commit aef14690 authored by rick.mollard's avatar rick.mollard
Browse files

Delete Untitled.ipynb

parent 86350cfe
Branches
No related tags found
No related merge requests found
%% Cell type:code id: tags:
``` python
import requests
import urllib3
import pprint
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
creds = {"username": "35021233", "password": "Overwatch20!!"}
policyrole = "wired_l2_shared_dur-3168-2"
# create sessions object
session = requests.session()
def get_logs(self):
# send post request to switch to login to the switch
login = session.post(f"https://{self}/rest/v1/login", data=creds, verify=False)
print(f"Login code from Switch: {login.status_code}")
# we use this cookies keep secure connection to the switch
# print(f"This is Cookie: {login.cookies}")
# after login we can request any rest-api request from switch
get_log = session.get(f"https://{self}/rest/v10.04/system/interfaces/1%2F1%2F2/port_access_clients?attributes=applied_role&depth=2")
up_down = "down"
# we use json format get response payload
if policyrole in f"{get_log.json()}":
print("Found!")
pprint.pprint(get_log.json())
else:
print("Not Found!")
headers = {
'accept': '*/*',
'Content-Type': 'application/json',
}
data = f'{{"stp_config":{{"admin_edge_port_enable":true,"bpdu_filter_enable":true,"bpdu_guard_enable":true}},"user_config":{{"admin":"{up_down}"}},"loop_protect_enable":true}}'
print(data)
response = session.put('https://10.171.0.34/rest/v10.04/system/interfaces/1%2F1%2F2', headers=headers, data=data)
# always there is limit of maximum simultanous login sesion any device support
logout = session.post(f"https://{self}/rest/v1/logout")
print(f"Logout Code from Switch:{logout.status_code}")
# data = get_log.json()
# print(f"Data from Jason : {data}")
print("Enter the Switch IP:")
ip_add = input()
get_logs(ip_add)
```
%% Output
Enter the Switch IP:
10.171.0.34
---------------------------------------------------------------------------
TimeoutError Traceback (most recent call last)
/usr/lib/python3/dist-packages/urllib3/connection.py in _new_conn(self)
158 try:
--> 159 conn = connection.create_connection(
160 (self._dns_host, self.port), self.timeout, **extra_kw
/usr/lib/python3/dist-packages/urllib3/util/connection.py in create_connection(address, timeout, source_address, socket_options)
83 if err is not None:
---> 84 raise err
85
/usr/lib/python3/dist-packages/urllib3/util/connection.py in create_connection(address, timeout, source_address, socket_options)
73 sock.bind(source_address)
---> 74 sock.connect(sa)
75 return sock
TimeoutError: [Errno 110] Connection timed out
During handling of the above exception, another exception occurred:
NewConnectionError Traceback (most recent call last)
/usr/lib/python3/dist-packages/urllib3/connectionpool.py in urlopen(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, body_pos, **response_kw)
664 # Make the request on the httplib connection object.
--> 665 httplib_response = self._make_request(
666 conn,
/usr/lib/python3/dist-packages/urllib3/connectionpool.py in _make_request(self, conn, method, url, timeout, chunked, **httplib_request_kw)
375 try:
--> 376 self._validate_conn(conn)
377 except (SocketTimeout, BaseSSLError) as e:
/usr/lib/python3/dist-packages/urllib3/connectionpool.py in _validate_conn(self, conn)
995 if not getattr(conn, "sock", None): # AppEngine might not have `.sock`
--> 996 conn.connect()
997
/usr/lib/python3/dist-packages/urllib3/connection.py in connect(self)
313 # Add certificate verification
--> 314 conn = self._new_conn()
315 hostname = self.host
/usr/lib/python3/dist-packages/urllib3/connection.py in _new_conn(self)
170 except SocketError as e:
--> 171 raise NewConnectionError(
172 self, "Failed to establish a new connection: %s" % e
NewConnectionError: <urllib3.connection.VerifiedHTTPSConnection object at 0x7fab87770820>: Failed to establish a new connection: [Errno 110] Connection timed out
During handling of the above exception, another exception occurred:
MaxRetryError Traceback (most recent call last)
/usr/lib/python3/dist-packages/requests/adapters.py in send(self, request, stream, timeout, verify, cert, proxies)
438 if not chunked:
--> 439 resp = conn.urlopen(
440 method=request.method,
/usr/lib/python3/dist-packages/urllib3/connectionpool.py in urlopen(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, body_pos, **response_kw)
718
--> 719 retries = retries.increment(
720 method, url, error=e, _pool=self, _stacktrace=sys.exc_info()[2]
/usr/lib/python3/dist-packages/urllib3/util/retry.py in increment(self, method, url, response, error, _pool, _stacktrace)
435 if new_retry.is_exhausted():
--> 436 raise MaxRetryError(_pool, url, error or ResponseError(cause))
437
MaxRetryError: HTTPSConnectionPool(host='10.171.0.34', port=443): Max retries exceeded with url: /rest/v1/login (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x7fab87770820>: Failed to establish a new connection: [Errno 110] Connection timed out'))
During handling of the above exception, another exception occurred:
ConnectionError Traceback (most recent call last)
/tmp/ipykernel_24880/1664368059.py in <module>
45 print("Enter the Switch IP:")
46 ip_add = input()
---> 47 get_logs(ip_add)
/tmp/ipykernel_24880/1664368059.py in get_logs(self)
12 def get_logs(self):
13 # send post request to switch to login to the switch
---> 14 login = session.post(f"https://{self}/rest/v1/login", data=creds, verify=False)
15 print(f"Login code from Switch: {login.status_code}")
16 # we use this cookies keep secure connection to the switch
/usr/lib/python3/dist-packages/requests/sessions.py in post(self, url, data, json, **kwargs)
579 """
580
--> 581 return self.request('POST', url, data=data, json=json, **kwargs)
582
583 def put(self, url, data=None, **kwargs):
/usr/lib/python3/dist-packages/requests/sessions.py in request(self, method, url, params, data, headers, cookies, files, auth, timeout, allow_redirects, proxies, hooks, stream, verify, cert, json)
531 }
532 send_kwargs.update(settings)
--> 533 resp = self.send(prep, **send_kwargs)
534
535 return resp
/usr/lib/python3/dist-packages/requests/sessions.py in send(self, request, **kwargs)
644
645 # Send the request
--> 646 r = adapter.send(request, **kwargs)
647
648 # Total elapsed time of the request (approximately)
/usr/lib/python3/dist-packages/requests/adapters.py in send(self, request, stream, timeout, verify, cert, proxies)
514 raise SSLError(e, request=request)
515
--> 516 raise ConnectionError(e, request=request)
517
518 except ClosedPoolError as e:
ConnectionError: HTTPSConnectionPool(host='10.171.0.34', port=443): Max retries exceeded with url: /rest/v1/login (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x7fab87770820>: Failed to establish a new connection: [Errno 110] Connection timed out'))
%% Cell type:code id: tags:
``` python
from netmiko import ConnectHandler
from getpass import getpass
from prettyprinter import pprint
net_connect = ConnectHandler(
device_type="aruba_procurve",
host="10.171.0.34",
username="35021233",
password="Overwatch20!!",
)
policyrole = "wired_l2_print_dur-3134-6"
output = net_connect.send_command("show port-access clients role wired_l2_print_dur-3134-6")
pprint(output)
if policyrole in output:
print("Found!")
else:
print("Not Found!")
print(output)
print(net_connect.find_prompt())
net_connect.disconnect()
```
%% Cell type:code id: tags:
``` python
```
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment