Automation for Experts: EEM, Python & NETCONF/RESTCONF
CCIE Enterprise Infrastructure EVE-NG / GNS3
مقصدObjectiveObjective
اس سبق کے بعد آپ EEM applets لکھ سکیں گے، Python سے device output پڑھ سکیں گے، اور NETCONF/RESTCONF XML/JSON examples کے ساتھ سمجھ جائیں گے۔Is lesson ke baad aap EEM applets likh sakenge, Python se device output parh sakenge, aur NETCONF/RESTCONF XML/JSON examples ke saath samajh jayenge.After this lesson you will be able to write EEM applets, read device output with Python, and understand NETCONF/RESTCONF with XML/JSON examples.
آسان مثالSimple AnalogySimple Analogy
Automation ایسا ہے جیسے اسسٹنٹ کو تربیت دینا — کام ایک بار کر کے دکھایا (EEM)، پھر وہ ہمیشہ خود کرے گا۔ NETCONF/RESTCONF ایک formal instruction form ہے (XML/JSON) تاکہ زبان سے commands چلانے کی بجائے۔Automation aisa hai jaise assistant ko tarbiyat dena — kaam ek baar kar ke dikhaya (EEM), phir wo hamesha khud karega. NETCONF/RESTCONF ek formal instruction form hai (XML/JSON) taake zaban se commands chillane ki bajaye.Automation is like training an assistant — you do the job once while they watch (EEM), then they handle it forever. NETCONF/RESTCONF is a formal instruction form (XML/JSON) instead of shouting commands.
سیٹ اپLab SetupLab Setup
کوئی بھی router جس میں EEM support ہو۔ Python اور NETCONF کے حصوں کے لیے Linux box لیں جس میں ncclient اور requests libraries installed ہوں۔Koi bhi router jis mein EEM support ho. Python aur NETCONF ke hisson ke liye Linux box lein jis mein ncclient aur requests libraries installed hon.Any router with EEM support. For Python and NETCONF parts, use a Linux box with the ncclient and requests libraries installed.
اقداماتStepsSteps
Step 1
EEM applet: روز رات 2 بجے config کا backup خود بخود لیں۔ $h hostname variable ہے۔EEM applet: roz raat 2 baje config ka backup khud-ba-khud lein. $h hostname variable hai.EEM applet: automatically back up the config every night at 2 AM. $h is the hostname variable.
event manager applet BACKUP-CONFIG event timer cron cron-entry "0 2 * * *" action 1.0 cli command "enable" action 2.0 cli command "copy running-config tftp://10.1.1.1/$h.cfg" show event manager policy active
Step 2
Event-driven EEM: جب کوئی interface down ہو تو فوری diagnostics چلائیں — دستی ردعمل سے تیز۔Event-driven EEM: jab koi interface down ho to fori diagnostics chalayein — dasti rad-e-amal se tez.Event-driven EEM: when any interface goes down, run diagnostics immediately — faster than manual reaction.
event manager applet IF-DOWN-ALERT event syslog pattern "Line protocol on Interface.*changed state to down" action 1.0 cli command "enable" action 2.0 cli command "show ip interface brief | include down"
Step 3
Python کی بنیاد: device output کو فائل سے پڑھیں اور regex سے hostname نکالیں۔ اصل outputs کو 'show run | redirect' سے فائل میں save کریں۔Python ki bunyaad: device output ko file se parhen aur regex se hostname nikalen. Asli outputs ko 'show run | redirect' se file mein save karein.Python basics: read saved device output and extract the hostname with regex. Save real outputs to files with 'show run | redirect'.
import re
output = open('show_run.txt').read()
for m in re.findall(r'^hostname (\S+)', output, re.M): print('Hostname:', m)Step 4
آگے بڑھیں: routing-table output کو (protocol، prefix) کی list میں تبدیل کریں — automated compliance checks کی بنیاد۔Aage barhein: routing-table output ko (protocol, prefix) ki list mein tabdeel karein — automated compliance checks ki bunyaad.Go further: parse routing-table output into a list of (protocol, prefix) tuples — the basis of automated compliance checks.
import re
text = open('show_ip_route.txt').read()
routes = re.findall(r'^(\S+)\s+(\d+\.\d+\.\d+\.\d+/\d+)', text, re.M)
print(len(routes), 'routes found')Step 5
NETCONF کا تصور: device SSH port 830 پر XML بولتا ہے۔ get-config running config کو structured XML کے طور پر پڑھتا ہے — screen-scraping کی ضرورت ختم۔NETCONF ka tasawwur: device SSH port 830 par XML bolta hai. get-config running config ko structured XML ke tor par parhta hai — screen-scraping ki zaroorat khatam.NETCONF concept: the device speaks XML over SSH port 830. get-config reads the running config as structured XML — no more screen-scraping.
netconf ssh 10.0.0.1 -p 830
Step 6
XML مثال: get-config request۔ JSON مثال: وہی hostname RESTCONF format میں۔ دونوں ایک ہی بات کہتے ہیں — shape YANG طے کرتا ہے۔XML example: get-config request. JSON example: wahi hostname RESTCONF format mein. Dono ek hi baat kehte hain — shape YANG tay karta hai.XML example: a get-config request. JSON example: the same hostname expressed in RESTCONF format. Both say the same thing — YANG defines the shape.
<rpc><get-config><source><running/></source></get-config></rpc>
{"Cisco-IOS-XE-native:hostname": "R1"}Step 7
RESTCONF کا تصور: ایک سادہ HTTPS GET hostname JSON کے طور پر دیتی ہے۔ حفاظت سے automate کریں — production میں ہمیشہ verify=True اور اصل certificates۔RESTCONF ka tasawwur: ek saadi HTTPS GET hostname JSON ke tor par deti hai. Hifazat se automate karein — production mein hamesha verify=True aur asli certificates.RESTCONF concept: a simple HTTPS GET returns the hostname as JSON. Automate safely — always use verify=True with real certificates in production.
import requests
requests.get('https://10.0.0.1/restconf/data/Cisco-IOS-XE-native:native/hostname', verify=False).json()تصدیقVerifyVerify
یقین کریں کہ applet schedule پر چلتی ہے، Python saved outputs سے درست data نکالتا ہے، اور آپ بتا سکتے ہیں NETCONF کب اور RESTCONF کب استعمال کرنا ہے۔Yaqeen karein ke applet schedule par chalti hai, Python saved outputs se durust data nikalta hai, aur aap bata sakte hain NETCONF kab aur RESTCONF kab use karna hai.Confirm the applet runs on schedule, Python extracts data correctly from saved outputs, and you can explain when to use NETCONF vs RESTCONF.
show event manager policy active show event manager history events
خرابی دور کرناTroubleshootingTroubleshooting
⚠️ EEM applet کبھی trigger نہیں ہوتی۔EEM applet kabhi trigger nahi hoti.EEM applet never triggers.
✅ Event syntax اور authorization چیک کریں — EEM CLI actions کے لیے 'event manager session cli username' چاہیے proper privilege کے ساتھ۔Event syntax aur authorization check karein — EEM CLI actions ke liye 'event manager session cli username' chahiye proper privilege ke saath.Check event syntax and authorization — EEM CLI actions need 'event manager session cli username' with proper privilege.
⚠️ RESTCONF 404 دے رہا ہے۔RESTCONF 404 de raha hai.RESTCONF returns 404.
✅ URL path (YANG path) غلط ہے۔ Model کا نام اور spelling چیک کریں — RESTCONF paths case-sensitive ہیں۔URL path (YANG path) ghalat hai. Model ka naam aur spelling check karein — RESTCONF paths case-sensitive hain.The URL path (YANG path) is wrong. Check the model name and spelling — RESTCONF paths are case-sensitive.
انٹرویو سوالاتInterview Q&AInterview Q&A
❓ NETCONF اور RESTCONF میں کیا فرق ہے؟NETCONF aur RESTCONF mein kya farq hai?What is the difference between NETCONF and RESTCONF?
NETCONF XML over SSH استعمال کرتا ہے fixed operations (get-config، edit-config) کے ساتھ۔ RESTCONF HTTP verbs (GET، PUT، POST) JSON کے ساتھ — web developers کے لیے آسان۔NETCONF XML over SSH use karta hai fixed operations (get-config, edit-config) ke saath. RESTCONF HTTP verbs (GET, PUT, POST) JSON ke saath — web developers ke liye asaan.NETCONF uses XML over SSH with a fixed operations set (get-config, edit-config). RESTCONF uses HTTP verbs (GET, PUT, POST) with JSON — easier for web developers.
❓ NETCONF اور RESTCONF کو YANG models کیوں چاہئیں؟NETCONF aur RESTCONF ko YANG models kyun chahiyein?Why do NETCONF and RESTCONF need YANG models?
YANG models طے کرتے ہیں کون سا data پڑھا/لکھا جا سکتا ہے اور اس کی structure کیا ہے — ان کے بغیر NETCONF/RESTCONF کو 'hostname' کا پتا ہی نہیں ہوتا۔YANG models tay karte hain kaun sa data parha/likha ja sakta hai aur us ki structure kya hai — in ke baghair NETCONF/RESTCONF ko 'hostname' ka pata hi nahi hota.YANG models define exactly what data can be read/written and its structure — without them, NETCONF/RESTCONF wouldn't know what a 'hostname' looks like.