Embed presentation
Downloaded 2,517 times














![C, Java, Python 䛾ẚ㍑ • ⌧ᅾ䛾䝕䜱䝺䜽䝖䝸௨ୗ䛾ᵓ㐀䜢᭩䛝ฟ䛩䝥䝻䜾䝷䝮 Python ධ㛛 [/Users/yuichi/Documents/_TRAINING] CCIE Java listFile.py python [/Users/yuichi/Documents/_TRAINING/CCIE] ccie-‐lab.key [/Users/yuichi/Documents/_TRAINING/Java] Java Basic Training Day 2.pptx Java Basic Training Day 3.pptx Java Basic Training Day 4.pptx Java Basic Training Day1.pptx [/Users/yuichi/Documents/_TRAINING/python] adfunc.py dict-‐test.py excepcon.py func.py 15 _TRAINING CCIE Java Python ccie-‐lab.key Java Basic Training Day2 ..... adfunc.py ..... lismile.py 䝥䝻䜾䝷䝮䛾ฟຊ](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-15-2048.jpg&f=jpg&w=240)

































![Listᆺ • ᣑᙇ䛜䛷䛝䜛㓄ิ [x,y,z] 䛸䛔䛖ᙧᘧ䛷సᡂ • ₇⟬Ꮚ [x] 䛷䚸㓄ิ䛾 x ␒┠䛻䜰䜽䝉䝇䛩䜛 • ఱ␒┠䛛(index)䛾ᣦᐃ䛿0䛛䜙ᩘ䛘䜛 Python ධ㛛 49 list1 = [3,4,5] print(list1) => [3, 4, 5] list2 = [“I”, “Love”, “Python”] print(list2[2]) => “Python” list3 = [[1,2], ['A',”B”]] print(list3) => [[1, 2], [“A”, “B”]] print(list3[1][1]) => “B” print(list3[5][0]) => # index out of range 䛾 Error ⾲♧](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-49-2048.jpg&f=jpg&w=240)
![Listᆺ • 㓄ิ㛗䜢ྲྀᚓ: len(X) • 䜸䝤䝆䜵䜽䝖䛾 index 䜢ྲྀᚓ: index(X) • 㓄ิ䛻䜸䝤䝆䜵䜽䝖䜢㏣ຍ: append(x) • 㓄ิ䛛䜙䜸䝤䝆䜵䜽䝖䜢๐㝖: del Python ධ㛛 50 list1 = [1,2,3,4,5] print(list1.index(3)) => 2 print(len(list1)) => 5 list1.append(6) print(list) => [1,2,3,4,5,6] del list2[1] print(list2) => [1,3,4,5,6]](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-50-2048.jpg&f=jpg&w=240)

















![forᩥ䛾᭩ᘧ Python ධ㛛 68 for ኚᩘ in 䝸䝇䝖: ᐇ⾜䛩䜛ฎ⌮㻌 䝸䝇䝖䛾0␒┠䜢ኚᩘ䛻௦ධ䛧䛶ฎ⌮䜢ᐇ⾜ 䝸䝇䝖䛾1␒┠䜢ኚᩘ䛻௦ධ䛧䛶ฎ⌮䜢ᐇ⾜ ..... string = for char in [H,e,l,l,o]: string += char print(string) Hello](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-68-2048.jpg&f=jpg&w=240)







![㛵ᩘ䛾䜃ฟ䛧 • ≉ᐃ䛾ฎ⌮䜢⾜䛖䛯䜑䛾䜃ฟ䛧ཱྀ • 㛵ᩘྡ䛻ᘬᩘ䜢䛘䛶䜃ฟ䛩 • 㛵ᩘ䛿㏉䜚್䜢㏉䛩 Python ධ㛛 76 㛵ᩘྡ(ᘬᩘ) ㏉䜚್ length = len([1,2,3,4,5]) print(length) 5](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-76-2048.jpg&f=jpg&w=240)











![sys.argv • 䝁䝬䞁䝗䝷䜲䞁ᘬᩘ䛻䜰䜽䝉䝇䛩䜛䛻䛿 sys 䝰䝆䝳䞊 䝹䜢 import 䛩䜛 • ᘬᩘ䛜argv䛻䝸䝇䝖䛸䛧䛶᱁⣡䛥䜜䛶䛔䜛 Python ධ㛛 88 import sys print(-‐-‐-‐-‐ args -‐-‐-‐-‐) print(len(sys.argv)) print(sys.argv[0]) print(sys.argv[1]) print(sys.argv[2]) print(-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐) a = int(sys.argv[1]) b = int(sys.argv[2]) print({} + {} = {}.format(a,b,a+b)) $ python test.py 1 5 -‐-‐-‐-‐ args -‐-‐-‐-‐ 3 test.py 1 5 -‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐ 1 + 5 = 6 test.py ฟຊ](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-88-2048.jpg&f=jpg&w=240)












![㎡᭩䛾᧯స • ㎡᭩䛾ᙧᘧ: {key1:value1, key2:value2} • ್䛾ྲྀᚓ: ㎡᭩䜸䝤䝆䜵䜽䝖[key] • ್䛾㏣ຍ(᭦᪂): ㎡᭩䜸䝤䝆䜵䜽䝖[key] = value • ್䛾๐㝖: del ㎡᭩䜸䝤䝆䜵䜽䝖[key] • key୍ぴ䛾ྲྀᚓ: ㎡᭩䜸䝤䝆䜵䜽䝖.keys() • key䜢ᣢ䛳䛶䛔䜛䛛: ㎡᭩䜸䝤䝆䜵䜽䝖.has_key(key) Python ධ㛛 101](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-101-2048.jpg&f=jpg&w=240)
![㎡᭩ᆺ䛾᧯స # ㎡᭩䜸䝤䝆䜵䜽䝖䜢సᡂ d = {Apple:Red, Lemon:Yellow} # key 䛛䜙 Value 䜢ྲྀᚓ d[Apple] 'Red' # ㎡᭩䜸䝤䝆䜵䜽䝖䛻せ⣲䜢㏣ຍ d[Grape] = Purple d {'Grape': 'Purple', 'Lemon': 'Yellow', 'Apple': 'Red'} # ㎡᭩䜸䝤䝆䜵䜽䝖䛾せ⣲䜢๐㝖 del d[Apple] d {'Grape': 'Purple', 'Lemon': 'Yellow'} Python ධ㛛 102](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-102-2048.jpg&f=jpg&w=240)

![㎡᭩ᆺ䛾䝃䞁䝥䝹 • Arp Table 䜢 List 䛸㻌㎡᭩䛷ᐇ(䜽䝷䝇䛺䛧) Python ධ㛛 104 ipList = [] macList = [] def registerMAC1(ip, mac): if ip in ipList: index = ipList.index(ip) macList[index] = mac else: ipList.append(ip) macList.append(mac) def getMAC1(ip): if ip in ipList: index = ipList.index(ip) return macList[index] else: return None arpTable = {} def registerMAC2(ip, mac): arpTable[ip] = mac def getMAC2(ip): return arpTable.get(ip) List䛻䜘䜛ᐇ: ᥈⣴䝁䝇䝖 Order(N) ㎡᭩䛻䜘䜛ᐇ: ᥈⣴䝁䝇䝖 Order(1) List䛻䜘䜛ᐇ ㎡᭩䛻䜘䜛ᐇ](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-104-2048.jpg&f=jpg&w=240)
![䝉䝑䝖 • 㔜」䚸㡰ᗎ䛾䛺䛔」ᩘ䛾䝕䞊䝍䜢ಖᣢ䛩䜛ᆺ • Value 䛾䛺䛔 Map 䛻㏆䛔 Python ධ㛛 105 a = set([1,2,3,4,5,3,2]) print(a) set([1, 2, 3, 4, 5]) a = set(hello) b = set(world) print(a) set(['h', 'e', 'l', 'o']) print(b) set(['d', 'r', 'o', 'w', 'l']) print(a b) set(['l', 'o'])](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-105-2048.jpg&f=jpg&w=240)


![䝍䝥䝹ᆺ䛾᧯స • Tuple 䛾సᡂ: tupleObject = (elem1, elem2,...) • せ⣲䛾ྲྀᚓ: item = tupleObject[index] • 」ᩘ䛾せ⣲䛾ྲྀᚓ: • せ⣲䛾᭦᪂: 䛷䛝䛺䛔 Python ධ㛛 108](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-108-2048.jpg&f=jpg&w=240)
![䝍䝥䝹ᆺ䛾᧯స # Tuple 䛾సᡂ yuiito = (Yuichi, Ito, 2011) yuiito ('Yuichi', 'Ito', 2011) # せ⣲䛾ཧ↷ yuiito[1] 'Ito' yuiito = (Yuichi, Ito, 2011) (name, famName, year) = yuiito year 2011 # せ⣲䛾᭦᪂ yuiito[2] = 2009 Traceback (most recent call last): File stdin, line 1, in module TypeError: 'tuple' object does not support item assignment Python ධ㛛 109](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-109-2048.jpg&f=jpg&w=240)
![䝍䝥䝹ᆺ䝃䞁䝥䝹 • 䝕䞊䝍ᵓ㐀䜢సᡂྍ⬟(䜽䝷䝇䛾⡆᫆∧) • ྍኚ䛺䜸䝤䝆䜵䜽䝖 Python ධ㛛 110 def getMinMax(numList): minNum = numList[0] maxNum = numList[0] for n in numList: if n minNum: minNum = n if n maxNum: maxNum = n return (minNum, maxNum)](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-110-2048.jpg&f=jpg&w=240)










![㏉䜚್ • 㛵ᩘ䛷ฎ⌮䛥䜜䛯䝕䞊䝍䜢䜃ฟ䛧ඖ䛻㏉䛩 • ㏉䜚್䛜䛺䛔ሙྜ䜒䛒䜛(స⏝䛾䜏䛾㛵ᩘ) Python ධ㛛 121 length = len([1,2,3,4,5]) length len([1,2,3,4,5]) 5 ㏉䜚್](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-121-2048.jpg&f=jpg&w=240)







![₇⩦ • day2-‐1.py.zip 䜢䝎䜴䞁䝻䞊䝗䛧ゎ • text = ࠥ 䛸䛧䛶๓ᐃ⩏䛥䜜䛶䛔䜛ᩥᏐิ䛾 ༢ㄒฟ⌧ᩘ䜢䜹䜴䞁䝖䛩䜛䝥䝻䜾䝷䝮䜢సᡂ • 䛂༢ㄒ:ฟ⌧ᩘ䛃䛸䛔䛖䝣䜷䞊䝬䝑䝖䛷ฟຊ䛩䜛 • ᩥᏐิ䛾ศ: ᩥᏐิ.split() -‐ [༢ㄒ䛾㓄ิ] Ⓨᒎ⦅ Python ධ㛛 dog:1 cat:2 ... 129 • ฟ⌧ᩘ䛜ከ䛔༢ㄒ㡰䛻䝋䞊䝖䛧䛶⾲♧䛩䜛 • 䝠䞁䝖䛿ḟ䝨䞊䝆](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-129-2048.jpg&f=jpg&w=240)
![₇⩦⿵㊊ • 䝸䝇䝖䜢䛳䛯⡆᫆䝋䞊䝖䜰䝹䝂䝸䝈䝮 • sort㛵ᩘ䜢䛳䛶䜒ᐇྍ⬟䛰䛜䚸䜎䛰ᩍ䛘䛶䛺䛔 䝔䜽䝙䝑䜽䛜ᚲせ Python ධ㛛 130 list1 = [3,6,2,9,0,1,5] list2 = [] while(0 != len(list1)): maxValue = max(list1) index = list1.index(maxValue) list2.append(maxValue) del list1[index] print(list2) -‐ [9, 6, 5, 3, 2, 1, 0]](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-130-2048.jpg&f=jpg&w=240)












![ᩥᏐิ䛾ฎ⌮2 • ⧞䜚㏉䛧: ᩥᏐิ * ⧞䜚㏉䛧ᅇᩘ • ᩥᏐิ: str(ᩥᏐิ௨እ䛾䜸䝤䝆䜵䜽䝖) • ᩥᏐิ䛾୍ᩥᏐ䜢ྲྀᚓ㻌ᩥᏐิ[index] • ᩥᏐิ䛾⠊ᅖྲྀᚓ: ᩥᏐิ[start:end] Python ධ㛛 143](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-143-2048.jpg&f=jpg&w=240)
![䝃䞁䝥䝹:ᩥᏐิ䛾ฎ⌮2 # ⧞䜚㏉䛧 print(cisco * 3) ciscociscocisco # ᩥᏐิ print(value: + str(5)) # 㠀ᩥᏐิ䜢ᩥᏐิ䛧䛶䛛䜙⤖ྜ value: 5 # ᩥᏐิ䛾୍ᩥᏐ䜢ྲྀᚓ hello world[4] 'o' # ᩥᏐิ䛾୍㒊䜢ྲྀᚓ hello world[4:8] 'o wo' Python ධ㛛 144](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-144-2048.jpg&f=jpg&w=240)

![䝃䞁䝥䝹:ᩥᏐิ䛾ฎ⌮3 # ⨨䛝䛘 hello world.replace(world, python) 'hello python' # ศ hello world.split( ) ['hello', 'world'] # ྵ䜐䛛 hell in hello world True # ⨨ hello world.find(o) 4 hello world.find(X) -‐1 # ᣦᐃ䛧䛯ᩥᏐิ䛜ྵ䜎䜜䛶䛔䛺䛔ሙྜ䛿 -‐1 Python ධ㛛 146](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-146-2048.jpg&f=jpg&w=240)


![ṇつ⾲⌧䛾᭷ຠᛶ • ᩥᏐิ䛜ᩚᩘ䛛䛹䛖䛛ุᐃ䛩䜛䛯䜑䛾䝥䝻 䜾䝷䝮䜢సᡂ䛩䜛 • ṇつ⾲⌧䛾᭷↓䛷䝁䞊䝗㔞䛜ᖜ䛻㐪䛖 Python ධ㛛 149 def isIntegerString(string): numberList = ['1','2','3','4','5','6','7','8','9','0'] if len(string) == 0: return false for char in string: if char not in numberList: return False return True import re def isIntegerString2(string): return re.match('^[d]*$', string) != None ṇつ⾲⌧䛺䛧 ṇつ⾲⌧䛒䜚](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-149-2048.jpg&f=jpg&w=240)

![ṇつ⾲⌧ Python ධ㛛 151 m = re.match(^[d]*$, 12345) print(m) _sre.SRE_Match object at 0x1005bdbf8 m.group() '12345' m.start(), m.end() (0, 5)](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-151-2048.jpg&f=jpg&w=240)



![ㄞ䜏㎸䜏ฎ⌮ • Read – readline(): ୍⾜ㄞ䜏㎸䜏 – readlines(): 䛶䛾⾜䜢 list 䛸䛧䛶ㄞ䜏㎸䜏 Python ධ㛛 155 f = open(file.txt, r) f.readline() 'hello world1n' f.readline() 'hello world2n' f.readlines() ['hello world3n', 'hello world4n'] hello world1 hello world2 hello world3 hello world4 file.txt](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-155-2048.jpg&f=jpg&w=240)
![᭩䛝㎸䜏ฎ⌮ • Write – write(ᩥᏐิ) : ᩥᏐิ䜢᭩䛝㎸䜏 – writelines(ᩥᏐิ䛾䝸䝇䝖) : 䝸䝇䝖䜢᭩䛝㎸䜏 – flush() : 䝞䝑䝣䜯䜢䝕䜱䝇䜽䛻䝣䝷䝑䝅䝳 Python ධ㛛 156 f2 = open(file2.txt, w) f2.write(hello) f2.write(world) f2.write(n) wlist = [abc, def, ghi] f2.writelines(wlist) f2.flush() f2.close() helloworld abcdefghi file2.txt](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-156-2048.jpg&f=jpg&w=240)



![Pickel Python ධ㛛 160 yuichi$ python data = [hello, world, 1, 2, 3] import pickle f = open(dump.txt, w) pickle.dump(data, f) f.close() exit() (lp0 S'hello' p1 aS'world' p2 aI1 aI2 aI3 a. $ python f = open(dump.txt) import pickle data = pickle.load(f) print(data) ['hello', 'world', 1, 2, 3] dump.txt](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-160-2048.jpg&f=jpg&w=240)





![䝟䝇䛾᧯స㛵ᩘ Python ධ㛛 166 os.path.exists(/Users/yuichi) True os.path.isfile(/Users/yuichi) False os.listdir(./) ['dir1', 'file1'] os.mkdir(dir2) os.rmdir(dir2)](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-166-2048.jpg&f=jpg&w=240)




![䝁䝬䞁䝗䛾䜃ฟ䛧 Python ධ㛛 171 • os.system(䝁䝬䞁䝗) : ฟຊ䛜せ䛺ሙྜ • commands.getoutput(䝁䝬䞁䝗) : ฟຊ䛜ᚲせ䛺ሙྜ commands.getoutput(ls).split(n) ['CONFIG_FILE', 'Cisco Mac Support.webloc', 'Desktop', 'Documents', 'Downloads', 'Dropbox', 'Library', 'MATERIAL', 'Movies', 'Music', 'Pictures', 'Public', 'SR', 'STP-‐BA-‐Dispute_Japan-‐ RSVT.pptx', 'VM', 'appProperces', 'bingo.py', 'bingo.pyc', 'dump.txt', 'file.txt', 'file2.txt', 'get-‐fpath-‐info.py', 'mki-‐fp-‐ capture.zip', 'tcpdump', 'tcpdump.zip', 'test', 'unctled folder']](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-171-2048.jpg&f=jpg&w=240)

![exping 䛾ᐇ • 」ᩘ䛾ᐄඛ䛾ping฿㐩⋡䜢 ᐃ Python ධ㛛 173 import commands def ping(dest): result = commands.getoutput(ping -‐c 5 {}.format(dest)) lines = result.split(n) length = len(lines) packetLoss = lines[length -‐2].split()[6] r7 = lines[length -‐1].split()[3].split(/)[1] return (dest, packetLoss, r7) dests = [cisco.com, google.com, yahoo.com] for dest in dests: print(ping(dest)) ('cisco.com', '0.0%', '196.756') ('google.com', '0.0%', '13.317') ('yahoo.com', '0.0%', '262.060')](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-173-2048.jpg&f=jpg&w=240)



![₇⩦ • ᮍᡂ䛾䝻䜾ᢳฟ䝥䝻䜾䝷䝮䜢ᡂ䛥䛫䜛 • 䝞䜾䛿3⟠ᡤ䛻䛒䜛 yuichi$ python log.py -‐f log.log -‐s 2014 Jan 28 12:30:50 -‐e 2014 Jan 29 16:06:15 -‐k port-‐channel -‐f : Python ධ㛛 177 䝣䜯䜲䝹ྡ -‐s : 䝇䝍䞊䝖้ -‐e : ⤊้ -‐k : 䜻䞊䝽䞊䝗 dhcp-‐10-‐141-‐56-‐250:Documents yuichi$ python log.py -‐f log.log -‐s 2014 Jan 28 12:30:50 -‐e 2014 Jan 29 16:06:15 -‐k port-‐channel 2014 Jan 29 12:14:20 N6K-‐2 %ISIS_FABRICPATH-‐5-‐ADJCHANGE: isis_fabricpath-‐default [5150] P2P adj L1 n7kb-‐MKI-‐FP over port-‐channel50 -‐ DOWN (New) on MT-‐0 2014 Jan 29 12:14:20 N6K-‐2 %ISIS_FABRICPATH-‐5-‐ADJCHANGE: isis_fabricpath-‐default [5150] P2P adj L1 n7kb-‐MKI-‐FP over port-‐channel50 -‐ INIT on MT-‐0 2014 Jan 29 12:14:20 N6K-‐2 %ISIS_FABRICPATH-‐5-‐ADJCHANGE: isis_fabricpath-‐default [5150] P2P adj L1 n7kb-‐MKI-‐FP over port-‐channel50 -‐ UP on MT-‐0 2014 Jan 29 12:24:58 N6K-‐2 %ISIS_FABRICPATH-‐5-‐ADJCHANGE: isis_fabricpath-‐default [5150] P2P adj L1 n7kb-‐MKI-‐FP over port-‐channel50 -‐ DOWN (Delete All) on MT-‐0 2014 Jan 29 12:27:01 N6K-‐2 %ISIS_FABRICPATH-‐5-‐ADJCHANGE: isis_fabricpath-‐default [5150] P2P adj L1 n7kb-‐MKI-‐FP over port-‐channel50 -‐ DOWN (New) on MT-‐0 2014 Jan 29 12:27:01 N6K-‐2 %ISIS_FABRICPATH-‐5-‐ADJCHANGE: isis_fabricpath-‐default [5150] P2P adj L1 n7kb-‐MKI-‐FP over port-‐channel50 -‐ INIT on MT-‐0 2014 Jan 29 12:27:01 N6K-‐2 %ISIS_FABRICPATH-‐5-‐ADJCHANGE: isis_fabricpath-‐default [5150] P2P adj L1 n7kb-‐MKI-‐FP over port-‐channel50 -‐ UP on MT-‐0](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-177-2048.jpg&f=jpg&w=240)
![䝁䞊䝗(䝞䜾䛿2⟠ᡤ) Python ධ㛛 178 import sys, os def getTimeString(line): return line[0:20] def parseTime(string): monthDict = {Jan:1, Feb:2, Mar:3, Apr:4, May:5, Jun:6, Jul:7, Aug:8, Sep:9, Oct:10, Nov:11, Dec:12} d1 = string.split( ) d2 = d1[3].split(:) return (int(d1[0]), monthDict[d1[1]], int(d1[2]), int(d2[0]), int(d2[1]), int(d2[2])) def printFile(fname, start, end, keyword): if(not os.path.isfile(fname)): print(File {} isn't exist. exit.format(fname)) sys.exit() f = open(fname) for line in f.readlines(): line = line.rstrip() t = parseTime(getTimeString(line)) within = (start = t) and (t = end) contains = keyword in line if(within and contains): print(line) 䝁䝬䞁䝗ᘬᩘ䛾ึᮇ್ fname = start = 1900 Jan 1 00:00:00 end = 2100 Jan 1 00:00:00 keyword = for index in range(len(sys.argv)): arg = sys.argv[index] if(arg == -‐f): fname = sys.argv[index + 1] elif(arg == -‐s): start = sys.argv[index + 1] elif(arg == -‐k): keyword = sys.argv[index + 1] if(fname == ): s1 = syntax: python log.py -‐f FILENAME -‐s s2 = START_TIME -‐e END_TIME -‐k KEYWORD print(s1 + s2) print(START_TIME, END_TIME, KEYWORD are opcon.) else: printFile(fname, start, end, keyword) 䝻䜾䛾୍⾜䛛䜙㛫䛾ᩥᏐิ䜢ᢳฟ ᢳฟ䛧䛯ᩥᏐิ䜢ẚ㍑ྍ⬟䛺䝍䝥䝹䛻ኚ ᮲௳䛻䝬䝑䝏䛧䛯⾜䜢printฟຊ 䝁䝬䞁䝗ᘬᩘ䜢ㄞ䜏ྲྀ䜛 䝣䜯䜲䝹ྡ䛜䛘䜙䜜䛶䛔䛯䜙䝥䝻䜾䝷䝮䜢ᐇ⾜](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-178-2048.jpg&f=jpg&w=240)


![⏝䛩䜛䝁䝬䞁䝗 Python ධ㛛 181 yuichi$ ssh -‐l guest 10.71.224.172 pass: c1sco123 [guest@fedora172 ~]$ nmap 127.0.0.1 Starcng Nmap 6.01 ( h7p://nmap.org ) at 2014-‐02-‐08 13:05 EST Nmap scan report for fedora172.localdomain (127.0.0.1) Host is up (0.0016s latency). Not shown: 995 closed ports PORT STATE SERVICE 22/tcp open ssh 23/tcp open telnet 25/tcp open smtp 111/tcp open rpcbind 631/tcp open ipp](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-181-2048.jpg&f=jpg&w=240)










![ே㛫䛾ᛮ⪃᪉ἲ • 䛂ㄡ䛜ఱ䜢䛩䜛䛛䛃䛜㔜せ • ㄡ䛻䛹䛖䛔䛖䛂䝯䝑䝉䞊䝆䛃䜢㏦䛳䛯䜙䚸䛹䛖䛔䛖䛂⤖ ᯝ䛃䛜ᚓ䜙䜜䜛䛛 Python ධ㛛 [ෆ㒊ฎ⌮] 䜢䛩䜛 192 [䝯䝑䝉䞊䝆] 䛣䛾䜢䛧䛶䛟䛰䛥䛔 [⤖ᯝ] ୖྖ ᡂᯝ≀䛿䛣䜜䛷䛩䚹 㒊ୗ](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-192-2048.jpg&f=jpg&w=240)







![䜽䝷䝇 • 䜸䝤䝆䜵䜽䝖䛾✀㢮 • 䜽䝷䝇䛿ᒓᛶ䛸䛧䛶䝕䞊䝍䛸ฎ⌮䜢ᣢ䛴 Python ධ㛛 200 ᒓᛶ [䝕䞊䝍] -‐ 䜺䝋䝸䞁䛾㔞 [ฎ⌮] -‐ ㉮䜛 -‐ Ṇ䜎䜛 -‐ ᭤䛜䜛](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-200-2048.jpg&f=jpg&w=240)
![䜲䞁䝇䝍䞁䝇 • 䜽䝷䝇⮬య䛷ฎ⌮䛩䜛䛾䛷䛿䛺䛟䚸䜽䝷䝇䛛䜙స䜛䜲 䞁䝇䝍䞁䝇䛷ฎ⌮䜢䛩䜛 • 㒊ୗ䛸䛔䛖ᴫᛕ(䜽䝷䝇)䛷䛿䛺䛟㒊ୗ䛾䖣䖣(䜲䞁䝇䝍 䞁䝇)䛜䜢䛩䜛 Python ධ㛛 [ෆ㒊ฎ⌮] 䜢䛩䜛 201 [䝯䝑䝉䞊䝆] 䛣䛾䜢䛧䛶䛟䛰䛥䛔 [⤖ᯝ] 䜽䝷䝇: ୖྖ ᡂᯝ≀䛿䛣䜜䛷䛩䚹 䜲䞁䝇䝍䞁䝇: BOB 䜽䝷䝇: 㒊ୗ 䜲䞁䝇䝍䞁䝇: TOM](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-201-2048.jpg&f=jpg&w=240)










![䜽䝷䝇䝃䞁䝥䝹 Python ධ㛛 212 # bingo.py import random class BingoMachine: ballList = [] def __init__(self, min, max): self.ballList = range(min, max + 1) def getBall(self): if(len(self.ballList) == 0): return -‐1 index = int(random.uniform(0, len(self.ballList))) return self.ballList.pop(index) # ᐇ⾜⤖ᯝ from bingo import * b = BingoMachine(1,5) b.getBall() 1 b.getBall() 5 b.getBall() 2 b.getBall() 3 b.getBall() 4 b.getBall() -‐1 䝡䞁䝂䝬䝅䞊䞁 ᩘᏐ㻌n – m 䛾㛫䛾ᩘᏐ䜢䝷䞁䝎䝮䛻㏉䛩 ୍ᗘ㏉䛧䛯್䛿㏉䛥䛺䛔](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-212-2048.jpg&f=jpg&w=240)














![䝬䝑䝥 • ఱ䜒䛺䛔䝬䝑䝥䜢ᥥ⏬ • ⦪䛸ᶓ䛾㛗䛥䜢ᣦᐃྍ⬟ • ⾜(row)ᣦྥ䛷ᥥ⏬䛩䜛 227 +-‐-‐-‐-‐-‐-‐-‐-‐+ | | | | | | +-‐-‐-‐-‐-‐-‐-‐-‐+ 4㝮䛿㻌䛂+䛃 yᗙᶆ0, ್᭱䛿㻌䛂–䛃 xᗙᶆ0, ್᭱䛿㻌䛂|䛃 䛭䜜௨እ䛿✵ⓑ class DQMap: max_x = 0 max_y = 0 def __init__(self, max_xy): (self.max_x, self.max_y) = max_xy def render_map(self): map_string_list = [] for y in range(self.max_y): for x in range(self.max_x): if(y == 0 or y == self.max_y -‐ 1): if(x == 0 or x == self.max_x -‐ 1): map_string_list.append(+) else: map_string_list.append(-‐) else: if(x == 0 or x == self.max_x -‐ 1): map_string_list.append(|) else: map_string_list.append( ) map_string_list.append(n) map_string = .join(map_string_list) print(map_string.rstrip())](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-227-2048.jpg&f=jpg&w=240)




![䝬䝑䝥䛸⏫ே䛾ᐇ Python ධ㛛 232 class DQMap: ... people_map = {} def __init__(self, max_xy, hero_xy, people_paramList): .... for params in people_paramList: (icon, x, y, comment) = params self.people_map[(x,y)] = people.People(icon, x, y, comment) def render_map(self, comment): map_string_list = [] (hx, hy) = self.hero.get_xy() for y in range(self.max_y): for x in range(self.max_x): if((x,y) == (hx, hy)): map_string_list.append(self.hero.get_icon()) elif(self.people_map.has_key((x, y))): p = self.people_map[(x, y)] map_string_list.append(p.get_icon()) 䝁䞁䝇䝖䝷䜽䝍䛷⏫ே䛾 ሗ䜢䝸䝇䝖ᙧᘧ䛷ᣦᐃ ⏫ே䛾䜲䞁䝇䝍䞁䝇䜢సᡂ (x,y)䜢䜻䞊䛻䛧㎡᭩䛷⏫ே䜢⟶⌮ ᥥ⏬䛾㝿䛻䚸x, y 䛻⏫ே䛜䛔䜛䛛☜ㄆ 䜒䛧䛔䜜䜀⏫ே䜢ᥥ⏬䛩䜛](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-232-2048.jpg&f=jpg&w=240)







![䝁䝢䞊䛸ཧ↷ • 䝁䝢䞊: ␗䛺䜛䜸䝤䝆䜵䜽䝖 • ཧ↷: ྠ୍䜸䝤䝆䜵䜽䝖䜢」ᩘ⟠ᡤ䛷⏝ • ཧ↷䛻ᑐ䛧䛶䛿స⏝䛾ὀព䛜ᚲせ Python ධ㛛 240 a = [1,2,3,4] b = a b.append(5) a [1, 2, 3, 4, 5]](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-240-2048.jpg&f=jpg&w=240)



![Expect 䛾ືస • spawn: 䝉䝑䝅䝵䞁䜢㛤ጞ • expect: ᮇᚅ䛩䜛ᶆ‽ධຊ(from 䝸䝰䞊䝖)䜢ᚅ䛴 • send: 䝸䝰䞊䝖䛻ฟຊ䜢㏦䜛 Python ධ㛛 244 dhcp-‐10-‐141-‐56-‐250:~ yuichi$ telnet -‐l yuiito rws1 Trying 10.71.231.245... Connected to rws1.cisco.com. Escape character is '^]'. Password: cisco123 Last login: Thu Jan 23 14:53:37 from dhcp-‐10-‐141-‐56-‐250.cisco.com unknown terminal xterm-‐256color unknown terminal xterm-‐256color [yuiito@rws1 yuiito]$ echo hello test [yuiito@rws1 yuiito]$ ls snmpget.sh test 㟷䛜䝸䝰䞊䝖䛛䜙䛾ᶆ‽ධຊ ㉥䛜䝸䝰䞊䝖䜈䛾ᶆ‽ฟຊ](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-244-2048.jpg&f=jpg&w=240)
![expect 䝃䞁䝥䝹 • Linux 䜈䛾䝻䜾䜲䞁䛸᧯స Python ධ㛛 245 Password: cisco123 yuiito$ echo hello test login-‐to-‐rws1.exp #! /usr/bin/expect spawn telnet -‐l yuiito rws1 expect Password: send cisco123n expect yuiito]$ send touch hellon interact 䜃ฟ䛧 yuichi$ expect login-‐to-‐rws1.exp](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-245-2048.jpg&f=jpg&w=240)



![pexpect 䛾䝃䞁䝥䝹 • rws1 䛻䝻䜾䜲䞁䛧䛶䝣䜯䜲䝹㻌pexpect 䜢సᡂ Python ධ㛛 249 import pexpect child = pexpect.spawn(telnet -‐l yuiito rws1) child.expect(Password:) child.sendline(cisco123) child.expect(yuiito]$) child.sendline(touch pexpect)](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-249-2048.jpg&f=jpg&w=240)
![pexpext䛾䝃䞁䝥䝹 • Nexus䛾タᐃ䜢䝣䜯䜲䝹ྡ䜢᪥䛻䛧䛶FTP䝃䞊䝞䛻ಖᏑ Python ධ㛛 250 import pexpect, datecme def getLines(string): lines = lines = [x.strip() for x in string.split(n)] return filter(lambda x: x!= '', lines) def getHostName(exp): exp.sendline(show hostname) exp.expect(#) return getLines(exp.before)[1] def getToDay(): d = datecme.datecme.today() return {}_{}_{}-‐{}_{}_{}.format(d.year, d.month, d.day, d.hour, d.minute, d.second) def makeFileName(exp): hostName = getHostName(exp) today = getToDay() return {}-‐{}.conf.format(hostName, today) def login(exp, password): exp.expect(Password:) exp.sendline(password) exp.expect(#) exp.sendline(terminal length 0) exp.expect(#) def saveFile(exp, saveFrom, saveTo, vrf, username, password): command = copy {} {}.format(saveFrom, saveTo) exp.sendline(command) exp.expect(Enter vrf) exp.sendline(vrf) exp.expect(Enter username:) exp.sendline(username) exp.expect(Password:) exp.sendline(password) exp.expect(#) child = pexpect.spawn(telnet -‐l admin 10.71.156.229) login(child, cisco) saveTo = p://10.71.224.115/yuiito/ + makeFileName(child) saveFile(child, running-‐config, saveTo, management, anonymous, ) 䝣䜯䜲䝹ྡ䜔䝁䝬䞁䝗䛾⏕ᡂ䛜 python ௵䛫 child.close()](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-250-2048.jpg&f=jpg&w=240)
















































































![map 㛵ᩘ • List 䛾ྛせ⣲䛻ᑐ䛧䛶㛵ᩘ䜢㐺⏝䛩䜛㛵ᩘ map(㛵ᩘ, Python 䝸䝇䝖) ධ㛛 331 N = 10 def double(x): return x * 2 list1 = map(double, range(N)) list2 = [] for i in range(N): list2.append(i*2) print(list1) = [0, 2, 4, 6, 8, 10, 12, 14, 16, 18] print(list2) = [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-331-2048.jpg&f=jpg&w=240)
![filter 㛵ᩘ • List 䛾ྛせ⣲䛻ᑐ䛧䛶㛵ᩘ䜢㐺⏝䛧䚸True 䛻 䛺䛳䛯せ⣲䛾䜏䛷 List 䜢స䜛 Python ධ㛛 332 def isOver0(x): return x 0 list1 = range(-‐5,5) list2 = filter(isOver0, list1) print(list1) = [-‐5, -‐4, -‐3, -‐2, -‐1, 0, 1, 2, 3, 4] print(list2) = [1, 2, 3, 4]](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-332-2048.jpg&f=jpg&w=240)
![lamdaᘧ • 㛵ᩘ䜢䛭䛾ሙ䛷ᐃ⩏䛩䜛䛯䜑䛾ᡭἲ • 㛵ᩘ䜢㏉䛩 Python ධ㛛 333 lamda(ᘬᩘ): ฎ⌮ power = lambda x: x**2 list1 = range(10) map(power, list1) [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] map(lambda x: x**2, range(10)) [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-333-2048.jpg&f=jpg&w=240)


![䝸䝇䝖ෆໟ⾲グ • if ᩥ䜢䛳䛶せ⣲䜢⤠䜜䜛 Python ධ㛛 336 [x**2 for x in range(10) if x % 2 == 0] [0, 4, 16, 36, 64] 1. 䝸䝇䝖䛛䜙അᩘ䛾䜏ᢤ䛝ฟ䛩 2. അᩘ䛾䝸䝇䝖䛻ᑐ䛧䛶 2䛩䜛ฎ⌮䜢㐺⏝ [ฎ⌮ for せ⣲ in 䝸䝇䝖 if ᮲௳ᘧ] 䝸䝇䝖䛾せ⣲䛛䜙᮲௳ᘧ䛜True䛻䛺䜛䜒䛾䛾䜏ᢳฟ 䛭䜜䛻ᑐ䛧䛶ฎ⌮䜢㐺⏝](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-336-2048.jpg&f=jpg&w=240)


![dir • 䜽䝷䝇䜔䝟䝑䜿䞊䝆䛾୰㌟䜢☜ㄆ䛩䜛㛵ᩘ dir(str) ['__add__', '__class__', '__contains__', '__dela7r__', '__doc__', '__eq__', '__format__', '__ge__', '__geta7ribute__', '__gectem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__seta7r__', '__sizeof__', '__str__', '__subclasshook__', '_forma7er_field_name_split', '_forma7er_parser', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'isctle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'parccon', 'replace', 'rfind', 'rindex', 'rjust', 'rparccon', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'ctle', 'translate', 'upper', 'zfill'] Python ධ㛛 339](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-339-2048.jpg&f=jpg&w=240)





![䛾䝎䜴䞁䜢᳨ฟ (䝁䞁䝋䞊䝹⾲♧䜢䝏䜵䝑䜽) ᳨ฟ䛧䛯䜙䚸௨ୗ䛾䝇䜽䝸䝥䝖䜢ᐇ⾜ Python ධ㛛 345 EEM 䛷 peer-‐link MKI-‐VDC2# show file boomlash:if-‐mon.py # VPC 䛾≧ែ☜ㄆ䝁䝬䞁䝗䜢Ⓨ⾜䚹VPC 䛾䝇䝔䞊䝖䜢ᚓ䜛 shVpc = cli (show vpc) shVpcLines = shVpc.splitlines() stateList = shVpcLines[11].split() vpcState = stateList[len(state_list)-‐1] # 䜒䛧VPC䛜 secondary 䛺䜙䚸≉ᐃ䜲䞁䝍䝣䜵䞊䝇䜢ⴠ䛸䛩 if (vpcState == secondary): print(shutdown fp interface) cli(conf t ; int e7/9,e9/9 ; shut)](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-345-2048.jpg&f=jpg&w=240)


![Python ධ㛛 348 if __name__ == __main__: if(len(sys.argv) == 4): fileName = sys.argv[1] sleepInterval = int(sys.argv[2]) sleepCount = int(sys.argv[3]) print(taking Rate logs) writeTime(fileName) writeRateLogs(fileName, sleepInterval, sleepCount) print(taking FP logs) writeTime(fileName) writeFPLogs(fileName) writeOldLogs(fileName) def makeEcho(string, fileName): return 'echo ' + string + ' boomlash:' + fileName def makeShow(string, fileName): return string + boomlash: + fileName def makeCombi(string, fileName): #䝁䝬䞁䝗䛾้, ྡ๓, ⤖ᯝ䜢᭩䛟 return makeShow(show clock, fileName) + ; + makeEcho(# + string, fileName) + ; + makeShow(string, fileName) + ; + makeEcho(, fileName) def writeRateLogs(fileName, interval, n): # Rate 䜢ᐃᮇ ᐃ for i in range(0, n): cli(makeCombi(SHOW_RATE_BETWEEN_N6K_AND_N7K, fileName)) cli(makeCombi(SHOW_RATE_AT_PEER_LINK, fileName)) cli(makeEcho(, fileName)) cme.sleep(n) def writeFPLogs(fileName): # 䝁䝬䞁䝗䛾⨶ิ cli(makeCombi(SHOW_FP_ISIS_ROUTE, fileName)) cli(makeCombi(SHOW_FWM_L2MP_NEXT_HOP, fileName)) cli(makeCombi(SHOW_FWM_HWSTM, fileName)) cli(makeCombi(SHOW_FWM_L2MP_ROUTE, fileName)) cli(makeCombi(SHOW_FWM_MAC_HSRP_VMAC, fileName)) cli(makeCombi(SHOW_FWM_L2MP_VLAN, fileName)) cli(makeCombi(SHOW_FWM_L2MP_TOPO_0, fileName))](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-348-2048.jpg&f=jpg&w=240)









The document discusses various aspects of Python programming within the context of Cisco Systems and includes information about learning resources, technical details, and programming concepts. It covers multiple Python functionalities such as ping operations, program structure, and installation on different operating systems. Additionally, it highlights the significance of Python in network management and introduces ongoing Python training sessions.














![C, Java, Python 䛾ẚ㍑ • ⌧ᅾ䛾䝕䜱䝺䜽䝖䝸௨ୗ䛾ᵓ㐀䜢᭩䛝ฟ䛩䝥䝻䜾䝷䝮 Python ධ㛛 [/Users/yuichi/Documents/_TRAINING] CCIE Java listFile.py python [/Users/yuichi/Documents/_TRAINING/CCIE] ccie-‐lab.key [/Users/yuichi/Documents/_TRAINING/Java] Java Basic Training Day 2.pptx Java Basic Training Day 3.pptx Java Basic Training Day 4.pptx Java Basic Training Day1.pptx [/Users/yuichi/Documents/_TRAINING/python] adfunc.py dict-‐test.py excepcon.py func.py 15 _TRAINING CCIE Java Python ccie-‐lab.key Java Basic Training Day2 ..... adfunc.py ..... lismile.py 䝥䝻䜾䝷䝮䛾ฟຊ](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-15-2048.jpg&f=jpg&w=240)

































![Listᆺ • ᣑᙇ䛜䛷䛝䜛㓄ิ [x,y,z] 䛸䛔䛖ᙧᘧ䛷సᡂ • ₇⟬Ꮚ [x] 䛷䚸㓄ิ䛾 x ␒┠䛻䜰䜽䝉䝇䛩䜛 • ఱ␒┠䛛(index)䛾ᣦᐃ䛿0䛛䜙ᩘ䛘䜛 Python ධ㛛 49 list1 = [3,4,5] print(list1) => [3, 4, 5] list2 = [“I”, “Love”, “Python”] print(list2[2]) => “Python” list3 = [[1,2], ['A',”B”]] print(list3) => [[1, 2], [“A”, “B”]] print(list3[1][1]) => “B” print(list3[5][0]) => # index out of range 䛾 Error ⾲♧](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-49-2048.jpg&f=jpg&w=240)
![Listᆺ • 㓄ิ㛗䜢ྲྀᚓ: len(X) • 䜸䝤䝆䜵䜽䝖䛾 index 䜢ྲྀᚓ: index(X) • 㓄ิ䛻䜸䝤䝆䜵䜽䝖䜢㏣ຍ: append(x) • 㓄ิ䛛䜙䜸䝤䝆䜵䜽䝖䜢๐㝖: del Python ධ㛛 50 list1 = [1,2,3,4,5] print(list1.index(3)) => 2 print(len(list1)) => 5 list1.append(6) print(list) => [1,2,3,4,5,6] del list2[1] print(list2) => [1,3,4,5,6]](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-50-2048.jpg&f=jpg&w=240)

















![forᩥ䛾᭩ᘧ Python ධ㛛 68 for ኚᩘ in 䝸䝇䝖: ᐇ⾜䛩䜛ฎ⌮㻌 䝸䝇䝖䛾0␒┠䜢ኚᩘ䛻௦ධ䛧䛶ฎ⌮䜢ᐇ⾜ 䝸䝇䝖䛾1␒┠䜢ኚᩘ䛻௦ධ䛧䛶ฎ⌮䜢ᐇ⾜ ..... string = for char in [H,e,l,l,o]: string += char print(string) Hello](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-68-2048.jpg&f=jpg&w=240)







![㛵ᩘ䛾䜃ฟ䛧 • ≉ᐃ䛾ฎ⌮䜢⾜䛖䛯䜑䛾䜃ฟ䛧ཱྀ • 㛵ᩘྡ䛻ᘬᩘ䜢䛘䛶䜃ฟ䛩 • 㛵ᩘ䛿㏉䜚್䜢㏉䛩 Python ධ㛛 76 㛵ᩘྡ(ᘬᩘ) ㏉䜚್ length = len([1,2,3,4,5]) print(length) 5](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-76-2048.jpg&f=jpg&w=240)











![sys.argv • 䝁䝬䞁䝗䝷䜲䞁ᘬᩘ䛻䜰䜽䝉䝇䛩䜛䛻䛿 sys 䝰䝆䝳䞊 䝹䜢 import 䛩䜛 • ᘬᩘ䛜argv䛻䝸䝇䝖䛸䛧䛶᱁⣡䛥䜜䛶䛔䜛 Python ධ㛛 88 import sys print(-‐-‐-‐-‐ args -‐-‐-‐-‐) print(len(sys.argv)) print(sys.argv[0]) print(sys.argv[1]) print(sys.argv[2]) print(-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐) a = int(sys.argv[1]) b = int(sys.argv[2]) print({} + {} = {}.format(a,b,a+b)) $ python test.py 1 5 -‐-‐-‐-‐ args -‐-‐-‐-‐ 3 test.py 1 5 -‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐ 1 + 5 = 6 test.py ฟຊ](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-88-2048.jpg&f=jpg&w=240)












![㎡᭩䛾᧯స • ㎡᭩䛾ᙧᘧ: {key1:value1, key2:value2} • ್䛾ྲྀᚓ: ㎡᭩䜸䝤䝆䜵䜽䝖[key] • ್䛾㏣ຍ(᭦᪂): ㎡᭩䜸䝤䝆䜵䜽䝖[key] = value • ್䛾๐㝖: del ㎡᭩䜸䝤䝆䜵䜽䝖[key] • key୍ぴ䛾ྲྀᚓ: ㎡᭩䜸䝤䝆䜵䜽䝖.keys() • key䜢ᣢ䛳䛶䛔䜛䛛: ㎡᭩䜸䝤䝆䜵䜽䝖.has_key(key) Python ධ㛛 101](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-101-2048.jpg&f=jpg&w=240)
![㎡᭩ᆺ䛾᧯స # ㎡᭩䜸䝤䝆䜵䜽䝖䜢సᡂ d = {Apple:Red, Lemon:Yellow} # key 䛛䜙 Value 䜢ྲྀᚓ d[Apple] 'Red' # ㎡᭩䜸䝤䝆䜵䜽䝖䛻せ⣲䜢㏣ຍ d[Grape] = Purple d {'Grape': 'Purple', 'Lemon': 'Yellow', 'Apple': 'Red'} # ㎡᭩䜸䝤䝆䜵䜽䝖䛾せ⣲䜢๐㝖 del d[Apple] d {'Grape': 'Purple', 'Lemon': 'Yellow'} Python ධ㛛 102](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-102-2048.jpg&f=jpg&w=240)

![㎡᭩ᆺ䛾䝃䞁䝥䝹 • Arp Table 䜢 List 䛸㻌㎡᭩䛷ᐇ(䜽䝷䝇䛺䛧) Python ධ㛛 104 ipList = [] macList = [] def registerMAC1(ip, mac): if ip in ipList: index = ipList.index(ip) macList[index] = mac else: ipList.append(ip) macList.append(mac) def getMAC1(ip): if ip in ipList: index = ipList.index(ip) return macList[index] else: return None arpTable = {} def registerMAC2(ip, mac): arpTable[ip] = mac def getMAC2(ip): return arpTable.get(ip) List䛻䜘䜛ᐇ: ᥈⣴䝁䝇䝖 Order(N) ㎡᭩䛻䜘䜛ᐇ: ᥈⣴䝁䝇䝖 Order(1) List䛻䜘䜛ᐇ ㎡᭩䛻䜘䜛ᐇ](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-104-2048.jpg&f=jpg&w=240)
![䝉䝑䝖 • 㔜」䚸㡰ᗎ䛾䛺䛔」ᩘ䛾䝕䞊䝍䜢ಖᣢ䛩䜛ᆺ • Value 䛾䛺䛔 Map 䛻㏆䛔 Python ධ㛛 105 a = set([1,2,3,4,5,3,2]) print(a) set([1, 2, 3, 4, 5]) a = set(hello) b = set(world) print(a) set(['h', 'e', 'l', 'o']) print(b) set(['d', 'r', 'o', 'w', 'l']) print(a b) set(['l', 'o'])](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-105-2048.jpg&f=jpg&w=240)


![䝍䝥䝹ᆺ䛾᧯స • Tuple 䛾సᡂ: tupleObject = (elem1, elem2,...) • せ⣲䛾ྲྀᚓ: item = tupleObject[index] • 」ᩘ䛾せ⣲䛾ྲྀᚓ: • せ⣲䛾᭦᪂: 䛷䛝䛺䛔 Python ධ㛛 108](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-108-2048.jpg&f=jpg&w=240)
![䝍䝥䝹ᆺ䛾᧯స # Tuple 䛾సᡂ yuiito = (Yuichi, Ito, 2011) yuiito ('Yuichi', 'Ito', 2011) # せ⣲䛾ཧ↷ yuiito[1] 'Ito' yuiito = (Yuichi, Ito, 2011) (name, famName, year) = yuiito year 2011 # せ⣲䛾᭦᪂ yuiito[2] = 2009 Traceback (most recent call last): File stdin, line 1, in module TypeError: 'tuple' object does not support item assignment Python ධ㛛 109](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-109-2048.jpg&f=jpg&w=240)
![䝍䝥䝹ᆺ䝃䞁䝥䝹 • 䝕䞊䝍ᵓ㐀䜢సᡂྍ⬟(䜽䝷䝇䛾⡆᫆∧) • ྍኚ䛺䜸䝤䝆䜵䜽䝖 Python ධ㛛 110 def getMinMax(numList): minNum = numList[0] maxNum = numList[0] for n in numList: if n minNum: minNum = n if n maxNum: maxNum = n return (minNum, maxNum)](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-110-2048.jpg&f=jpg&w=240)










![㏉䜚್ • 㛵ᩘ䛷ฎ⌮䛥䜜䛯䝕䞊䝍䜢䜃ฟ䛧ඖ䛻㏉䛩 • ㏉䜚್䛜䛺䛔ሙྜ䜒䛒䜛(స⏝䛾䜏䛾㛵ᩘ) Python ධ㛛 121 length = len([1,2,3,4,5]) length len([1,2,3,4,5]) 5 ㏉䜚್](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-121-2048.jpg&f=jpg&w=240)







![₇⩦ • day2-‐1.py.zip 䜢䝎䜴䞁䝻䞊䝗䛧ゎ • text = ࠥ 䛸䛧䛶๓ᐃ⩏䛥䜜䛶䛔䜛ᩥᏐิ䛾 ༢ㄒฟ⌧ᩘ䜢䜹䜴䞁䝖䛩䜛䝥䝻䜾䝷䝮䜢సᡂ • 䛂༢ㄒ:ฟ⌧ᩘ䛃䛸䛔䛖䝣䜷䞊䝬䝑䝖䛷ฟຊ䛩䜛 • ᩥᏐิ䛾ศ: ᩥᏐิ.split() -‐ [༢ㄒ䛾㓄ิ] Ⓨᒎ⦅ Python ධ㛛 dog:1 cat:2 ... 129 • ฟ⌧ᩘ䛜ከ䛔༢ㄒ㡰䛻䝋䞊䝖䛧䛶⾲♧䛩䜛 • 䝠䞁䝖䛿ḟ䝨䞊䝆](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-129-2048.jpg&f=jpg&w=240)
![₇⩦⿵㊊ • 䝸䝇䝖䜢䛳䛯⡆᫆䝋䞊䝖䜰䝹䝂䝸䝈䝮 • sort㛵ᩘ䜢䛳䛶䜒ᐇྍ⬟䛰䛜䚸䜎䛰ᩍ䛘䛶䛺䛔 䝔䜽䝙䝑䜽䛜ᚲせ Python ධ㛛 130 list1 = [3,6,2,9,0,1,5] list2 = [] while(0 != len(list1)): maxValue = max(list1) index = list1.index(maxValue) list2.append(maxValue) del list1[index] print(list2) -‐ [9, 6, 5, 3, 2, 1, 0]](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-130-2048.jpg&f=jpg&w=240)












![ᩥᏐิ䛾ฎ⌮2 • ⧞䜚㏉䛧: ᩥᏐิ * ⧞䜚㏉䛧ᅇᩘ • ᩥᏐิ: str(ᩥᏐิ௨እ䛾䜸䝤䝆䜵䜽䝖) • ᩥᏐิ䛾୍ᩥᏐ䜢ྲྀᚓ㻌ᩥᏐิ[index] • ᩥᏐิ䛾⠊ᅖྲྀᚓ: ᩥᏐิ[start:end] Python ධ㛛 143](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-143-2048.jpg&f=jpg&w=240)
![䝃䞁䝥䝹:ᩥᏐิ䛾ฎ⌮2 # ⧞䜚㏉䛧 print(cisco * 3) ciscociscocisco # ᩥᏐิ print(value: + str(5)) # 㠀ᩥᏐิ䜢ᩥᏐิ䛧䛶䛛䜙⤖ྜ value: 5 # ᩥᏐิ䛾୍ᩥᏐ䜢ྲྀᚓ hello world[4] 'o' # ᩥᏐิ䛾୍㒊䜢ྲྀᚓ hello world[4:8] 'o wo' Python ධ㛛 144](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-144-2048.jpg&f=jpg&w=240)

![䝃䞁䝥䝹:ᩥᏐิ䛾ฎ⌮3 # ⨨䛝䛘 hello world.replace(world, python) 'hello python' # ศ hello world.split( ) ['hello', 'world'] # ྵ䜐䛛 hell in hello world True # ⨨ hello world.find(o) 4 hello world.find(X) -‐1 # ᣦᐃ䛧䛯ᩥᏐิ䛜ྵ䜎䜜䛶䛔䛺䛔ሙྜ䛿 -‐1 Python ධ㛛 146](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-146-2048.jpg&f=jpg&w=240)


![ṇつ⾲⌧䛾᭷ຠᛶ • ᩥᏐิ䛜ᩚᩘ䛛䛹䛖䛛ุᐃ䛩䜛䛯䜑䛾䝥䝻 䜾䝷䝮䜢సᡂ䛩䜛 • ṇつ⾲⌧䛾᭷↓䛷䝁䞊䝗㔞䛜ᖜ䛻㐪䛖 Python ධ㛛 149 def isIntegerString(string): numberList = ['1','2','3','4','5','6','7','8','9','0'] if len(string) == 0: return false for char in string: if char not in numberList: return False return True import re def isIntegerString2(string): return re.match('^[d]*$', string) != None ṇつ⾲⌧䛺䛧 ṇつ⾲⌧䛒䜚](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-149-2048.jpg&f=jpg&w=240)

![ṇつ⾲⌧ Python ධ㛛 151 m = re.match(^[d]*$, 12345) print(m) _sre.SRE_Match object at 0x1005bdbf8 m.group() '12345' m.start(), m.end() (0, 5)](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-151-2048.jpg&f=jpg&w=240)



![ㄞ䜏㎸䜏ฎ⌮ • Read – readline(): ୍⾜ㄞ䜏㎸䜏 – readlines(): 䛶䛾⾜䜢 list 䛸䛧䛶ㄞ䜏㎸䜏 Python ධ㛛 155 f = open(file.txt, r) f.readline() 'hello world1n' f.readline() 'hello world2n' f.readlines() ['hello world3n', 'hello world4n'] hello world1 hello world2 hello world3 hello world4 file.txt](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-155-2048.jpg&f=jpg&w=240)
![᭩䛝㎸䜏ฎ⌮ • Write – write(ᩥᏐิ) : ᩥᏐิ䜢᭩䛝㎸䜏 – writelines(ᩥᏐิ䛾䝸䝇䝖) : 䝸䝇䝖䜢᭩䛝㎸䜏 – flush() : 䝞䝑䝣䜯䜢䝕䜱䝇䜽䛻䝣䝷䝑䝅䝳 Python ධ㛛 156 f2 = open(file2.txt, w) f2.write(hello) f2.write(world) f2.write(n) wlist = [abc, def, ghi] f2.writelines(wlist) f2.flush() f2.close() helloworld abcdefghi file2.txt](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-156-2048.jpg&f=jpg&w=240)



![Pickel Python ධ㛛 160 yuichi$ python data = [hello, world, 1, 2, 3] import pickle f = open(dump.txt, w) pickle.dump(data, f) f.close() exit() (lp0 S'hello' p1 aS'world' p2 aI1 aI2 aI3 a. $ python f = open(dump.txt) import pickle data = pickle.load(f) print(data) ['hello', 'world', 1, 2, 3] dump.txt](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-160-2048.jpg&f=jpg&w=240)





![䝟䝇䛾᧯స㛵ᩘ Python ධ㛛 166 os.path.exists(/Users/yuichi) True os.path.isfile(/Users/yuichi) False os.listdir(./) ['dir1', 'file1'] os.mkdir(dir2) os.rmdir(dir2)](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-166-2048.jpg&f=jpg&w=240)




![䝁䝬䞁䝗䛾䜃ฟ䛧 Python ධ㛛 171 • os.system(䝁䝬䞁䝗) : ฟຊ䛜せ䛺ሙྜ • commands.getoutput(䝁䝬䞁䝗) : ฟຊ䛜ᚲせ䛺ሙྜ commands.getoutput(ls).split(n) ['CONFIG_FILE', 'Cisco Mac Support.webloc', 'Desktop', 'Documents', 'Downloads', 'Dropbox', 'Library', 'MATERIAL', 'Movies', 'Music', 'Pictures', 'Public', 'SR', 'STP-‐BA-‐Dispute_Japan-‐ RSVT.pptx', 'VM', 'appProperces', 'bingo.py', 'bingo.pyc', 'dump.txt', 'file.txt', 'file2.txt', 'get-‐fpath-‐info.py', 'mki-‐fp-‐ capture.zip', 'tcpdump', 'tcpdump.zip', 'test', 'unctled folder']](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-171-2048.jpg&f=jpg&w=240)

![exping 䛾ᐇ • 」ᩘ䛾ᐄඛ䛾ping฿㐩⋡䜢 ᐃ Python ධ㛛 173 import commands def ping(dest): result = commands.getoutput(ping -‐c 5 {}.format(dest)) lines = result.split(n) length = len(lines) packetLoss = lines[length -‐2].split()[6] r7 = lines[length -‐1].split()[3].split(/)[1] return (dest, packetLoss, r7) dests = [cisco.com, google.com, yahoo.com] for dest in dests: print(ping(dest)) ('cisco.com', '0.0%', '196.756') ('google.com', '0.0%', '13.317') ('yahoo.com', '0.0%', '262.060')](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-173-2048.jpg&f=jpg&w=240)



![₇⩦ • ᮍᡂ䛾䝻䜾ᢳฟ䝥䝻䜾䝷䝮䜢ᡂ䛥䛫䜛 • 䝞䜾䛿3⟠ᡤ䛻䛒䜛 yuichi$ python log.py -‐f log.log -‐s 2014 Jan 28 12:30:50 -‐e 2014 Jan 29 16:06:15 -‐k port-‐channel -‐f : Python ධ㛛 177 䝣䜯䜲䝹ྡ -‐s : 䝇䝍䞊䝖้ -‐e : ⤊้ -‐k : 䜻䞊䝽䞊䝗 dhcp-‐10-‐141-‐56-‐250:Documents yuichi$ python log.py -‐f log.log -‐s 2014 Jan 28 12:30:50 -‐e 2014 Jan 29 16:06:15 -‐k port-‐channel 2014 Jan 29 12:14:20 N6K-‐2 %ISIS_FABRICPATH-‐5-‐ADJCHANGE: isis_fabricpath-‐default [5150] P2P adj L1 n7kb-‐MKI-‐FP over port-‐channel50 -‐ DOWN (New) on MT-‐0 2014 Jan 29 12:14:20 N6K-‐2 %ISIS_FABRICPATH-‐5-‐ADJCHANGE: isis_fabricpath-‐default [5150] P2P adj L1 n7kb-‐MKI-‐FP over port-‐channel50 -‐ INIT on MT-‐0 2014 Jan 29 12:14:20 N6K-‐2 %ISIS_FABRICPATH-‐5-‐ADJCHANGE: isis_fabricpath-‐default [5150] P2P adj L1 n7kb-‐MKI-‐FP over port-‐channel50 -‐ UP on MT-‐0 2014 Jan 29 12:24:58 N6K-‐2 %ISIS_FABRICPATH-‐5-‐ADJCHANGE: isis_fabricpath-‐default [5150] P2P adj L1 n7kb-‐MKI-‐FP over port-‐channel50 -‐ DOWN (Delete All) on MT-‐0 2014 Jan 29 12:27:01 N6K-‐2 %ISIS_FABRICPATH-‐5-‐ADJCHANGE: isis_fabricpath-‐default [5150] P2P adj L1 n7kb-‐MKI-‐FP over port-‐channel50 -‐ DOWN (New) on MT-‐0 2014 Jan 29 12:27:01 N6K-‐2 %ISIS_FABRICPATH-‐5-‐ADJCHANGE: isis_fabricpath-‐default [5150] P2P adj L1 n7kb-‐MKI-‐FP over port-‐channel50 -‐ INIT on MT-‐0 2014 Jan 29 12:27:01 N6K-‐2 %ISIS_FABRICPATH-‐5-‐ADJCHANGE: isis_fabricpath-‐default [5150] P2P adj L1 n7kb-‐MKI-‐FP over port-‐channel50 -‐ UP on MT-‐0](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-177-2048.jpg&f=jpg&w=240)
![䝁䞊䝗(䝞䜾䛿2⟠ᡤ) Python ධ㛛 178 import sys, os def getTimeString(line): return line[0:20] def parseTime(string): monthDict = {Jan:1, Feb:2, Mar:3, Apr:4, May:5, Jun:6, Jul:7, Aug:8, Sep:9, Oct:10, Nov:11, Dec:12} d1 = string.split( ) d2 = d1[3].split(:) return (int(d1[0]), monthDict[d1[1]], int(d1[2]), int(d2[0]), int(d2[1]), int(d2[2])) def printFile(fname, start, end, keyword): if(not os.path.isfile(fname)): print(File {} isn't exist. exit.format(fname)) sys.exit() f = open(fname) for line in f.readlines(): line = line.rstrip() t = parseTime(getTimeString(line)) within = (start = t) and (t = end) contains = keyword in line if(within and contains): print(line) 䝁䝬䞁䝗ᘬᩘ䛾ึᮇ್ fname = start = 1900 Jan 1 00:00:00 end = 2100 Jan 1 00:00:00 keyword = for index in range(len(sys.argv)): arg = sys.argv[index] if(arg == -‐f): fname = sys.argv[index + 1] elif(arg == -‐s): start = sys.argv[index + 1] elif(arg == -‐k): keyword = sys.argv[index + 1] if(fname == ): s1 = syntax: python log.py -‐f FILENAME -‐s s2 = START_TIME -‐e END_TIME -‐k KEYWORD print(s1 + s2) print(START_TIME, END_TIME, KEYWORD are opcon.) else: printFile(fname, start, end, keyword) 䝻䜾䛾୍⾜䛛䜙㛫䛾ᩥᏐิ䜢ᢳฟ ᢳฟ䛧䛯ᩥᏐิ䜢ẚ㍑ྍ⬟䛺䝍䝥䝹䛻ኚ ᮲௳䛻䝬䝑䝏䛧䛯⾜䜢printฟຊ 䝁䝬䞁䝗ᘬᩘ䜢ㄞ䜏ྲྀ䜛 䝣䜯䜲䝹ྡ䛜䛘䜙䜜䛶䛔䛯䜙䝥䝻䜾䝷䝮䜢ᐇ⾜](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-178-2048.jpg&f=jpg&w=240)


![⏝䛩䜛䝁䝬䞁䝗 Python ධ㛛 181 yuichi$ ssh -‐l guest 10.71.224.172 pass: c1sco123 [guest@fedora172 ~]$ nmap 127.0.0.1 Starcng Nmap 6.01 ( h7p://nmap.org ) at 2014-‐02-‐08 13:05 EST Nmap scan report for fedora172.localdomain (127.0.0.1) Host is up (0.0016s latency). Not shown: 995 closed ports PORT STATE SERVICE 22/tcp open ssh 23/tcp open telnet 25/tcp open smtp 111/tcp open rpcbind 631/tcp open ipp](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-181-2048.jpg&f=jpg&w=240)










![ே㛫䛾ᛮ⪃᪉ἲ • 䛂ㄡ䛜ఱ䜢䛩䜛䛛䛃䛜㔜せ • ㄡ䛻䛹䛖䛔䛖䛂䝯䝑䝉䞊䝆䛃䜢㏦䛳䛯䜙䚸䛹䛖䛔䛖䛂⤖ ᯝ䛃䛜ᚓ䜙䜜䜛䛛 Python ධ㛛 [ෆ㒊ฎ⌮] 䜢䛩䜛 192 [䝯䝑䝉䞊䝆] 䛣䛾䜢䛧䛶䛟䛰䛥䛔 [⤖ᯝ] ୖྖ ᡂᯝ≀䛿䛣䜜䛷䛩䚹 㒊ୗ](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-192-2048.jpg&f=jpg&w=240)







![䜽䝷䝇 • 䜸䝤䝆䜵䜽䝖䛾✀㢮 • 䜽䝷䝇䛿ᒓᛶ䛸䛧䛶䝕䞊䝍䛸ฎ⌮䜢ᣢ䛴 Python ධ㛛 200 ᒓᛶ [䝕䞊䝍] -‐ 䜺䝋䝸䞁䛾㔞 [ฎ⌮] -‐ ㉮䜛 -‐ Ṇ䜎䜛 -‐ ᭤䛜䜛](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-200-2048.jpg&f=jpg&w=240)
![䜲䞁䝇䝍䞁䝇 • 䜽䝷䝇⮬య䛷ฎ⌮䛩䜛䛾䛷䛿䛺䛟䚸䜽䝷䝇䛛䜙స䜛䜲 䞁䝇䝍䞁䝇䛷ฎ⌮䜢䛩䜛 • 㒊ୗ䛸䛔䛖ᴫᛕ(䜽䝷䝇)䛷䛿䛺䛟㒊ୗ䛾䖣䖣(䜲䞁䝇䝍 䞁䝇)䛜䜢䛩䜛 Python ධ㛛 [ෆ㒊ฎ⌮] 䜢䛩䜛 201 [䝯䝑䝉䞊䝆] 䛣䛾䜢䛧䛶䛟䛰䛥䛔 [⤖ᯝ] 䜽䝷䝇: ୖྖ ᡂᯝ≀䛿䛣䜜䛷䛩䚹 䜲䞁䝇䝍䞁䝇: BOB 䜽䝷䝇: 㒊ୗ 䜲䞁䝇䝍䞁䝇: TOM](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-201-2048.jpg&f=jpg&w=240)










![䜽䝷䝇䝃䞁䝥䝹 Python ධ㛛 212 # bingo.py import random class BingoMachine: ballList = [] def __init__(self, min, max): self.ballList = range(min, max + 1) def getBall(self): if(len(self.ballList) == 0): return -‐1 index = int(random.uniform(0, len(self.ballList))) return self.ballList.pop(index) # ᐇ⾜⤖ᯝ from bingo import * b = BingoMachine(1,5) b.getBall() 1 b.getBall() 5 b.getBall() 2 b.getBall() 3 b.getBall() 4 b.getBall() -‐1 䝡䞁䝂䝬䝅䞊䞁 ᩘᏐ㻌n – m 䛾㛫䛾ᩘᏐ䜢䝷䞁䝎䝮䛻㏉䛩 ୍ᗘ㏉䛧䛯್䛿㏉䛥䛺䛔](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-212-2048.jpg&f=jpg&w=240)














![䝬䝑䝥 • ఱ䜒䛺䛔䝬䝑䝥䜢ᥥ⏬ • ⦪䛸ᶓ䛾㛗䛥䜢ᣦᐃྍ⬟ • ⾜(row)ᣦྥ䛷ᥥ⏬䛩䜛 227 +-‐-‐-‐-‐-‐-‐-‐-‐+ | | | | | | +-‐-‐-‐-‐-‐-‐-‐-‐+ 4㝮䛿㻌䛂+䛃 yᗙᶆ0, ್᭱䛿㻌䛂–䛃 xᗙᶆ0, ್᭱䛿㻌䛂|䛃 䛭䜜௨እ䛿✵ⓑ class DQMap: max_x = 0 max_y = 0 def __init__(self, max_xy): (self.max_x, self.max_y) = max_xy def render_map(self): map_string_list = [] for y in range(self.max_y): for x in range(self.max_x): if(y == 0 or y == self.max_y -‐ 1): if(x == 0 or x == self.max_x -‐ 1): map_string_list.append(+) else: map_string_list.append(-‐) else: if(x == 0 or x == self.max_x -‐ 1): map_string_list.append(|) else: map_string_list.append( ) map_string_list.append(n) map_string = .join(map_string_list) print(map_string.rstrip())](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-227-2048.jpg&f=jpg&w=240)




![䝬䝑䝥䛸⏫ே䛾ᐇ Python ධ㛛 232 class DQMap: ... people_map = {} def __init__(self, max_xy, hero_xy, people_paramList): .... for params in people_paramList: (icon, x, y, comment) = params self.people_map[(x,y)] = people.People(icon, x, y, comment) def render_map(self, comment): map_string_list = [] (hx, hy) = self.hero.get_xy() for y in range(self.max_y): for x in range(self.max_x): if((x,y) == (hx, hy)): map_string_list.append(self.hero.get_icon()) elif(self.people_map.has_key((x, y))): p = self.people_map[(x, y)] map_string_list.append(p.get_icon()) 䝁䞁䝇䝖䝷䜽䝍䛷⏫ே䛾 ሗ䜢䝸䝇䝖ᙧᘧ䛷ᣦᐃ ⏫ே䛾䜲䞁䝇䝍䞁䝇䜢సᡂ (x,y)䜢䜻䞊䛻䛧㎡᭩䛷⏫ே䜢⟶⌮ ᥥ⏬䛾㝿䛻䚸x, y 䛻⏫ே䛜䛔䜛䛛☜ㄆ 䜒䛧䛔䜜䜀⏫ே䜢ᥥ⏬䛩䜛](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-232-2048.jpg&f=jpg&w=240)







![䝁䝢䞊䛸ཧ↷ • 䝁䝢䞊: ␗䛺䜛䜸䝤䝆䜵䜽䝖 • ཧ↷: ྠ୍䜸䝤䝆䜵䜽䝖䜢」ᩘ⟠ᡤ䛷⏝ • ཧ↷䛻ᑐ䛧䛶䛿స⏝䛾ὀព䛜ᚲせ Python ධ㛛 240 a = [1,2,3,4] b = a b.append(5) a [1, 2, 3, 4, 5]](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-240-2048.jpg&f=jpg&w=240)



![Expect 䛾ືస • spawn: 䝉䝑䝅䝵䞁䜢㛤ጞ • expect: ᮇᚅ䛩䜛ᶆ‽ධຊ(from 䝸䝰䞊䝖)䜢ᚅ䛴 • send: 䝸䝰䞊䝖䛻ฟຊ䜢㏦䜛 Python ධ㛛 244 dhcp-‐10-‐141-‐56-‐250:~ yuichi$ telnet -‐l yuiito rws1 Trying 10.71.231.245... Connected to rws1.cisco.com. Escape character is '^]'. Password: cisco123 Last login: Thu Jan 23 14:53:37 from dhcp-‐10-‐141-‐56-‐250.cisco.com unknown terminal xterm-‐256color unknown terminal xterm-‐256color [yuiito@rws1 yuiito]$ echo hello test [yuiito@rws1 yuiito]$ ls snmpget.sh test 㟷䛜䝸䝰䞊䝖䛛䜙䛾ᶆ‽ධຊ ㉥䛜䝸䝰䞊䝖䜈䛾ᶆ‽ฟຊ](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-244-2048.jpg&f=jpg&w=240)
![expect 䝃䞁䝥䝹 • Linux 䜈䛾䝻䜾䜲䞁䛸᧯స Python ධ㛛 245 Password: cisco123 yuiito$ echo hello test login-‐to-‐rws1.exp #! /usr/bin/expect spawn telnet -‐l yuiito rws1 expect Password: send cisco123n expect yuiito]$ send touch hellon interact 䜃ฟ䛧 yuichi$ expect login-‐to-‐rws1.exp](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-245-2048.jpg&f=jpg&w=240)



![pexpect 䛾䝃䞁䝥䝹 • rws1 䛻䝻䜾䜲䞁䛧䛶䝣䜯䜲䝹㻌pexpect 䜢సᡂ Python ධ㛛 249 import pexpect child = pexpect.spawn(telnet -‐l yuiito rws1) child.expect(Password:) child.sendline(cisco123) child.expect(yuiito]$) child.sendline(touch pexpect)](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-249-2048.jpg&f=jpg&w=240)
![pexpext䛾䝃䞁䝥䝹 • Nexus䛾タᐃ䜢䝣䜯䜲䝹ྡ䜢᪥䛻䛧䛶FTP䝃䞊䝞䛻ಖᏑ Python ධ㛛 250 import pexpect, datecme def getLines(string): lines = lines = [x.strip() for x in string.split(n)] return filter(lambda x: x!= '', lines) def getHostName(exp): exp.sendline(show hostname) exp.expect(#) return getLines(exp.before)[1] def getToDay(): d = datecme.datecme.today() return {}_{}_{}-‐{}_{}_{}.format(d.year, d.month, d.day, d.hour, d.minute, d.second) def makeFileName(exp): hostName = getHostName(exp) today = getToDay() return {}-‐{}.conf.format(hostName, today) def login(exp, password): exp.expect(Password:) exp.sendline(password) exp.expect(#) exp.sendline(terminal length 0) exp.expect(#) def saveFile(exp, saveFrom, saveTo, vrf, username, password): command = copy {} {}.format(saveFrom, saveTo) exp.sendline(command) exp.expect(Enter vrf) exp.sendline(vrf) exp.expect(Enter username:) exp.sendline(username) exp.expect(Password:) exp.sendline(password) exp.expect(#) child = pexpect.spawn(telnet -‐l admin 10.71.156.229) login(child, cisco) saveTo = p://10.71.224.115/yuiito/ + makeFileName(child) saveFile(child, running-‐config, saveTo, management, anonymous, ) 䝣䜯䜲䝹ྡ䜔䝁䝬䞁䝗䛾⏕ᡂ䛜 python ௵䛫 child.close()](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-250-2048.jpg&f=jpg&w=240)
















































































![map 㛵ᩘ • List 䛾ྛせ⣲䛻ᑐ䛧䛶㛵ᩘ䜢㐺⏝䛩䜛㛵ᩘ map(㛵ᩘ, Python 䝸䝇䝖) ධ㛛 331 N = 10 def double(x): return x * 2 list1 = map(double, range(N)) list2 = [] for i in range(N): list2.append(i*2) print(list1) = [0, 2, 4, 6, 8, 10, 12, 14, 16, 18] print(list2) = [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-331-2048.jpg&f=jpg&w=240)
![filter 㛵ᩘ • List 䛾ྛせ⣲䛻ᑐ䛧䛶㛵ᩘ䜢㐺⏝䛧䚸True 䛻 䛺䛳䛯せ⣲䛾䜏䛷 List 䜢స䜛 Python ධ㛛 332 def isOver0(x): return x 0 list1 = range(-‐5,5) list2 = filter(isOver0, list1) print(list1) = [-‐5, -‐4, -‐3, -‐2, -‐1, 0, 1, 2, 3, 4] print(list2) = [1, 2, 3, 4]](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-332-2048.jpg&f=jpg&w=240)
![lamdaᘧ • 㛵ᩘ䜢䛭䛾ሙ䛷ᐃ⩏䛩䜛䛯䜑䛾ᡭἲ • 㛵ᩘ䜢㏉䛩 Python ධ㛛 333 lamda(ᘬᩘ): ฎ⌮ power = lambda x: x**2 list1 = range(10) map(power, list1) [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] map(lambda x: x**2, range(10)) [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-333-2048.jpg&f=jpg&w=240)


![䝸䝇䝖ෆໟ⾲グ • if ᩥ䜢䛳䛶せ⣲䜢⤠䜜䜛 Python ධ㛛 336 [x**2 for x in range(10) if x % 2 == 0] [0, 4, 16, 36, 64] 1. 䝸䝇䝖䛛䜙അᩘ䛾䜏ᢤ䛝ฟ䛩 2. അᩘ䛾䝸䝇䝖䛻ᑐ䛧䛶 2䛩䜛ฎ⌮䜢㐺⏝ [ฎ⌮ for せ⣲ in 䝸䝇䝖 if ᮲௳ᘧ] 䝸䝇䝖䛾せ⣲䛛䜙᮲௳ᘧ䛜True䛻䛺䜛䜒䛾䛾䜏ᢳฟ 䛭䜜䛻ᑐ䛧䛶ฎ⌮䜢㐺⏝](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-336-2048.jpg&f=jpg&w=240)


![dir • 䜽䝷䝇䜔䝟䝑䜿䞊䝆䛾୰㌟䜢☜ㄆ䛩䜛㛵ᩘ dir(str) ['__add__', '__class__', '__contains__', '__dela7r__', '__doc__', '__eq__', '__format__', '__ge__', '__geta7ribute__', '__gectem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__seta7r__', '__sizeof__', '__str__', '__subclasshook__', '_forma7er_field_name_split', '_forma7er_parser', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'isctle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'parccon', 'replace', 'rfind', 'rindex', 'rjust', 'rparccon', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'ctle', 'translate', 'upper', 'zfill'] Python ධ㛛 339](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-339-2048.jpg&f=jpg&w=240)





![䛾䝎䜴䞁䜢᳨ฟ (䝁䞁䝋䞊䝹⾲♧䜢䝏䜵䝑䜽) ᳨ฟ䛧䛯䜙䚸௨ୗ䛾䝇䜽䝸䝥䝖䜢ᐇ⾜ Python ධ㛛 345 EEM 䛷 peer-‐link MKI-‐VDC2# show file boomlash:if-‐mon.py # VPC 䛾≧ែ☜ㄆ䝁䝬䞁䝗䜢Ⓨ⾜䚹VPC 䛾䝇䝔䞊䝖䜢ᚓ䜛 shVpc = cli (show vpc) shVpcLines = shVpc.splitlines() stateList = shVpcLines[11].split() vpcState = stateList[len(state_list)-‐1] # 䜒䛧VPC䛜 secondary 䛺䜙䚸≉ᐃ䜲䞁䝍䝣䜵䞊䝇䜢ⴠ䛸䛩 if (vpcState == secondary): print(shutdown fp interface) cli(conf t ; int e7/9,e9/9 ; shut)](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-345-2048.jpg&f=jpg&w=240)


![Python ධ㛛 348 if __name__ == __main__: if(len(sys.argv) == 4): fileName = sys.argv[1] sleepInterval = int(sys.argv[2]) sleepCount = int(sys.argv[3]) print(taking Rate logs) writeTime(fileName) writeRateLogs(fileName, sleepInterval, sleepCount) print(taking FP logs) writeTime(fileName) writeFPLogs(fileName) writeOldLogs(fileName) def makeEcho(string, fileName): return 'echo ' + string + ' boomlash:' + fileName def makeShow(string, fileName): return string + boomlash: + fileName def makeCombi(string, fileName): #䝁䝬䞁䝗䛾้, ྡ๓, ⤖ᯝ䜢᭩䛟 return makeShow(show clock, fileName) + ; + makeEcho(# + string, fileName) + ; + makeShow(string, fileName) + ; + makeEcho(, fileName) def writeRateLogs(fileName, interval, n): # Rate 䜢ᐃᮇ ᐃ for i in range(0, n): cli(makeCombi(SHOW_RATE_BETWEEN_N6K_AND_N7K, fileName)) cli(makeCombi(SHOW_RATE_AT_PEER_LINK, fileName)) cli(makeEcho(, fileName)) cme.sleep(n) def writeFPLogs(fileName): # 䝁䝬䞁䝗䛾⨶ิ cli(makeCombi(SHOW_FP_ISIS_ROUTE, fileName)) cli(makeCombi(SHOW_FWM_L2MP_NEXT_HOP, fileName)) cli(makeCombi(SHOW_FWM_HWSTM, fileName)) cli(makeCombi(SHOW_FWM_L2MP_ROUTE, fileName)) cli(makeCombi(SHOW_FWM_MAC_HSRP_VMAC, fileName)) cli(makeCombi(SHOW_FWM_L2MP_VLAN, fileName)) cli(makeCombi(SHOW_FWM_L2MP_TOPO_0, fileName))](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonintroduction-141212181657-conversion-gate01%2f75%2fPython-4-348-2048.jpg&f=jpg&w=240)







