Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
R
rm-pythonScripts
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
network-tools
rm-pythonScripts
Commits
29a5d087
Commit
29a5d087
authored
2 years ago
by
keitzmann
Browse files
Options
Downloads
Patches
Plain Diff
adding threading to findfails-mt.py
parent
9d66eae1
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
findfails-mt.py
+143
-0
143 additions, 0 deletions
findfails-mt.py
with
143 additions
and
0 deletions
findfails-mt.py
0 → 100644
+
143
−
0
View file @
29a5d087
from
netmiko
import
(
ConnectHandler
,
NetmikoTimeoutException
)
from
getpass
import
getpass
import
requests
import
urllib3
import
re
import
time
import
getpass
from
threading
import
Thread
,
currentThread
,
Lock
from
queue
import
Queue
import
datetime
urllib3
.
disable_warnings
(
urllib3
.
exceptions
.
InsecureRequestWarning
)
#
# This script will look to see which ports are in a "failed" state and try to reathenticate the port.
#
# 2023-04-10 - Added Multi-threading support. Adapted from example found at
# https://oznetnerd.com/2020/06/28/multithreading-with-python-netmiko/
###########################################################################
print
(
"
1 = UNL-City
\n
2 = UNL-East
\n
3 = UNO
\n
4 = UNK
\n
Select Campus:
"
)
campus
=
input
()
print
(
f
"
Campus Selected:
{
campus
}
"
)
print
(
"
----------------------------------
"
)
print
(
"
----------------------------------
"
)
print
(
"
1 = Find Failed ports
\n
Select Role:
"
)
role
=
input
()
if
campus
==
"
1
"
:
file
=
[
line
.
strip
()
for
line
in
open
(
"
unlCitySwitches-cx.txt
"
,
'
r
'
)]
if
campus
==
"
2
"
:
file
=
[
line
.
strip
()
for
line
in
open
(
"
unlEastSwitches-cx.txt
"
,
'
r
'
)]
if
campus
==
"
3
"
:
file
=
[
line
.
strip
()
for
line
in
open
(
"
unoSwitches-cx.txt
"
,
'
r
'
)]
if
campus
==
"
4
"
:
file
=
[
line
.
strip
()
for
line
in
open
(
"
unkSwitches-cx.txt
"
,
'
r
'
)]
#print(file[])
#file.close()
if
role
==
"
1
"
:
policyrole
=
"
Fail
"
##########################################################################
print
(
"
Enter Username:
"
)
Ausername
=
input
()
print
(
"
Enter Password:
"
)
Apassword
=
getpass
.
getpass
()
creds
=
{
"
username
"
:
{
Ausername
},
"
password
"
:
{
Apassword
}}
# Threading setup
# NUM_THREADS is number of concurrent connections you want.
# (You can experiment with values)
NUM_THREADS
=
20
PRINT_LOCK
=
Lock
()
def
main
():
start_time
=
datetime
.
datetime
.
now
()
device_queue
=
Queue
(
maxsize
=
0
)
# Loop through list of switch IP's, add to queue
for
selectIP
in
file
:
device_queue
.
put
(
selectIP
)
# Pass the Queue and length to MultiThread function
numdev
=
len
(
file
)
run_mt
(
mt_function
=
check_fail
,
q
=
device_queue
,
devs
=
numdev
)
# Show how long script took
print
(
"
\n
Elapsed time:
"
+
str
(
datetime
.
datetime
.
now
()
-
start_time
))
def
check_fail
(
q
,
kwargs
):
while
True
:
thread_name
=
currentThread
().
getName
()
if
q
.
empty
():
mt_print
(
f
"
{
thread_name
}
: Closing as there
'
s no jobs left in the queue.
"
)
return
ip_add
=
q
.
get
()
print
(
f
"
Connecting to ..
{
ip_add
}
"
)
try
:
net_connect
=
ConnectHandler
(
device_type
=
"
aruba_procurve
"
,
host
=
ip_add
,
username
=
Ausername
,
password
=
Apassword
,
)
###############################################################################
output
=
net_connect
.
send_command
(
f
"
show port-access clients
"
)
to1
=
output
.
split
(
"
\n
"
)
print
(
f
"
Ports that have role:
{
policyrole
}
:
"
)
for
to
in
to1
:
to
=
to
.
lstrip
()
#print(to)
if
policyrole
in
to
:
print
(
to
)
to3
=
to
.
split
(
"
"
)[
0
]
output2
=
net_connect
.
send_command
(
f
"
port-access reauthenticate interface
{
to3
}
"
)
print
(
f
"
{
to3
}
Reauthentication initiated.
"
)
#output1 = [f"interface {to3}","shut","no power"]
#net_connect.send_config_set(output1)
#time.sleep(1)
#output2 = [f"interface {to3}","no shut","power"]
#net_connect.send_config_set(output2)
to
=
to
.
split
(
"
"
)[
0
]
net_connect
.
disconnect
()
except
(
NetmikoTimeoutException
)
as
error
:
print
(
"
Switch is not responding----->
"
+
ip_add
)
print
(
"
Switch is not responding----->
"
+
ip_add
)
print
(
"
Switch is not responding----->
"
+
ip_add
)
q
.
task_done
()
# For helping prevent overlapping print statements
# between different queues.
def
mt_print
(
msg
):
with
PRINT_LOCK
:
print
(
msg
)
# Main function used for MultiThreading
def
run_mt
(
mt_function
,
q
,
**
kwargs
):
devs
=
kwargs
[
'
devs
'
]
num_threads
=
min
(
NUM_THREADS
,
devs
)
for
i
in
range
(
num_threads
):
thread_name
=
f
'
Thread-
{
i
}
'
worker
=
Thread
(
name
=
thread_name
,
target
=
mt_function
,
args
=
(
q
,
kwargs
))
worker
.
start
()
q
.
join
()
if
__name__
==
"
__main__
"
:
main
()
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment