if else statement for netmiko - python

i want to write a python script that will check for variables in the switch configuration, if the variables are there, the script will not input them, but if the variables aren't there, the script will configure it. after the config, the script should print a CSV of all the commands attached to a particular ip.
from netmiko import ConnectHandler
from getpass import getpass
import readline
import time
import os
from datetime import datetime
from csv import reader
with open("/Users/ben/desktop/ntptest.csv", "r") as f:
devices_list = f.read().splitlines
for devices in devices_list():
print ('connecting to device' + devices)
ip = devices
cisco1 = {
"device_type": "cisco_ios",
"host": devices,
"username": "****",
"password": getpass(),
"port" : 22,
'verbose' : True
}
with ConnectHandler(**devices) as net_connect:
output = net_connect.send_command['sh ntp status', 'sh clock det', 'sh ntp ass']
net_connect.enable()
print("Connected Successfully")
P = '192.168.1.1'
S = '192.1.1.1.'
if output [1] == P and output [2] == S:
print ('Both Primary and Secondary NTP are present')
elif output [1] == None and output [2] == S:
output1 = net_connect.send_config_set('conf t' , 'ntp 192.168.9.9')
print(output, output1)
elif output [1] == P and output [2] == None:
output2 = net_connect.send_config_set('conf t', 'ntp server 192.168.9.9')
print(output, output2)
elif output [1] == None and output [2] == None:
output3 = net_connect.send_config_set('conf t', 'ntp server 192.1.1.1', 'ntp server 134.174.17.6')
print(output, output3)
else:
print('None of the condition exist')

Related

multithread pinging of IP address in Python

I have a list of IP addresses like 1000 no's. I am reading the ip_file.txt and storing the result file as result_date.txt. Below is the code that I achieved the result. But my issue is it's taking too long to execute the entire files. Can anyone suggest multithreading, please so that the desired result can be achieved quickly? Thanks in advance.
#!/usr/bin/env python
import os
import csv
import paramiko
from datetime import datetime
import time
import sys
import re
from collections import defaultdict
# Verifies your os type
from paramiko import file
OS_TYPE = os.name
# Sets the count modifier to the os type
count = '-n' if OS_TYPE == 'nt' else '-c'
def create_ip_list():
ip_list = []
with open("ip_file.txt", "r") as file:
for line in file:
ip_list.append(line.strip())
return ip_list
# fetching data
now = datetime.now()
dat = now.strftime("%d/%m/%Y")
# time = now.strftime("%H:%M:%S")
date_string = dat.replace('/', '-')
timestr = time.strftime("%d%m%Y-%H%M%S")
def ping_device(ip_list):
"""Ping ip_list and return results
return: None
rtype: None
"""
results_file = open("results_" + str(timestr) + ".txt", "w")
for ip in ip_list:
response = os.popen(f"ping {ip} {count} 1").read()
time.sleep(1.5)
#fetch Average time
print(response)
for i in response.split("\n"):
para = i.split("=")
try:
if para[0].strip() == "Minimum":
latency = para[3].strip()
print(latency)
# output1=latency[0:8].split(" ")
# test=output1[0]
# print(test)
except:
print("time run")
if "Received = 1" and "Approximate" in response:
#print(f"UP {ip} Ping Successful")
results_file.write(f"{ip},UP,{latency}" + "\n")
else:
print(f"Down {ip} Ping Unsuccessful")
results_file.write(f"{ip} Down" + "\n")
results_file.close()
if __name__ == "__main__":
ping_device(create_ip_list())
Write a function ping_one_device that takes a single ip and returns a single string giving the status. It should be easy to pull this out of ping_device.
Then
with open(results_file, "w") as results_file:
with ThreadPoolExecutor() as executor:
for result in map(ping_one_device, ip_list):
results_file.write(result)

Use Netmiko to loop through file and get switch ARP entries

I am trying to output the ARP table of a switch by using Netmiko. I would like to use a file containing IP addresses and then have Python/Netmiko run a "show arp" and then add the IP address from the file I give. I would like it to loop through the IP address file to show all ARP entries for the IP addresses in the file and then output to a file containing both IP and MAC addresses. Below is what I have for a single address any help would be greatly appreciated:
#!/usr/bin/env python3
#CF extract ARP table and send output as text file
from netmiko import ConnectHandler
from datetime import datetime
import time
import sys
##initializing device
device = {
'device_type': 'hp_comware',
'ip': '10.1.10.10',
'username': 'xxxx',
'password': 'xxxx',
}
start_time = datetime.now()
print (start_time)
net_connect = ConnectHandler(**device)
output = net_connect.send_command("dis arp 172.16.100.100")
time.sleep(2)
filename="test-arp.txt"
saveconfig=open(filename, 'w+')
saveconfig.write(output)
saveconfig.close()
time.sleep(2)
net_connect.disconnect()
end_time = datetime.now()
print (end_time)
With the code below, you can perform the operation very quickly on 100 devices at the same time (you can increase it if you want) in accordance with the promt of the device.
from netmiko import Netmiko
from multiprocessing.dummy import Pool as ThreadPool
import time
f_2 = open("multiple_device_list_cisco.txt","r") # You should open a notepad with this name and add all the IPs one under the other.
multiple_device_list = f_2.readlines()
file1 = open("Result.txt", "a") # this will be your automatic output when the code is finished
def _ssh_(nodeip):
try:
hp = {
'device_type': 'hp_comware', 'ip': nodeip, 'username':
xxxx, 'password': xxxx, 'secret':xxxx, "conn_timeout": 20}
hp_connect = Netmiko(**hp)
print(nodeip.strip() + " " + "is reachable")
except Exception as e:
print (e)
f_3.write(nodeip.strip() + "\n")
return
prompt_hp_fnk = hp_connect.find_prompt()
hostname_fnk = prompt_hp_fnk.strip("#") # Here you should put whatever the prompt of your HP device is
print(hostname_fnk)
output = hp_connect.send_command_timing("dis arp "+ nodeip)
file1.write(nodeip +" "+ output+ "\n")
hp_connect.disconnect()
myPool = ThreadPool(100) # you can increase or decrease this value
result = myPool.map(_ssh_,multiple_device_list)
I wrote the necessary changes to the above code. I hope that will be useful

How can I make my current code use Multithreading/concurrent.futures?

I am new to netmiko/Python scripting , Using online examples was able to make a script to take configuration backup. The backup is copied to the text file and output is saved.
Currently this backup is done sequentially and it does not connect to all device at once and take the backup. I want to connect to all the devices concurrently.
I understand multithreading or concurrent.futures can solve this issueenter code here but I was not able to do it so far.
Can anyone please suggest, how my existing code can be modified to achieve it. Below is the code.
from netmiko import ConnectHandler
from netmiko.ssh_exception import NetMikoTimeoutException
from paramiko.ssh_exception import SSHException
from netmiko.ssh_exception import AuthenticationException
import getpass
import sys
import time
import os
from datetime import datetime
##getting system date
day=time.strftime('%d')
month=time.strftime('%m')
year=time.strftime('%Y')
today=day+"-"+month+"-"+year
enter code here
##initialising device
device = {
'device_type': 'cisco_ios',
'ip': '192.168.100.21',
'username': 'Cisco',
'password': 'Cisco',
'secret':'Cisco',
'session_log': 'log.txt'
}
##opening IP file
ipfile=open("iplist.txt")
print ("Script to take backup of devices, Please enter your credential")
device['username']=input("username ")
device['password']=getpass.getpass()
print("Enter enable password: ")
device['secret']=getpass.getpass()enter code here
##taking backup
for line in ipfile:
try:
device['ip']=line.strip("\n")
print("\n\nConnecting Device ",line)
net_connect = ConnectHandler(**device)
net_connect.enable()
time.sleep(1)
with open('config.txt') as f:
cmd = f.read().splitlines()
print ("Reading the running config ")
output = net_connect.send_config_set(cmd)
output4 = "Failed"
time.sleep(7)
filename=device['ip']+'-'+today+".txt"
folder = os.path.join(today)
file = os.path.join(folder,filename)
os.makedirs(folder,exist_ok=True)
saveconfig=open(file,'w+')
print("Writing Configuration to file")
saveconfig.write(output)
saveconfig.close()
time.sleep(10)
net_connect.disconnect()
print ("Configuration saved to file",filename)
except:
print ("Access to "+device['ip']+" failed,backup did not taken")
output4 = "Failed"
file= device['ip']+'-'+today+"Error"+".txt"
config=open(file,'w+')
config.write(output4)
config.close()
ipfile.close()
print ("\nAll device backup completed")enter code here
You can refer below script and modify it according to your requirement. Here I have used python multiprocessing for connecting devices in pools.
#This script will allow for user pick hosts and enter show commands interactively
#
#Enable Multiprocessing
from multiprocessing import Pool
#
#getpass will not display password
from getpass import getpass
#ConnectionHandler is the function used by netmiko to connect to devices
from netmiko import ConnectHandler
#Time tracker
from time import time
#create variables for username and password
#create variables for configs and hosts
uname = input("Username: ")
passwd = getpass("Password: ")
cmd = input("Enter show commands seperated by ',': ")
host = input("Enter the host IPs seperate with space: ")
#This will put hosts and commands entered into list format
hosts = host.split()
cmds = cmd.split(",")
starting_time = time()
#Each member of the pool of 5 will be run through this function
def run_script(host_ip):
ios_rtr = {
"device_type": "cisco_ios",
"ip": host_ip,
"username": uname,
"password": passwd,
}
#connect to the device via ssh
net_connect = ConnectHandler(**ios_rtr)
#print the device IP or Hostname
print("Connected to host:", host_ip)
#this for loop is used to iterate through the show commands
for show_commands in cmds:
output = net_connect.send_command(show_commands)
print("Connected to host:", host_ip)
print(output)
print('\n---- Elapsed time=', time()-starting_time)
if __name__ == "__main__":
# Pool(5) means 5 process will be run at a time, more hosts will go in the next group
with Pool(5) as p:
print(p.map(run_script, hosts))
#This is the key to sending show commands vs config commands
#show commands --> net_connect.send_command()
#config commmands --> net_connect.send_config_set()
With this code, you can run commands on many devices at the same time. You can also use it by hiding your identity information with user_pass. There is also a device prompt discovery feature.
with open("user_pass.txt", "r") as f5:
user_pass = f5.readlines()
for list_user_pass in user_pass:
if "username" in list_user_pass:
username = list_user_pass.split(":")[1].strip()
if "password" in list_user_pass:
password = list_user_pass.split(":")[1].strip()
def _ssh_(nodeip):
try:
access_mpls = {
'device_type': 'huawei_olt', 'ip': nodeip, 'username':
username, 'password': password, }
net_connect = Netmiko(**access_mpls)
print(nodeip.strip() + " " + "success enter")
except Exception as e:
print(e)
f_3.write(nodeip.strip() + "\n")
return
prompt_gpon_fnk = net_connect.find_prompt()
hostname_fnk = prompt_gpon_fnk.strip("<" + ">")
print(hostname_fnk)
net_connect.send_command_timing("enable")
net_connect.send_command_timing("undo smart")
output = net_connect.send_command_timing("config")
print("config moda girildi")
net_connect.send_command_timing("acl 2010 ")
net_connect.send_command_timing("quit")
net_connect.send_command_timing("save")
print("config done")
with open("MDU_OK_2.txt", "a") as f:
f.write(nodeip + "\n")
f.close()
net_connect.disconnect()
f_2 = open("ip_list_2.txt", "r") ip_list = f_2.readlines()
f_2.close() f_3 = open("ssh_unsuccess_2.txt", "w")
myPool = ThreadPool(100) result = myPool.map(ssh, ip_list)

Python | Netmiko | Auto ping

im beginner and i have tried for a lot
code :
conn = netmiko.ConnectHandler(ip='10.254.60.10', device_type='cisco_ios',
username='user', password='P#ssw0rd')
print (conn.send_command('show interface Ethernet0/0 | i line|Des|Int'))
output like this
Ethernet0/0 is up, line protocol is up
Description: CUSTOMER A
Internet address is 10.254.60.69/30
how to auto ping to IP PtP using conn.send_command() based on result of show interface command?
example ping to 10.254.60.70
You get text
text = '''Ethernet0/0 is up, line protocol is up Description: CUSTOMER A
Internet address is 10.254.60.70/30'''
and you can get IP/MASK using string functions
address = text.split(' ')[-1]
print(address) # 10.254.60.70/30
and then you can use standard module ipaddress
import ipaddress
net = ipaddress.ip_interface(address)
ip = str(net.network.broadcast_address)
print( ip ) # 10.254.60.71
or not standard module netaddr
import netaddr
net = netaddr.IPNetwork(address)
ip = str(net.broadcast)
print( ip ) # 10.254.60.71
EDIT: Minimal working code
text = '''Ethernet0/0 is up, line protocol is up Description: CUSTOMER A
Internet address is 10.254.60.69/30'''
address = text.split(' ')[-1]
print(address) # 10.254.60.69/30
print('\n--- ipaddress ---\n')
import ipaddress
net = ipaddress.ip_interface(address)
print('ip :', net.ip ) # 10.254.60.69
print('ip+1:', net.ip+1 ) # 10.254.60.70
print('ip-1:', net.ip-1 ) # 10.254.60.68
#bip = net.network.broadcast_address
bip = str(net.network.broadcast_address)
print('bip :', bip ) # 10.254.60.71
print('\n--- netaddr ---\n')
import netaddr
net = netaddr.IPNetwork(address)
print('ip :', net.ip ) # 10.254.60.69
print('ip+1:', net.ip+1 ) # 10.254.60.70
print('ip-1:', net.ip-1 ) # 10.254.60.68
bip = net.broadcast
#bip = str(net.broadcast)
print('bip :', bip ) # 10.254.60.71
Result:
10.254.60.69/30
--- ipaddress ---
ip : 10.254.60.69
ip+1: 10.254.60.70
ip-1: 10.254.60.68
bip : 10.254.60.71
--- netaddr ---
ip : 10.254.60.69
ip+1: 10.254.60.70
ip-1: 10.254.60.68
bip : 10.254.60.71
This could be your sample and so easy minimal code with Netmiko :
from netmiko import ConnectHandler
cisco_Router = {
"device_type": "cisco_ios",
"host": "your_router_ip",
"username": "your_username",
"password": "your_password"}
with ConnectHandler(**cisco_Router) as net_connect:
result = net_connect.send_command("ping 4.2.2.4")
net_connect.disconnect()
print(result)
I haven't write it for netmiko yet, but I often use this code for paramiko.
import threading
from ping3 import ping
from queue import Queue
from ipaddress import ip_network, ip_address
import paramiko
import time
from termcolor import colored
import sys
import os
import subprocess
file1 = open('PING_OK.txt', 'w')
file2 = open('PING_NOK.txt', 'w')
hosts=[]
f1 = open('hostfile.txt', 'r')
devices= f1.readlines()
#print(devices)
for i in devices:
i = i.split()
hosts.append(i[0])
hosts_live = []
q = Queue()
for host in hosts:
q.put(host)
enter = "\r\n"
def ping2(ip_address):
from pythonping import ping
output = ping(ip_address, verbose=True)
output_list =output.rtt_avg_ms
print(output_list)
if output_list == 2000:
print("erişim yok"+ip_address)
file2.write(ip_address+"\n")
else:
print ("erişim var"+ip_address)
file1.write(ip_address + "\n")
def worker():
while True:
host = q.get()
ping2(host)
time.sleep(2)
q.task_done()
for i in range(1):#aynı anda bağlantı 15 ten fazla girilmemeli #
t = threading.Thread(target=worker)
t.deamon = True
t.start()
q.join()
file1.close()
file2.close()

python ftp make a callback to a server function from client

I'm creating a simple ftp server / client using pyftpdlib and ftplib in order to receive data from a remote computer. I used a ThrottledHandler in order to limit the bandwidth, and I need to change the limit from time to time. The thing is, when I deploy the server to remote, I won't be able to reach it easily, to change the bandwidth limit or stopping / starting the server. So here is my question:
Is there a way to make a callback to a server function that changes the bandwidth limit from the client side, while the server is up and running?
Note: The code is working without any problems right now.
server code:
import os
from pyftpdlib.authorizers import DummyAuthorizer
from pyftpdlib.handlers import FTPHandler
from pyftpdlib.handlers import DTPHandler
from pyftpdlib.handlers import ThrottledDTPHandler
from pyftpdlib.servers import FTPServer
authorizer = DummyAuthorizer()
authorizer.add_user("name", "password", "home_dir", perm="elr",
msg_login="name has logged in.",
msg_quit="name has left the server."
)
throttled_handler = ThrottledDTPHandler
throttled_handler.read_limit = 1024 * 1024 # kbps
throttled_handler.write_limit = 1024 * 1024 # kbps
handler = FTPHandler
handler.use_gmt_times = False
handler.authorizer = authorizer
handler.dtp_handler = throttled_handler
server = FTPServer((IP, PORT), handler)
server.serve_forever()
and the client code:
from ftplib import FTP
import os
import datetime
output_folder = "destination_dir"
# Option 1
def listFiles():
print("\n Files in current directory:")
file_list = ftp.nlst()
print(file_list)
# Option 2
def moveInto(folder_name):
if folder_name != "..":
if not folder_name in ftp.nlst():
print("This folder does not exist.")
return
ftp.cwd(folder_name)
return
# Option 3 and 4
def copyAll(copy_path=output_folder):
file_list = []
ftp.retrlines("LIST", callback=file_list.append)
for item in file_list:
# Following lines parse the item details.
file_parts = list(filter(lambda x: x != "", item.split(" ")))
file_name = file_parts[-1]
file_date = file_parts[-4:-1]
if not "." in file_name:
new_path = os.path.join(copy_path, file_name)
ftp.cwd(file_name)
if not os.path.exists(new_path):
os.mkdir(new_path)
print("Folder named {} has been created.".format(file_name))
copyAll(new_path)
else:
new_file_path = os.path.join(copy_path, file_name)
if not os.path.exists(new_file_path):
new_file = open(os.path.join(copy_path, file_name), "wb")
ftp.retrbinary("RETR {}".format(file_name), new_file.write)
new_file.close()
correctModifiedDate(os.path.join(copy_path, file_name), file_date)
print("File named {} has been copied.".format(file_name))
if ftp.pwd() != "/":
ftp.cwd("..")
return
def correctModifiedDate(file_path, correct_date):
# TODO: implement the correction of last modified date.
# print(correct_date)
pass
ftp = FTP("")
ftp.connect(IP, PORT)
ftp.login("name", "password")
print("You have connected to the ftp server.")
while True:
print("\ncommands:")
choice = input("1. List files\n"
"2. Move into\n"
"3. Copy all current folder structure\n"
"4. Continuously copy all current folder structure\n"
"5. Quit "
)
if str(choice) == "1":
listFiles()
elif str(choice) == "2":
folder = input("folder name: ")
moveInto(folder)
elif str(choice) == "3":
copyAll()
elif str(choice) == "4":
while True:
copyAll()
elif str(choice) == "5":
exit_choice = input("\nAre you sure you want to leave the server? Y/N ")
if exit_choice.upper() == "Y":
break
else:
print("You have entered an invalid choice...")

Categories

Resources