Python, JSON, YANG, REST/RESTCONF, EEM & Automation

CCNP ENCOR 350-401 v1.2 EVE-NG / GNS3

مقصدObjectiveObjective

اس تھیوری+لیب لیسن میں آپ آٹومیشن کی بنیاد سیکھیں گے — Python اسکرپٹ پڑھنا، JSON سمجھنا، YANG ماڈل، RESTCONF، Catalyst Center اور SD-WAN Manager APIs، EEM applet، اور agent بمقابلہ agentless ٹولز۔ ENCOR D6 کا ٹاپک۔Is theory+lab lesson mein aap automation ki bunyaad seekhenge — Python script parhna, JSON samajhna, YANG model, RESTCONF, Catalyst Center aur SD-WAN Manager APIs, EEM applet, aur agent vs agentless tools. ENCOR D6 ka topic.In this theory+lab lesson you will learn automation basics — reading a Python script, understanding JSON, the YANG model, RESTCONF, Catalyst Center and SD-WAN Manager APIs, EEM applets, and agent vs agentless tools. An ENCOR D6 topic.

آسان مثالSimple AnalogySimple Analogy

آٹومیشن ایک روبوٹ اسسٹنٹ ہے۔ Python اسے دی گئی ہدایات ہیں (انگریزی جیسی زبان میں)۔ JSON خریداری کی فہرست جیسا فارمیٹ ہے — صاف اور ترتیب سے۔ YANG مینیو کا سانچہ ہے جو بتاتا ہے ڈیٹا کیسا دکھے گا۔ RESTCONF ویٹر ہے — آرڈر (request) لو، کھانا (data) لاؤ۔ EEM خودکار الارم ہے — وقت ہو تو خود بجتا ہے۔Automation ek robot assistant hai. Python usay di gayi hidayat hain (angrezi jaisi zaban mein). JSON kharidari ki fehrist jaisa format hai — saaf aur tarteeb se. YANG menu ka saancha hai jo batata hai data kaisa dikhega. RESTCONF waiter hai — order (request) lo, khana (data) lao. EEM khudkaar alarm hai — waqt ho to khud bajta hai.Automation is a robot assistant. Python is the instructions given to it (in an English-like language). JSON is a format like a shopping list — clean and ordered. YANG is a menu template that says what the data will look like. RESTCONF is the waiter — take the order (request), bring the food (data). EEM is an automatic alarm — it rings by itself when the time comes.

سیٹ اپLab SetupLab Setup

R1 روٹر (EEM کے لیے) اور Python والا PC۔ ڈیوائسز: 1x روٹر 2911 (R1)، 1x پی سی (Python installed)۔ تھیوری حصے کے لیے کوئی ٹوپولوجی نہیں۔R1 router (EEM ke liye) aur Python wala PC. Devices: 1x Router 2911 (R1), 1x PC (Python installed). Theory hisse ke liye koi topology nahi.R1 router (for EEM) and a PC with Python. Devices: 1x Router 2911 (R1), 1x PC (Python installed). No topology needed for the theory parts.

اقداماتStepsSteps

Step 1

یہ چھوٹی Python اسکرپٹ پڑھیں: urllib سے ڈیوائس کو GET request بھیجتی ہے، جواب JSON میں آتا ہے، json.loads اسے Python ڈکشنری بناتا ہے، پھر پہلے انٹرفیس کا نام print ہوتا ہے۔Ye chhoti Python script parho: urllib se device ko GET request bhejti hai, jawab JSON mein aata hai, json.loads usay Python dictionary banata hai, phir pehle interface ka naam print hota hai.Read this small Python script: it sends a GET request to the device with urllib, the reply comes in JSON, json.loads turns it into a Python dictionary, then it prints the first interface's name.

import urllib.request, json
url = "https://192.168.10.1/restconf/data/ietf-interfaces:interfaces"
req = urllib.request.Request(url)
req.add_header("Accept", "application/yang-data+json")
data = json.loads(urllib.request.urlopen(req).read())
print(data["ietf-interfaces:interfaces"]["interface"][0]["name"])

Step 2

JSON پڑھنا: curly braces {} ڈکشنری (key:value)، square brackets [] لسٹ۔ نیچے جواب میں interfaces کی لسٹ ہے — ہر انٹرفیس کا name اور enabled نظر آ رہا ہے۔JSON parhna: curly braces {} dictionary (key:value), square brackets [] list. Neeche jawab mein interfaces ki list hai — har interface ka name aur enabled nazar aa raha hai.Reading JSON: curly braces {} are a dictionary (key:value), square brackets [] are a list. Below, the reply has a list of interfaces — each interface's name and enabled flag is visible.

{
  "ietf-interfaces:interfaces": {
    "interface": [
      {"name": "GigabitEthernet0/0", "enabled": true},
      {"name": "GigabitEthernet0/1", "enabled": false}
    ]
  }
}

Step 3

YANG کا کانسیپٹ: ڈیٹا کا ماڈل — بتاتا ہے کہ ڈیوائس کا ڈیٹا کیسا دکھے گا (containers، lists، leafs)۔ NETCONF اور RESTCONF اسی ماڈل کی زبان بولتے ہیں۔YANG ka concept: data ka model — batata hai ke device ka data kaisa dikhega (containers, lists, leafs). NETCONF aur RESTCONF isi model ki zaban bolte hain.YANG concept: a data model — it describes what the device's data looks like (containers, lists, leafs). NETCONF and RESTCONF speak this model's language.

Step 4

RESTCONF: HTTP پر GET/POST/PUT/DELETE — پڑھنا، بنانا، بدلنا، مٹانا۔ جواب JSON میں۔ Catalyst Center اور SD-WAN Manager (vManage) کے REST APIs بھی یہی کرتے ہیں — پہلے token لو، پھر API calls سے پورا نیٹ ورک کنٹرول کرو۔RESTCONF: HTTP par GET/POST/PUT/DELETE — parhna, banana, badalna, mitana. Jawab JSON mein. Catalyst Center aur SD-WAN Manager (vManage) ke REST APIs bhi yehi karte hain — pehle token lo, phir API calls se poora network control karo.RESTCONF: GET/POST/PUT/DELETE over HTTP — read, create, change, delete. Replies come in JSON. The REST APIs of Catalyst Center and SD-WAN Manager (vManage) do the same — first get a token, then control the whole network with API calls.

Step 5

R1 پر EEM applet بنائیں — روز رات 2 بجے running-config کا بیک اپ TFTP سرور پر بھیجے۔ event trigger ہے، actions CLI کمانڈز ہیں۔R1 par EEM applet banao — roz raat 2 baje running-config ka backup TFTP server par bheje. event trigger hai, actions CLI commands hain.Create an EEM applet on R1 — every night at 2 AM it sends a backup of the running-config to the TFTP server. The event is the trigger; the actions are CLI commands.

enable
configure terminal
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://192.168.10.100/R1-backup.cfg"
exit
exit

Step 6

Agent بمقابلہ agentless: Ansible agentless ہے — SSH سے کام کرتا ہے، ڈیوائس پر کچھ install نہیں۔ Puppet اور Chef agent-based ہیں — ہر ڈیوائس پر agent install ہوتا ہے جو سرور سے ہدایات لیتا ہے۔Agent vs agentless: Ansible agentless hai — SSH se kaam karta hai, device par kuch install nahi. Puppet aur Chef agent-based hain — har device par agent install hota hai jo server se hidayat leta hai.Agent vs agentless: Ansible is agentless — it works over SSH, nothing installed on the device. Puppet and Chef are agent-based — an agent is installed on each device that takes instructions from the server.

Step 7

ٹیسٹنگ: show event manager policy registered سے applet دیکھیں۔ Python اسکرپٹ PC پر چلا کر دیکھیں کہ انٹرفیس کا نام print ہوتا ہے۔Testing: show event manager policy registered se applet dekho. Python script PC par chala kar dekho ke interface ka naam print hota hai.Testing: view the applet with show event manager policy registered. Run the Python script on the PC and check that it prints the interface name.

show event manager policy registered
show event manager history events

تصدیقVerifyVerify

show event manager policy registered میں BACKUP-CONFIG نظر آئے تو لیب کامیاب۔ تھیوری حصہ انٹرویو سوالات سے چیک کریں۔show event manager policy registered mein BACKUP-CONFIG nazar aaye to lab kamyab. Theory hissa interview sawalat se check karo.If BACKUP-CONFIG appears in show event manager policy registered, the lab is a success. Check the theory part with the interview questions.

show event manager policy registered

خرابی دور کرناTroubleshootingTroubleshooting

⚠️ EEM applet trigger نہیں ہو رہا۔EEM applet trigger nahi ho raha.The EEM applet is not triggering.

✅ Router کی گھڑی (clock) درست ہو — cron اسی پر چلتا ہے۔ event کی syntax چیک کریں۔Router ki ghari (clock) durust ho — cron isi par chalta hai. event ki syntax check karo.The router's clock must be correct — cron runs on it. Check the event syntax.

⚠️ RESTCONF سے 401 Unauthorized آ رہا ہے۔RESTCONF se 401 Unauthorized aa raha hai.RESTCONF returns 401 Unauthorized.

✅ یوزر/پاس ورڈ یا token غلط/expire ہوگا۔ پہلے authentication ٹھیک کریں، پھر request بھیجیں۔User/password ya token ghalat/expire hoga. Pehle authentication theek karo, phir request bhejo.The username/password or token is wrong/expired. Fix authentication first, then send the request.

انٹرویو سوالاتInterview Q&AInterview Q&A

❓ REST اور RESTCONF میں فرق؟REST aur RESTCONF mein farq?What is the difference between REST and RESTCONF?

REST ایک انداز ہے (HTTP verbs سے کام)۔ RESTCONF اسی انداز کو نیٹ ورک ڈیوائسز پر YANG ماڈلز کے ساتھ لاگو کرتا ہے۔REST ek andaaz hai (HTTP verbs se kaam). RESTCONF isi andaaz ko network devices par YANG models ke saath laagu karta hai.REST is a style (working with HTTP verbs). RESTCONF applies that style to network devices with YANG models.

❓ Ansible agentless کیوں کہا جاتا ہے؟Ansible agentless kyun kaha jata hai?Why is Ansible called agentless?

کیونکہ ڈیوائس پر کوئی agent install نہیں ہوتا — Ansible SSH سے جڑ کر کام کرتا ہے۔Kyunki device par koi agent install nahi hota — Ansible SSH se jur kar kaam karta hai.Because no agent is installed on the device — Ansible connects and works over SSH.