timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | import os |
| 4 | import pexpect |
| 5 | import re |
| 6 | import time |
| 7 | import sys |
| 8 | |
| 9 | def pronto(ip, user, passwd): |
| 10 | print "Connecting to Pronto switch" |
| 11 | child = pexpect.spawn("telnet " + ip) |
| 12 | i = child.expect(["login:", "CLI#",pexpect.TIMEOUT]) |
| 13 | if i == 0: |
| 14 | print "Username and password required. Passing login info." |
| 15 | child.sendline(user) |
| 16 | child.expect("Password:") |
| 17 | child.sendline(passwd) |
| 18 | child.expect("CLI#") |
| 19 | print "Logged in, getting flowtable." |
| 20 | child.sendline("flowtable brief") |
| 21 | for t in range (9): |
| 22 | t2 = 9 - t |
| 23 | print "\r" + str(t2) |
| 24 | sys.stdout.write("\033[F") |
| 25 | time.sleep(1) |
| 26 | print "Scanning flowtable" |
| 27 | child.expect("Flow table show") |
| 28 | count = 0 |
| 29 | while 1: |
| 30 | i = child.expect(['17\d\.\d{1,3}\.\d{1,3}\.\d{1,3}','CLI#',pexpect.TIMEOUT]) |
| 31 | if i == 0: |
| 32 | count = count + 1 |
| 33 | elif i == 1: |
| 34 | print "Pronto flows: " + str(count) + "\nDone\n" |
| 35 | break |
| 36 | else: |
| 37 | break |
| 38 | |
| 39 | def cisco(ip,user,passwd): |
| 40 | print "Establishing Cisco switch connection" |
| 41 | child = pexpect.spawn("ssh " + user + "@" + ip) |
| 42 | i = child.expect(["Password:", "CLI#",pexpect.TIMEOUT]) |
| 43 | if i == 0: |
| 44 | print "Password required. Passing now." |
| 45 | child.sendline(passwd) |
| 46 | child.expect("#") |
| 47 | print "Logged in. Retrieving flow table then counting flows." |
| 48 | child.sendline("show openflow switch all flows all") |
| 49 | child.expect("Logical Openflow Switch") |
| 50 | print "Flow table retrieved. Counting flows" |
| 51 | count = 0 |
| 52 | while 1: |
| 53 | i = child.expect(["nw_src=17","#",pexpect.TIMEOUT]) |
| 54 | if i == 0: |
| 55 | count = count + 1 |
| 56 | elif i == 1: |
| 57 | print "Cisco flows: " + str(count) + "\nDone\n" |
| 58 | break |
| 59 | else: |
| 60 | break |
| 61 | |
| 62 | if __name__ == "__main__": |
| 63 | usage_msg = "<Switch brand> <IP> <Username> <Password>\n" |
| 64 | usage_msg = usage_msg + "\nCurrently supported switches:\n" |
| 65 | usage_msg = usage_msg + "Pronto | Cisco\n" |
| 66 | usage_msg = usage_msg + "\nShorthand commands: \n" |
| 67 | usage_msg = usage_msg + "SDNIP : Runs \"Pronto 10.128.0.63 admin admin\" and \"Cisco 10.128.0.30 admin onos_test\" \n" |
| 68 | |
| 69 | if len(sys.argv) == 2: |
| 70 | if sys.argv[1].lower() == "sdnip": |
| 71 | switch = sys.argv[1] |
| 72 | elif len(sys.argv) < 5 or (sys.argv[1] == "-h" or sys.argv[1] == "--help"): |
| 73 | print(usage_msg) |
| 74 | exit(0) |
| 75 | else: |
| 76 | switch = sys.argv[1] |
| 77 | ip = sys.argv[2] |
| 78 | user = sys.argv[3] |
| 79 | passwd = sys.argv[4] |
| 80 | |
| 81 | if switch.lower() == "sdnip": |
| 82 | pronto("10.128.0.63","admin","admin") |
| 83 | cisco("10.128.0.30","admin","onos_test") |
| 84 | elif switch.lower() == "pronto": |
| 85 | pronto(ip,user,passwd) |
| 86 | elif switch.lower() == "cisco": |
| 87 | cisco(ip,user,passwd) |