Wrote some code to pull down serial numbers from cisco ip phones figured it might help others down the line. Also has some string manipulation and http streaming stuff going on.

private void toolStripButton1_Click(object sender, EventArgs e)
        {
            try
            {
                using(StreamReader sr = new StreamReader("phoneIP.txt")) 
                {
                    String line;
                    while((line = sr.ReadLine()) != null) 
                    {
                        parseSerial(getPage(line), line);
                    }
                }
                writeSerials();
            }
            catch (Exception ex) { MessageBox.Show("Error opening file phoneIP.txt:\n" + ex.Message); };
        }
        internal ArrayList serialNumbers = new ArrayList();

        internal StringBuilder getPage(string IP)
        {
            StringBuilder sb = new StringBuilder();
            WebRequest startRequest = WebRequest.Create("http://" + IP + "/CGI/Java/Serviceability?adapter=device.statistics.device");
            WebResponse getResponse = startRequest.GetResponse();
            Stream recieveStream = getResponse.GetResponseStream();
            Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
            StreamReader readStream = new StreamReader(recieveStream, encode);
            string response = readStream.ReadToEnd();
            sb.Append(response);
            readStream.Close();
            getResponse.Close();
            return sb;
        }

        internal void parseSerial(StringBuilder page, string IP)
        {
            string fullText = page.ToString();
            int location = fullText.IndexOf("Serial Number");
            page.Remove(0, location);
            fullText = page.ToString();
            int endLocation = fullText.IndexOf("");
            int stringLength = fullText.Length - endLocation;
            page.Remove(endLocation, stringLength);
            fullText = page.ToString();
            int finalRemove1 = fullText.IndexOf("");
            int finalRemove2 = fullText.IndexOf("");
            page.Remove(finalRemove1, finalRemove2 - finalRemove1 + 3);
            page.Insert(finalRemove1, ",");
            serialNumbers.Add(page.ToString()+","+IP);
        }

        internal void writeSerials()
        {
            TextWriter tw = new StreamWriter("serials.csv");
            foreach (string phoneInfo in serialNumbers)
            {
                tw.WriteLine(phoneInfo);
            }
            tw.Close();
        }

trackback image tell a friend image Permalink Image

Post Tags: microsoft  code  cisco  c# 


Next entry: SharePoint Calendar Reminders Previous entry: Relink outlook to MAILTO:



Comments: (0)

Post a comment

Name:
(Required)

Email:
(Required)

URL:

Smileys

Remember my personal information

Notify me of follow-up comments?

Submit the word you see below: