امروز به معرفی اسکریپت تهیه بک آپ از تجهیزات سیسکو می پردازم. به عنوان یک مهندس شبکه حتما به یک ابزار برای تهیه بک آپ خودکار از پیکربندی های تجهیزات سیسکو نیاز پیدا کردید. ابزارهایی زیادی در این خصوص وجود دارد مثل Manage Engine Device Expert و یا Cisco Prime ولی نصب و راه اندازی این نرم افزارها قدری زمانبر و نیاز به خرید لایسنس یا کرک کردن برنامه دارد که پیشنهاد نمی شود.از طریق اسکریپت زیر می تونید فایل متنی شامل آدرس IP سوییچ ها ( یا روترها و حتی فایروال) را بخونید و پس از ارتباط SSH با تک تک تجهیزات، نسبت به تهیه بک آپ از تجهیز و انتقال به یک سرور TFTP اقدام کنید.
#----------------------------------------------------
# Backup Cisco Device By Python
# By : Hessam Hosseini
# Email : hessam.hosseyni@gmail.com
# Tel : 0912 0612 305
#----------------------------------------------------
#------------------------------------------------- Get User and Password of Switch -----------------------------------
username=input("\n\nUsername: ")
password=getpass(prompt='Password: ')
enable_pass=getpass(prompt='Enable Password: ')
#------------------------------------------------- Read Switch List from file -----------------------------------
with open('Switch_list.txt') as f:
devices_list = f.read().splitlines()
for device in devices_list:
DVIP = device
print (Style.DIM + Fore.WHITE + "\n-------------------------------------------------------")
print (Style.DIM + Fore.WHITE + "\nConnecting to " + DVIP + " ..." + Style.RESET_ALL)
#------------------------------------------------- SSH to Device -----------------------------------
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #IPv4 , TCP Connection
result = sock.connect_ex((DVIP,22))
if result != 0:
print (Style.DIM + Fore.YELLOW + " SSH Port on Switch " + DVIP + " is close." + Style.RESET_ALL)
continue
sock.close()
try:
remote_conn_pre=paramiko.SSHClient()
remote_conn_pre.set_missing_host_key_policy(paramiko.AutoAddPolicy())
remote_conn_pre.connect(DVIP,username=username,password=password,look_for_keys=False,allow_agent=False)
print(Style.DIM + Fore.CYAN + " SSH Connection to %s established." %DVIP + Style.RESET_ALL)
except Exception as e:
print(Style.BRIGHT + Fore.YELLOW + " SSH Connection to %s : Authentication Failed." %DVIP + Style.RESET_ALL)
continue
#------------------------------------------------- Configuration -----------------------------------
remote_conn.send('enable\n')
remote_conn.send(enable_pass+'\n')
remote_conn.send('configure terminal\n')
remote_conn.send('exit\n')
remote_conn.send('write\n')
print(Style.DIM + Fore.GREEN + " %s Configuration has been saved." %DVIP + Style.RESET_ALL)
#------------------------------------------------- Check the TFTP Server -----------------------------------
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) #IPv4 , UDP Connection
result = sock.connect_ex((tftp_server,69))
#print (result)
if result == 0:
print(Style.DIM + Fore.GREEN + " TFTP Server of %s is accessible from %s." %(tftp_server,DVIP) + Style.RESET_ALL)
sock.close()
else:
print(Style.DIM + Fore.GREEN + " TFTP Server of %s is not accessible from %s." %(tftp_server,DVIP) + Style.RESET_ALL)
continue
#------------------------------------------------- Backup to TFTP -----------------------------------
remote_conn.send('copy startup-config tftp:\n')
remote_conn.send(tftp_server+'\n')
remote_conn.send(DVIP+'---'+date+'\n')
time.sleep(6)
output = remote_conn.recv(65535)
if 'bytes copied in' in str(output):
print(Style.BRIGHT + Fore.RED + " %s Successfully Backed up." %DVIP + Style.RESET_ALL)
else:
print(Style.BRIGHT + Fore.RED + " %s Could not get Backed up." %DVIP + Style.RESET_ALL)
#------------------------------------------------- Close SSH -----------------------------------
remote_conn.close()
print(Style.DIM + Fore.CYAN + " SSH Connection to %s was closed." %DVIP + Style.RESET_ALL)
C:\>python cisco-backup.py
در این دوره آموزشی فرض بر این است که شما دوره مقدماتی پایتون رو پشت سر گذاشتید و با مفاهیم و Syntax زبان پایتون آشنایی دارید ولی باز هم چنانچه در مورد نحوه عملکرد اسکریپت و یا چگونگی اجرای آن ابهامی داشتید من در خدمت هستم.اگر در یادگیری سیسکو مشکل دارید ، به شما پیشنهاد می کنیم از طریق دوره های آموزش سیسکو حرفه ای سایت توسینسو و با دوره آموزش CCNA روتینگ و سویچینگ شروع کنید ، فراموش نکنید که پیشنیاز همه این دوره های آموزش شبکه ، دوره آموزش نتورک پلاس است ، فراموش نکنید توسینسو اولین و بهترین انتخاب شما در حوزه آموزش فناوری اطلاعات فارسی است.
کارشناس ارشد شبکه
کارشناس ارشد IT گرایش طراحی شبکه های کامپیوتری هستم. در حال حاضر به عنوان مدرس، طراح، مشاور و مجری امن سازی زیرساخت شبکه با سازمانها و دانشگاهها همکاری دارم.
زمان پاسخ گویی روز های شنبه الی چهارشنبه ساعت 9 الی 18
فقط به موضوعات مربوط به محصولات آموزشی و فروش پاسخ داده می شود