Tuesday, March 29, 2016

Playing With Python - A bit more!


 I´m working in a Python script and my objective is:


1-Access to a Wireless Lan Controller;
2-Perform some commands, for example, "show ap summary";
3-Save the output in a file;
4-Open the file;
5-Get the AP hostname and put in a variable.;
6-Perform the command "show ap auto-rf 802.11b 'AP name';
7-Save the output in a file;
8-Get information about Wireless
9-Show it in some format;

So far, I have accomplished 4 out of 9 steps. My script looks like this:

#!/usr/bin/python
from Exscript.util.interact import read_login
from Exscript.protocols import SSH2
from Exscript import Account

user    = "xxxxx"
passwd  = "xxxxxxxx"
wlc_ip  = "xxxxxxxxxxxxx"

account = Account(user,passwd)
conn = SSH2()
conn.connect(wlc_ip)
conn.login(account)

conn.execute(user)
conn.execute(passwd)
conn.execute("config paging disable")
conn.execute("show ap summary")

print conn.response

f = open(wlc_ip, 'w+')


f.write(conn.response)

conn.send("logout")
conn.send("y")
conn.close()

 I am learning about Python file handling in order for accomplish what I want to do. 
 As soon as I have a progress, I will put here. If someone out there had any hint, will be very very nice!

************************************************************************

Well, I didn´t to much progress but I did some.

Now I am at step 5!

First, this piece of code, gives me only the APs hostname:


fin = open("fin.txt")
fout = open("fout.txt","w+")
for line in fin:
     if 'AIR'  in line:
         list = line.split(" ")
         fout.write(list[0]+'\n')



fin.close()
fout.close()

What it does is, it open the fin.txt file which is the file I got from WLC and Analise. If AIR in line, which means, only those lines with APs, separate by spaces and take the first element. As AP hostname is the first element, I am storing on the fout file the hostname of all APs.

ap = open("fout.txt")

some_ap = ap.readlines()
print some_ap[1]


ap.close()

The second part of code, takes the file created above and create a list in which each index will actually a hostname. The idea now is call the command
"show ap auto-rf 802.11b 'AP name'" and pass on each AP as argument. This command must be executed one by one, so , that´s why I need to be specific.

For sure a developer would lauch at my code and I believe it is too bad but for now it´s all I have.

When everything is working fine, I will try to be more elegant. 

+++++++++++++++++++++++++++++++++++++++++++++

May 28, 2016

A few days ago I have finished my script. I did it with a little help from a friend but I did the most part of it, so I am posting it as my code:

 Recapping my objectives:

1-Access to a Wireless Lan Controller;
2-Perform some commands, for example, "show ap summary";
3-Save the output in a file;
4-Open the file;
5-Get the AP hostname and put in a variable.;
6-Perform the command "show ap auto-rf 802.11b 'AP name';
7-Save the output in a file;
8-Get information about Wireless
9-Show it in some format;

Well, step 1 was accomplished using Exscript. First I tried with Paramiko directly but the odd behavior of Cisco WLC asking for user login twice, prevent me succeed with Paramiko.
 Exscript was a big asset once it allows me login in and preform commands.

 Step 4 was solved the same way. The "conn.execute" Exscript´s function do the job like a charm.

Steps 3, 4 and 5 was accomplished using Python file handling ability. Not that complicated.

The piece of code responsible for that, looks like this:


for line in extract_hostname:
    if 'System Name' in line:
        list = line.split(" ")
        show_hostname.write(list[2]+'\n')
        print ("The hostname is : " + list[2])
        i=list[2].strip()

First, the script performed the command conn.execute("show sysinfo"), because this command will bring the WLC hostname somewhere.

The "show sysinfo" command output was stored in a file called "wlc_hostname"

get_hostname  = open("wlc_hostname", "w+")
get_hostname.write(conn.response)

get_hostname.close()

Then, I created an variable to receive the file:

extract_hostname = open("wlc_hostname")

show_hostname = open("hostname", "w+")

Finally, the code above work on this file in order to find the hostname.

for line in extract_hostname makes the script look line by line. The WLC hostname is shown somewhere like this:

System Name...................................... XXXXXXXXXX

When ths script gets to this line, it splits the line and get the piece that contains the hostname. Stores the hostname in a list for future usage.

Well, all this was only to get the WLC hostname. As I intend to run this script among many WLC, I think it is a good idea to have each file as WLC hostname.

Therefore, my final goal was extract KPIs from WLC and save it in a CSV file. Then, the same methodology was applied:

The command "Show ap auto-rf 802.11a/b 'ap hostname' is passed to WLC via "conn.execute", the hole output was written in a file with "conn.response" and then the file was edited with the piece of code bellow:





x = []
fin = open("XXXXXXXXXX")
fout = open("wlc_ip"+"-" + timestamp + ".txt","w+")

print "Creating a list of hostnames..."

for line in fin:
      if 'AIR'  in line:
         list = line.split(" ")
         x.append(list[0]) 


for line in x:
         conn.execute("show ap auto-rf 802.11a " + line)
         fout.write(conn.response)         



fin.close()
fout.close()

The code takes the output row from "show ap summary", read the row, find out the word 'AIR' and sttract the hostname, storing it in a variable called 'line'.
Once we get the hostname, a for statement run over the list "x" using the command "show ap auto-rf 802.11a and instead using the AP´s hostname, use the variable "line". For each loop, the information from one AP was got.

  The result is a very long file separated by AP. After that a similar script gets the KPIs in CSV format, as we can see bellow:

APhostname,RADIO_TYPE_80211a,0,0,1,2,1,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,2,0
APhostname,RADIO_TYPE_80211a,0,0,0,1,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0
APhostname,RADIO_TYPE_80211a,0,0,2,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
APhostname,RADIO_TYPE_80211a,0,0,0,1,1,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0
APhostname,RADIO_TYPE_80211a,0,0,0,1,1,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0
APhostname,RADIO_TYPE_80211a,0,0,1,10,1,0,0,0,2,2,4,2,0,0,0,0,0,0,2,6,2,0
APhostname,RADIO_TYPE_80211a,0,0,1,5,1,0,0,1,1,1,2,0,0,0,0,0,0,1,1,3,0,0
APhostname,RADIO_TYPE_80211a,0,0,0,3,1,0,0,1,0,0,0,1,1,0,0,0,0,1,0,0,2,0
APhostname,RADIO_TYPE_80211a,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
APhostname,RADIO_TYPE_80211a,0,0,13,2,1,0,0,0,0,2,0,0,0,0,0,0,0,0,0,2,0,0
APhostname,RADIO_TYPE_80211a,0,0,2,12,1,0,0,1,0,2,4,3,1,0,1,0,0,1,1,4,5,1

Each comma-separated value represents one KPI retrieved from the WLC. PKIs like air time utilization, "AP Name","Radio Type","Receive Utilization","Transmit Utilization","Channel Utilization","Attached Clients","Power Level","RSSI", "SNR",etc.

Why don´t use Cisco Prime instead? Those information can be shown with reports, doesn´t it? Yes, it is.

What I am looking for a system to control Wireless network behavior using firstly command line interface. With one command I can see how things are going on.I don´t need to access Prime GUI and export reports. Someone could say, then schedule reports! The problem with reports schedule is the period of one hour per report. In one hour we can have a lot of changes in the network.
 The script can get information at any time I want, I can reduce the frequency and this can be every 10 minutes or every each 5 seconds. I can store this results in a database for example. Actually, there is no limit at all!!



No comments:

Post a Comment