Python for Network Engineer (Python Network Programming Intro 1)

Python for Network Engineer
ZawGyi Version
Python Network Programming Intro 1
Programming ဆုိတာ ေျပာရရင္ computer(computing device) ကို ကိုယ္လုပ္ခ်င္တဲ့ instructions ေတြကို တစ္ခုျပီး တစ္ခု step by step ေရးထားတဲ့ Group of instruction တစ္ခု လို့ ျမင္ျကည့္က်ရေအာင္..
ညီ သိသေလာက္ Programming Language ကို နွစ္မ်ိုးခြဲထားလို့ရတယ္ဗ်ာ့..
Compiled Languages ရယ္ Scripting Languages ရယ္ ဆုိျပီး ရွိတယ္ဗ်ာ့..
Compiled Languages ဆိုတာ ဘာလဲဆုိေတာ့.. 
Computer တစ္လံုး တစ္ကယ္တမ္းနားလည္တာက 0/1 (on/off) ေတြကိုပဲနားလည္တာဗ်ာ့ printf တို့ cin cout တုိ့ အဲ့တာေတြကို နားလည္တာမဟုတ္ဘူး .. အဲ့ေတာ့ ကိုယ္ေတြလုပ္ခ်င္ ခိုင္းခ်င္တဲ့ instruction ေတြကို လူနားလည္တဲ့ code(Source Code) ေတြကေန computer machine နားလည္တဲ့ code(Machine Code) ေတြ အျဖစ္ေျပာင္းဖုိ့ လုိလာျပီ.. အဲ့လို လူနားလည္တဲ့ character/set of instructions ေတြကေန computer နားလည္တဲ့ 0/1 bits ေတြကို translate လုပ္ေပးဖုိ့ compiler ေတြနဲ့ compile လုပ္ရတယ္ဗ်ာ့... အဲ့လုိ set of instrutcions တစ္ခုလံုးကို compile လုပ္လိုက္ build လုပ္လိုက္မွ computer နားလည္တဲ့ machine code ေတြထြက္လာျပီး computer ေတြက ဖတ္ျပီး execute လုပ္လို့ run လို့ရမွာ.. Compiled language ေတြက ဘယ္ language ေတြလဲ ဆုိေတာ့.. java,C, C++, C#, Visual Basic, Pascal, etc စတာေတြျဖစ္တယ္ ;-)
Eg., 
$vi helloworld.c
#include<stdio.h>
int main()
{
printf("Helloworld");
}
ေရးျပီး helloworld ဆိုတဲ့ နာမည္နဲ့ ထားလိုက္္ပါ ျပီးေတာ့
$gcc helloworld.c -o hello ဆုိျပီး gcc(Gnu C Compiler) နဲ့ program တစ္ခုလံုး ကို compile လုပ္ျပီး ထြက္လာမယ့္ machine code ေတြနဲ့ file ကို -o နဲ့ output ကို hello ဆုိျပီး မွတ္ထားလိုက္ပါ.. ျပီးေတာ့ အဲ့ machine code ဖုိင္ကို execute လုပ္လုိက္ပါ $chmod 777 hello && ./hello
Scripting Language ဆုိတာက က်ေတာ့..
သူကက် ေျပာရရင္ programျကီးတစ္ခုလံုး(set of instructions) တစ္ခုလံုးကို compile လုပ္ေပးစရာမလုိပဲ.. instruction တစ္ခုခ်င္းဆီကို
one by one(Statement by statement) execute လုပ္ေပးတယ္ဗ်ာ့.. ဘယ္လိုမ်ိုးလဲဆိုေတာ့ scripting language ေတြက interpreter သံုးျပီး program run ေနတုန္္းမွာ statement တစ္ခုခ်င္းကို machine code အျဖစ္ေျပာင္းျပီး တိုက္ရုိက္ တစ္္ခုခ်င္း တစ္လိုင္းခ်င္း execute လုပ္တယ္ဗ်ာ့ .. Scripting Language ေတြက ဘယ္ေကာင္ေတြလဲဆုိေတာ့.. Python, Ruby on Rails, Java Script, PHP,etc စတာေတြျဖစ္တယ္
Eg,.
print("Helloworld") ဆိုတဲ့ code ေလးကို hello.py ဆိုတဲ့ နာမည္ေလးနဲ့ save ထားလိုက္မယ္.. ျပီးေတာ့ $python hello.py ဆိုျပီးru ျကည့္လိုက္ရင္ Helloworld ဆုိျပီ ထြက္လာလိမ့္မယ္... သူ့မွာ compile language လို compile လုပ္ေပးစရာမလိုဘူး တုိက္ရုိက္တန္း execute လုပ္ေပး run ေပးလိုက္ယံုပဲ.. ;-)
Scripting language ရယ္ compiled language မွာ ဘာကြာလဲဆုိေတာ့...
compiled language သည္ compile လုပ္ရတဲဲ့အခ်ိန္္လိုပါတယ္ 
scripting language ကေတာ့ compile လုပ္စရာမလုိပါဘူး
compile လုပ္လို့ ထြက္လာတဲ့ machine code ကို computer ေတြမွာ ေနာက္တစ္ျကိမ္ ထပ္ run စရာမလုိသလို source codeကိုလဲ ရွဲေပးစရာမလိုုပါဘူး ဒီတုိင္း ထြက္လာတဲ့ machine code ကို computer ေတြမွာ execute လုပ္ျပီး run လိုက္ယံုပါပဲ
scripting language မွာ ေတာ့ တစ္ျခားcomputer ေတြမွာ run မယ္ဆုိ source code ကိုရွဲ ေပးရပါတယ္..
looping ပါတ္တဲ့ program မွာဆုိ.. compiled language မွာ တစ္ျကိမ္ပဲ compile လုပ္တဲ့ အခ်ိန္္ ျကာျပီး ေနာက္အျကိမ္ေတြက် machine code ကို ျမန္ျမန္ဆန္္ဆန္ run ေပးလိုက္ယံုပါပဲ... scripting language မွာေတာ့ တစ္ျကိမ္run တိုင္း တစ္ေျကာင္းခ်င္းကို execute လုပ္ေပးရတဲ့အတြက္ ေရရွည္ျကည့္ရင္ အခ်ိန္ပိုျကာပါတယ္...
ကဲ python network program ေသးေသးေလး တစ္ခု ေရးဖုိ့ ျပင္ဆင္လိုက္ရေအာင္ဗ်ာ
https://sourceforge.net/projects/gns-3/files/Qemu%20Appliances/
အဲ့လင့္ကေနျပီးေတာ့ lisa-2.0.1-centos-6.3.tar.bz2 ဆုိတဲ့ centos အတြက္ qemu appliance ေလးကို ေဒါင္းလိုက္ပာ ျပီးေတာ့
$tar xjf lisa-2.0.1-centos-6.3.tar.bz2 ဆုိျပီး extract လုပ္လုိက္ပါ lisa-2.0.1-centos-6.3.img ဆိုျပီး ရလာပါလိမ့္မယ္
image file ေလးကို QEMU မွာ ထည့္ပံုထည့္နည္း အရင္ကေရးျပဘူးပါတယ္ page မွာ ျပန္ရွာျကည့္ပါ .. ခုေတာ့ အလြယ္ပဲ ျပန္ေျပာျပေတာ့မယ္..
GNS3 ရဲ့ edit ကေန preference ကို သြားပါ ျပီးေတာ့ Qemu VMs ကို နွိပ္ပါ... New ကိုေရြးပါ run this Qemu VM on GNS3 VM ကို select လုပ္ပါ next နွိပ္ပါ qemu binary ကို default အတုိင္းထားပါ next နွိပ္ပါ new image ေရြးေပးပါ upload လုပ္မွာလားဆုိ yes ေရြးေပးပါ finished ကိုနွိပ္ပါ... အဲ့တာဆုိ centos ကို qemu မွာ သြင္းထားပီးျဖစ္ပါတယ္
Topology photo ပံု ၁No automatic alt text available.
ျပီးေတာ့ switch ေတြကို control access ရဖုိ့ local password ေပးပါ
#enable password cisco
ျပီးေတာ့ telnet login အတြက္ username & password create လုပ္ပါ
#username spacex password cisco
#line vty 0 4
#login local
#transport input all
#end
ip ေပးပါ
#int vl 1
#ip addr 10.0.0.1 255.255.255.0
#no sh
ပံု ၂Image may contain: text
အဲ့တာဆို switch ဘက္ျခမ္းျပီးသြားပါျပီ ေနာက္ linux ဘက္ျခမး္မွာ ip configure လုပ္ရပါမယ္ 
centos >> login:root & password:password
ပံု ၃No automatic alt text available.
ifconfig eth0 10.0.0.254 netmask 255.255.255.0
ပံု၄Image may contain: text
ျပီးရင္ python script စပါမယ္...ပံု၅Image may contain: text
import getpass
import sys
import telnetlib /////library ေတြကို အရင္ import လုပ္ပါ
HOST = raw_input("Enter your Device IP: ") /// user ဆီက input ေတာင္းျပီး HOST ဆိုတဲ့ variable ထဲsaveပါတယ္
user = raw_input("Enter your telnet username: ") /// username ကို user ဆိုတဲ့ variable ထဲsaveပါတယ္
password = getpass.getpass() /// "Password:" ဆုိေတာင္းျပီး password ဆိုတဲ့ variable ထဲsaveပါတယ္
tln = telnetlib.Telnet(HOST) /// telnet connection ေဆာက္ပါတယ္ HOST ထဲမွာသိမ္းထားတဲ့ IP နဲ့
tln.read_until("Username: ") ///"Username: "ဆိုျပီးေမးမွာကို ေစာင့္ပါတယ္ ေမးတာကို ျမင္တာနဲ့ ေအာက္တစ္ေျကာင္း ဆင္းပါတယ္
tln.write(user + "\n") /// user ထဲမွာ သိမ္းထားတဲ့ username ရိုက္ပါတယ္
if password: /// password ေတာင္းတုန္္းက ရိုက္ခဲ့ရင္ 
tln.read_until("Password: ") ///"Password: " ဆုိျပီးေတာင္းတာနဲ့ ေနာက္တစ္ေျကာင္းဆင္းပါတယ္
tln.write(password + "\n") /// password ထဲမွာ သိမ္းထားတဲ့ က်ြန္ေတာ္တုိ့ ရို္ုက္လိုက္တဲ့ password ကိုထည့္ပါတယ္
///////////// UserName & Password မွန္သြားရင္ switch ထဲကို ေရာက္ပါျပီ loopback configure ခ်ဖုိ့ .. ရုိက္ရမယ့္ command ေတြကို တစ္ခုခ်င္း အစဥ္လိုက္ေရးပါ///////
tln.write("enable\n")
tln.write("cisco\n")
tln.write("conf t\n")
tln.write("int lo 0\n")
tln.write("ip addr 1.1.1.1 255.255.255.255\n")
tln.write("exit\n")
tln.write("end\n")
tln.write("exit\n")
print tln.read_all() /// ကိုယ္ switch မွာ ရိုက္ခဲ့တာေတြကို ျမင္ရေအာင္ ျပန္ထုတ္္တာပါ... ;-)
script မ run ခင္ ping ျကည့္ပါ ပံု ၆Image may contain: text
python script run ပါ ပံု၇No automatic alt text available.
ကဲ ေနာက္ရက္က်မွာ vlan creation ကို loop ပါတ္ျပီး လုပ္ေတြ input ျပန္ထုတ္တာေတြ ဆက္ေရးေတာ့မယ္...
Thanks for Reading
~[F]or-get
Uni Version
Python Network Programming Intro 1
Programming ဆိုတာ ပြောရရင် computer(computing device) ကို ကိုယ်လုပ်ချင်တဲ့ instructions တွေကို တစ်ခုပြီး တစ်ခု step by step ရေးထားတဲ့ Group of instruction တစ်ခု လို့ မြင်ကြည့်ကျရအောင်..
ညီ သိသလောက် Programming Language ကို နှစ်မျိုးခွဲထားလို့ရတယ်ဗျာ့..
Compiled Languages ရယ် Scripting Languages ရယ် ဆိုပြီး ရှိတယ်ဗျာ့..
Compiled Languages ဆိုတာ ဘာလဲဆိုတော့.. 
Computer တစ်လုံး တစ်ကယ်တမ်းနားလည်တာက 0/1 (on/off) တွေကိုပဲနားလည်တာဗျာ့ printf တို့ cin cout တို့ အဲ့တာတွေကို နားလည်တာမဟုတ်ဘူး .. အဲ့တော့ ကိုယ်တွေလုပ်ချင် ခိုင်းချင်တဲ့ instruction တွေကို လူနားလည်တဲ့ code(Source Code) တွေကနေ computer machine နားလည်တဲ့ code(Machine Code) တွေ အဖြစ်ပြောင်းဖို့ လိုလာပြီ.. အဲ့လို လူနားလည်တဲ့ character/set of instructions တွေကနေ computer နားလည်တဲ့ 0/1 bits တွေကို translate လုပ်ပေးဖို့ compiler တွေနဲ့ compile လုပ်ရတယ်ဗျာ့... အဲ့လို set of instrutcions တစ်ခုလုံးကို compile လုပ်လိုက် build လုပ်လိုက်မှ computer နားလည်တဲ့ machine code တွေထွက်လာပြီး computer တွေက ဖတ်ပြီး execute လုပ်လို့ run လို့ရမှာ.. Compiled language တွေက ဘယ် language တွေလဲ ဆိုတော့.. java,C, C++, C#, Visual Basic, Pascal, etc စတာတွေဖြစ်တယ် ;-)
Eg., 
$vi helloworld.c
#include<stdio.h>
int main()
{
printf("Helloworld");
}
ရေးပြီး helloworld ဆိုတဲ့ နာမည်နဲ့ ထားလိုက််ပါ ပြီးတော့
$gcc helloworld.c -o hello ဆိုပြီး gcc(Gnu C Compiler) နဲ့ program တစ်ခုလုံး ကို compile လုပ်ပြီး ထွက်လာမယ့် machine code တွေနဲ့ file ကို -o နဲ့ output ကို hello ဆိုပြီး မှတ်ထားလိုက်ပါ.. ပြီးတော့ အဲ့ machine code ဖိုင်ကို execute လုပ်လိုက်ပါ $chmod 777 hello && ./hello
Scripting Language ဆိုတာက ကျတော့..
သူကကျ ပြောရရင် programကြီးတစ်ခုလုံး(set of instructions) တစ်ခုလုံးကို compile လုပ်ပေးစရာမလိုပဲ.. instruction တစ်ခုချင်းဆီကို
one by one(Statement by statement) execute လုပ်ပေးတယ်ဗျာ့.. ဘယ်လိုမျိုးလဲဆိုတော့ scripting language တွေက interpreter သုံးပြီး program run နေတုန််းမှာ statement တစ်ခုချင်းကို machine code အဖြစ်ပြောင်းပြီး တိုက်ရိုက် တစ််ခုချင်း တစ်လိုင်းချင်း execute လုပ်တယ်ဗျာ့ .. Scripting Language တွေက ဘယ်ကောင်တွေလဲဆိုတော့.. Python, Ruby on Rails, Java Script, PHP,etc စတာတွေဖြစ်တယ်
Eg,.
print("Helloworld") ဆိုတဲ့ code လေးကို hello.py ဆိုတဲ့ နာမည်လေးနဲ့ save ထားလိုက်မယ်.. ပြီးတော့ $python hello.py ဆိုပြီးru ကြည့်လိုက်ရင် Helloworld ဆိုပြီ ထွက်လာလိမ့်မယ်... သူ့မှာ compile language လို compile လုပ်ပေးစရာမလိုဘူး တိုက်ရိုက်တန်း execute လုပ်ပေး run ပေးလိုက်ယုံပဲ.. ;-)
Scripting language ရယ် compiled language မှာ ဘာကွာလဲဆိုတော့...
compiled language သည် compile လုပ်ရတဲဲ့အချိန််လိုပါတယ် 
scripting language ကတော့ compile လုပ်စရာမလိုပါဘူး
compile လုပ်လို့ ထွက်လာတဲ့ machine code ကို computer တွေမှာ နောက်တစ်ကြိမ် ထပ် run စရာမလိုသလို source codeကိုလဲ ရှဲပေးစရာမလိုုပါဘူး ဒီတိုင်း ထွက်လာတဲ့ machine code ကို computer တွေမှာ execute လုပ်ပြီး run လိုက်ယုံပါပဲ
scripting language မှာ တော့ တစ်ခြားcomputer တွေမှာ run မယ်ဆို source code ကိုရှဲ ပေးရပါတယ်..
looping ပါတ်တဲ့ program မှာဆို.. compiled language မှာ တစ်ကြိမ်ပဲ compile လုပ်တဲ့ အချိန်် ကြာပြီး နောက်အကြိမ်တွေကျ machine code ကို မြန်မြန်ဆန််ဆန် run ပေးလိုက်ယုံပါပဲ... scripting language မှာတော့ တစ်ကြိမ်run တိုင်း တစ်ကြောင်းချင်းကို execute လုပ်ပေးရတဲ့အတွက် ရေရှည်ကြည့်ရင် အချိန်ပိုကြာပါတယ်...
ကဲ python network program သေးသေးလေး တစ်ခု ရေးဖို့ ပြင်ဆင်လိုက်ရအောင်ဗျာ
https://sourceforge.net/proj…/gns-3/files/Qemu%20Appliances/
အဲ့လင့်ကနေပြီးတော့ lisa-2.0.1-centos-6.3.tar.bz2 ဆိုတဲ့ centos အတွက် qemu appliance လေးကို ဒေါင်းလိုက်ပာ ပြီးတော့
$tar xjf lisa-2.0.1-centos-6.3.tar.bz2 ဆိုပြီး extract လုပ်လိုက်ပါ lisa-2.0.1-centos-6.3.img ဆိုပြီး ရလာပါလိမ့်မယ်
image file လေးကို QEMU မှာ ထည့်ပုံထည့်နည်း အရင်ကရေးပြဘူးပါတယ် page မှာ ပြန်ရှာကြည့်ပါ .. ခုတော့ အလွယ်ပဲ ပြန်ပြောပြတော့မယ်..
GNS3 ရဲ့ edit ကနေ preference ကို သွားပါ ပြီးတော့ Qemu VMs ကို နှိပ်ပါ... New ကိုရွေးပါ run this Qemu VM on GNS3 VM ကို select လုပ်ပါ next နှိပ်ပါ qemu binary ကို default အတိုင်းထားပါ next နှိပ်ပါ new image ရွေးပေးပါ upload လုပ်မှာလားဆို yes ရွေးပေးပါ finished ကိုနှိပ်ပါ... အဲ့တာဆို centos ကို qemu မှာ သွင်းထားပီးဖြစ်ပါတယ်
Topology photo ပုံ ၁
ပြီးတော့ switch တွေကို control access ရဖို့ local password ပေးပါ
#enable password cisco
ပြီးတော့ telnet login အတွက် username & password create လုပ်ပါ
#username spacex password cisco
#line vty 0 4
#login local
#transport input all
#end
ip ပေးပါ
#int vl 1
#ip addr 10.0.0.1 255.255.255.0
#no sh
ပုံ ၂
အဲ့တာဆို switch ဘက်ခြမ်းပြီးသွားပါပြီ နောက် linux ဘက်ခြမး်မှာ ip configure လုပ်ရပါမယ် 
centos >> login:root & password:password
ပုံ ၃
ifconfig eth0 10.0.0.254 netmask 255.255.255.0
ပုံ၄
ပြီးရင် python script စပါမယ်...ပုံ၅
import getpass
import sys
import telnetlib /////library တွေကို အရင် import လုပ်ပါ
HOST = raw_input("Enter your Device IP: ") /// user ဆီက input တောင်းပြီး HOST ဆိုတဲ့ variable ထဲsaveပါတယ်
user = raw_input("Enter your telnet username: ") /// username ကို user ဆိုတဲ့ variable ထဲsaveပါတယ်
password = getpass.getpass() /// "Password:" ဆိုတောင်းပြီး password ဆိုတဲ့ variable ထဲsaveပါတယ်
tln = telnetlib.Telnet(HOST) /// telnet connection ဆောက်ပါတယ် HOST ထဲမှာသိမ်းထားတဲ့ IP နဲ့
tln.read_until("Username: ") ///"Username: "ဆိုပြီးမေးမှာကို စောင့်ပါတယ် မေးတာကို မြင်တာနဲ့ အောက်တစ်ကြောင်း ဆင်းပါတယ်
tln.write(user + "\n") /// user ထဲမှာ သိမ်းထားတဲ့ username ရိုက်ပါတယ်
if password: /// password တောင်းတုန််းက ရိုက်ခဲ့ရင် 
tln.read_until("Password: ") ///"Password: " ဆိုပြီးတောင်းတာနဲ့ နောက်တစ်ကြောင်းဆင်းပါတယ်
tln.write(password + "\n") /// password ထဲမှာ သိမ်းထားတဲ့ ကျွန်တော်တို့ ရို်ုက်လိုက်တဲ့ password ကိုထည့်ပါတယ်
///////////// UserName & Password မှန်သွားရင် switch ထဲကို ရောက်ပါပြီ loopback configure ချဖို့ .. ရိုက်ရမယ့် command တွေကို တစ်ခုချင်း အစO်လိုက်ရေးပါ///////
tln.write("enable\n")
tln.write("cisco\n")
tln.write("conf t\n")
tln.write("int lo 0\n")
tln.write("ip addr 1.1.1.1 255.255.255.255\n")
tln.write("exit\n")
tln.write("end\n")
tln.write("exit\n")
print tln.read_all() /// ကိုယ် switch မှာ ရိုက်ခဲ့တာတွေကို မြင်ရအောင် ပြန်ထုတ််တာပါ... ;-)
script မ run ခင် ping ကြည့်ပါ ပုံ ၆
python script run ပါ ပုံရ
ကဲ နောက်ရက်ကျမှာ vlan creation ကို loop ပါတ်ပြီး လုပ်တွေ input ပြန်ထုတ်တာတွေ ဆက်ရေးတော့မယ်...
Thanks for Reading
~[F]or-get

Comments