Exemplo n.º 1
0
        public virtual void Connect(TargetInfo target)
        {
            var connectionPort = target.GetPort(ConnectionType.Telnet);
            this.TelnetConnection = new TelnetConnection(target.GetAddress(), connectionPort);
            this.TelnetConnection.TimeOutLoginMs = 1000;
            this.TelnetConnection.TimeOutReadMs = 30000;
            this.TelnetConnection.CiscoLogin(target.credentials.GetUserName(), target.credentials.GetPassword());

            string adminPass = target.credentials.GetAdminPassword();
            if (!String.IsNullOrEmpty(adminPass))
                this.TelnetConnection.CiscoEnable(adminPass);
        }
Exemplo n.º 2
0
        public static CiscoIOSVersion CiscoGetVersion(TelnetConnection tc)
        {
            CiscoIOSVersion retVal = null;
            string output = tc.CiscoCommand("show version");
            string[] lines = output.Split('\r', '\n');
            retVal = new CiscoIOSVersion();
            retVal.VersionString = "0.0";
            retVal.HostName = retVal.Architecture = "undefined";

            foreach (string line in lines)
            {
                if (line.StartsWith("IOS") || line.StartsWith("Cisco IOS"))
                {
                    string verString = " Version ";
                    int posVer = line.IndexOf(verString, StringComparison.OrdinalIgnoreCase);
                    if (posVer >= 0)
                    {
                        posVer += verString.Length;
                        verString = line.Substring(posVer);
                        string[] verStringCut = verString.Split(' ', '\t', ',');
                        retVal.VersionString = verStringCut[0];
                        retVal.OSName = "IOS";
                    }
                }

                if (line.IndexOf(" uptime is ", StringComparison.OrdinalIgnoreCase) >= 0)
                {
                    int posSpace = line.IndexOfAny(new char[] { ' ', '\t' });
                    retVal.HostName = posSpace != 0 ? line.Substring(0, posSpace) : "unknown";
                }

                if ((line.IndexOf(" bytes of memory", StringComparison.OrdinalIgnoreCase) >= 0))
                {
                    retVal.Architecture = line;
                }
            }

            return retVal;
        }
Exemplo n.º 3
0
 public static IList<NetworkInterface> CiscoGetInterfaces(TelnetConnection tc)
 {
     List<NetworkInterface> retList = new List<NetworkInterface>();
     NetworkInterface curif = null;
     char[] lineseps = { '\r', '\n' };
     char[] fieldseps = { ' ', '\t' };
     string output = tc.CiscoCommand("show interfaces");
     string[] lines = output.Split(lineseps, StringSplitOptions.RemoveEmptyEntries);
     foreach (string line in lines)
     {
         string[] ifields = line.Split(fieldseps, StringSplitOptions.RemoveEmptyEntries);
         if (!char.IsWhiteSpace(line[0]))
         {
             if (curif != null)
             {
                 retList.Add(curif);
             }
             curif = new NetworkInterface();
             curif.Name = ifields[0];
         }
         else
         {
             if (String.Equals(ifields[0], "Internet", StringComparison.OrdinalIgnoreCase) &&
                 String.Equals(ifields[1], "address", StringComparison.OrdinalIgnoreCase) &&
                 String.Equals(ifields[2], "is", StringComparison.OrdinalIgnoreCase))
             {
                 string tmpIp = ifields[3];
                 string[] tmpIpParts = tmpIp.Split('/');
                 curif.IpAddress = tmpIpParts[0];
             }
             if (String.Equals(ifields[0], "Hardware", StringComparison.OrdinalIgnoreCase) &&
                 String.Equals(ifields[1], "is", StringComparison.OrdinalIgnoreCase))
             {
                 string tmpMac = "";
                 for (int i = 2; i < ifields.Length - 2; i++)
                 {
                     if (String.Equals(ifields[i], "address", StringComparison.OrdinalIgnoreCase) &&
                         String.Equals(ifields[i + 1], "is", StringComparison.OrdinalIgnoreCase))
                     {
                         tmpMac = ifields[i + 2];
                         break;
                     }
                 }
                 if (tmpMac.Length == 14)    // xxxx.xxxx.xxxx MAC format
                 {
                     tmpMac = tmpMac.Substring(0, 2) + ":" +
                              tmpMac.Substring(2, 2) + ":" +
                              tmpMac.Substring(5, 2) + ":" +
                              tmpMac.Substring(7, 2) + ":" +
                              tmpMac.Substring(10, 2) + ":" +
                              tmpMac.Substring(12, 2);
                 }
                 if (!String.IsNullOrEmpty(tmpMac))
                     curif.MacAddress = tmpMac;
             }
         }
     }
     if (curif != null)
     {
         retList.Add(curif);
     }
     return retList;
 }
Exemplo n.º 4
0
 public LineObjectCollector(TelnetConnection telnetConnection)
 {
     this.TelnetConnection = telnetConnection;
 }
Exemplo n.º 5
0
        public static CiscoIOSVersion CiscoGetVersion(TelnetConnection tc)
        {
            CiscoIOSVersion retVal = null;
            string output = tc.CiscoCommand("show version");
            string[] lines = output.Split('\r', '\n');
            foreach (string line in lines)
            {
                if (line.StartsWith("IOS"))
                {
                    string verString = " Version ";
                    int posVer = line.IndexOf(verString, StringComparison.OrdinalIgnoreCase);
                    if (posVer >= 0)
                    {
                        posVer += verString.Length;
                        verString = line.Substring(posVer);
                        string[] verStringCut = verString.Split(' ', '\t', ',');
                        retVal = new CiscoIOSVersion();
                        retVal.VersionString = verStringCut[0];
                        retVal.OSName = "IOS";
                    }
                }

                if (line.IndexOf(" uptime is ", StringComparison.OrdinalIgnoreCase) >= 0)
                {
                    int posSpace = line.IndexOfAny(new char[] { ' ', '\t' });
                    retVal.HostName = line.Substring(0, posSpace);
                }

                if ((line.IndexOf(" processor ", StringComparison.OrdinalIgnoreCase) >= 0) &&
                    String.IsNullOrEmpty(retVal.Architecture))
                {
                    string[] tokens = line.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
                    for (int i = 0; i < tokens.Length; i++)
                    {
                        if (String.Equals(tokens[i], "processor", StringComparison.OrdinalIgnoreCase) && (i > 0))
                        {
                            string cpuStr = tokens[i - 1];
                            int lenCpuStr = cpuStr.Length;
                            if ((cpuStr[0] == '(') && (cpuStr[lenCpuStr - 1] == ')'))
                                cpuStr = cpuStr.Substring(1, lenCpuStr - 2);
                            retVal.Architecture = cpuStr;
                            break;
                        }
                    }
                }
            }
            return retVal;
        }