public SshShellTerminal(string host, string user, string password = null)
 {
     this.host = host;
     this.user = user;
     this.password = password;
     port = 22;
     sshShell = string.IsNullOrEmpty(password) ? new SshShell(host, user) : new SshShell(host, user, password);
 }
 public override bool CreateReader(ref Exception error)
 {
     try
     {
         Ssh = new SshShell(Recorder.RemoteHost, Recorder.User, Recorder.Password);
         Ssh.Connect(Port <= 0 ? 22 : Port, int.MaxValue);
         Keyword = DateTime.Now.Ticks + "." + Guid.NewGuid() + "." + random.Next();
         if (!SendCommand(ref error))
             return false;
         WaitBegin = true;
         sshReader = new StreamExpect
             {
                 Data = Ssh,
                 Expect = Pattern,
                 ReadTimeout = ReadTimeout,
                 Stream = Ssh.IO
             };
         sshReader.OnReadTimeout += sshReader_OnReadTimeout;
         return true;
     }
     catch (Exception e)
     {
         error = e;
     }
     return false;
 }
 public void Dispose()
 {
     if (sshShell != null)
     {
         try
         {
             sshShell.Close();
         }
         finally
         {
             try
             {
                 sshShell.GetStream().Close();
             }
             finally
             {
                 sshShell = null;
             }
         }
     }
 }
Exemplo n.º 4
0
        private void timer1_Tick(object sender, System.Timers.ElapsedEventArgs e)
        {
            L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() --> Timer tetiklenme ani: " + e.SignalTime.ToLongTimeString());

            timer1.Enabled = false;
            String line = "";
            Rec rec = new Rec();
            recList = new ArrayList();
            SshShell se = null;
            StringReader strR = null;

            try
            {
                L.Log(LogType.FILE, LogLevel.INFORM, " timer1_Tick() --> SshShell nesnesi icin. Host: " + remoteHost + ", User: "******", Pass: *******");

                se = new SshShell(remoteHost, user);
                if (!String.IsNullOrEmpty(password))
                    se.Password = password;

                //L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() --> Started to connect and parse lines.");
                if (se != null)
                {
                    L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() --> SshShell nesnesi Üretildi.");
                }
                else
                {
                    L.Log(LogType.FILE, LogLevel.INFORM, " timer1_Tick() --> SshShell nesnesi Üretilemedi.");
                }

                if (!se.Connected)
                {
                    try
                    {
                        se.Connect(22, 2000);
                        L.Log(LogType.FILE, LogLevel.INFORM, " timer1_Tick() --> Baglanti acildi.");
                    }
                    catch (Exception ex)
                    {
                        L.Log(LogType.FILE, LogLevel.ERROR, " timer1_Tick() --> Connection problem. Hata : " + ex.ToString());
                    }
                }
                else
                {
                    L.Log(LogType.FILE, LogLevel.INFORM, " timer1_Tick() --> Zaten kurulu olan Baglanti ile devam ediliyor.");
                }

                if (se.Connected)
                {
                    L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() --> Baglanti suan acik.");
                    if (se.ShellOpened)
                    {
                        L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() --> Shell suan acik.");
                        if (se.ShellConnected)
                        {
                            L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() --> Shell Baglanti kurdu.");
                            try
                            {
                                //We can collect data.
                                strR = GetData(remoteHost, user, password, se);
                            }
                            catch (Exception ex)
                            {
                                L.Log(LogType.FILE, LogLevel.ERROR, " timer1_Tick() --> Data Getirirken hata ile karsilasildi.");
                                L.Log(LogType.FILE, LogLevel.ERROR, " timer1_Tick() --> Hata: " + ex.ToString());
                            }
                        }
                    }
                }
                else
                {
                    L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() --> Baglanti kopuk!!!!");
                }

                while ((line = strR.ReadLine()) != null)
                {
                    L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() --> Line to Parse : " + line);
                    rec.Description = line;
                    //String[] arr = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    String[] arr = SpaceSplit(line, false);
                    if (line != "show ip dhcp binding")
                    {
                        try
                        {
                            rec.Description = line;
                            rec.CustomStr1 = arr[0];
                            rec.CustomStr2 = arr[1];
                            rec.CustomStr3 = arr[2];
                            rec.CustomStr4 = arr[3];
                            rec.Datetime = DateTime.Now.ToString(dateFormat);//

                            L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() --> Datetime : " + rec.Datetime);
                            L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() --> Customstr1 : " + rec.CustomStr1);
                            L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() --> Customstr2 : " + rec.CustomStr2);
                            L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() --> Customstr3 : " + rec.CustomStr3);
                            L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() --> Customstr4 : " + rec.CustomStr4);

                            recList.Add(rec);
                        }
                        catch (Exception exception)
                        {
                            L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() --> ERROR: " + exception.Message);
                        }
                    }
                }

                L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() --> cekilen veriler tarihlerine bakilarak kaydedilecek.");
                L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() --> Kalinan LastPosition: " + Local_LastPosition);

                lastDate = new DateTime();
                bool lastPositionSetted = DateTime.TryParse(Local_LastPosition, out lastDate);
                rec.LogName = "CiscoDhcpV_1_0_0Recorder";
                foreach (Rec r in recList)
                {
                    if (!lastPositionSetted)
                    {
                        //İlk kayitlar.
                        CustomServiceBase s = base.GetInstanceService("Security Manager Remote Recorder");
                        s.SetData(Dal, virtualhost, r);
                        L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() --> LastPosition is null. Veri atamasi gerceklestirildi.");
                    }
                    else
                    {
                        //Daha önce kayit alinmis.
                        if (Convert.ToDateTime(r.Datetime) > lastDate)
                        {
                            //Değisen kayit olmus. Veritabanina eklenmeli.
                            CustomServiceBase s = base.GetInstanceService("Security Manager Remote Recorder");
                            s.SetData(Dal, virtualhost, r);
                            L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() --> Record Date daha büyük. Veri atamasi gerceklestirildi. : " + r.Datetime);
                        }
                        else
                        {
                            //Tarih ayni. Kayit islemi yapilmayacak.
                            L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() --> Record Date daha kücük. Kaydedilmedi!. Tarih: " + r.Datetime);
                        }
                    }
                }

                L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() --> En büyük tarih belirleniyor.");
                maxDate = lastDate.ToString();

                foreach (Rec r in recList)
                {
                    if (Convert.ToDateTime(r.Datetime) > Convert.ToDateTime(maxDate))
                    {
                        maxDate = r.Datetime;
                    }
                }

                CustomServiceBase ser = base.GetInstanceService("Security Manager Remote Recorder");
                ser.SetReg(ID, maxDate, "", "", "");
                Local_LastPosition = maxDate;
                L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() --> Yeni LastPosition atandi. LastPostion: " + Local_LastPosition);

            }
            catch (Exception ex)
            {
                L.Log(LogType.FILE, LogLevel.ERROR, " timer1_Tick() --> Recorder Main Exception: " + ex.ToString());
            }
            finally
            {
                if (se != null)
                {
                    if (se.Connected)
                    {
                        se.Close();
                        L.Log(LogType.FILE, LogLevel.INFORM, " timer1_Tick() --> Baglanti kesildi.");
                    }
                }

                se = null;
                tempDate = "";
                tempMac = "";
                timer1.Enabled = true;
                L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() --> Timer'in isi bitti. Time: " + DateTime.Now.ToLongTimeString());
                L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() --> Bir Sonraki Tetikleme " + timer_interval / 60000 + " dakika sonra gerceklesecektir.");
                L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() --> =============================================== ");
                recList.Clear();
            }
            se.Close();
        }
Exemplo n.º 5
0
        private StringReader GetData(string host, string user, string password, SshShell se)
        {
            StringReader strR = null;
            int asc = 012;
            string commandToRead = "show ip dhcp binding";
            Stream st = null;
            StreamReader sr = null;
            StringBuilder strB = new StringBuilder();

            Stream st1 = null;
            StreamReader sr1 = null;
            StringBuilder strB1 = new StringBuilder();

            bool morePage = false;

            IConnector sshConn = ConnectionManager.getConnector("SSH");

            sshConn.SetConfigData(L);
            sshConn.Init();
            string stdOut;

            if (!sshConn.initConnection(host, user, password, password, 0))
            {
                return new StringReader("");
            }

            string defaultCommand = "terminal length 0";
            sshConn.write(defaultCommand);
            L.Log(LogType.FILE, LogLevel.DEBUG, " GetData() --> Default Komut calistirildi : " + defaultCommand);

            string result = sshConn.read(2000);
            result = result.Replace('\0', ' ');
            L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() | GetData() --> Read Line Default : " + result);

            while (true)
            {
                sshConn.write(commandToRead);

                L.Log(LogType.FILE, LogLevel.DEBUG, " GetData() --> Komut calistirildi : " + commandToRead);

                stdOut = sshConn.read(10000);

                L.Log(LogType.FILE, LogLevel.DEBUG, " GetData() --> Komut cevabı : " + stdOut);

                int cnt = 0;
                int asci = 0;
                string lineStr = "";
                string previousStr = "";

                StringReader reader = new StringReader(stdOut);

                while ((asci = reader.Read()) != 0)
                {
                    char karakter = Convert.ToChar(asci);//

                    if (karakter == '#')
                    {
                        if (cnt > 0)
                        {
                            break;
                        }
                        cnt++;
                    }

                    if (karakter != '\n')
                    {
                        lineStr += karakter.ToString();
                    }
                    else
                    {
                        L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() | GetData() --> Read Line : " + lineStr);
                        if (lineStr.ToLower().Contains("more"))
                        {
                            L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() | GetData() --> There is more ip to be read.");
                            L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() | GetData() --> We will read more page.");
                            L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() | GetData() --> Last ip line we have got : " + previousStr);
                            morePage = true;
                        }
                        else
                        {
                            previousStr = lineStr;
                            strB.Append(lineStr);
                        }
                        lineStr = "";
                    }
                }

                L.Log(LogType.FILE, LogLevel.DEBUG, " GetData() --> End of While. ");

                if (morePage)
                {
                    int s = 32;
                    commandToRead = "show ip arp | ";
                    morePage = false;
                }
                else
                {
                    L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() | GetData() --> All ip has got.");
                    break;
                }

            }

            L.Log(LogType.FILE, LogLevel.DEBUG, " GetData() --> End of While true. ");
            strR = new StringReader(stdOut);

            sshConn.dropConnection();

            return strR;
        }
        private void timer1_Tick(object sender, System.Timers.ElapsedEventArgs e)
        {
            L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() --> Timer tetiklenme anı: " + e.SignalTime.ToLongTimeString());

            timer1.Enabled = false;
            String line = "";
            Rec rec = new Rec();
            recList = new ArrayList();
            SshShell se = null;
            StringReader strR = null;

            try
            {
                L.Log(LogType.FILE, LogLevel.INFORM, " timer1_Tick() --> SshShell nesnesi için. Host: " + remoteHost + ", User: "******", Pass: *******");

                se = new SshShell(remoteHost, user);
                if (!String.IsNullOrEmpty(password))
                    se.Password = password;

                //L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() --> Started to connect and parse lines.");
                if (se != null)
                {
                    L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() --> SshShell nesnesi Üretildi.");
                }
                else
                {
                    L.Log(LogType.FILE, LogLevel.INFORM, " timer1_Tick() --> SshShell nesnesi Üretilemedi.");
                }

                if (!se.Connected)
                {
                    try
                    {
                        se.Connect(22, 2000);
                        L.Log(LogType.FILE, LogLevel.INFORM, " timer1_Tick() --> Bağlantı açıldı.");
                    }
                    catch (Exception ex)
                    {
                        L.Log(LogType.FILE, LogLevel.ERROR, " timer1_Tick() --> Connection problem. Hata : " + ex.ToString());
                    }
                }
                else
                {
                    L.Log(LogType.FILE, LogLevel.INFORM, " timer1_Tick() --> Zaten kurulu olan bağlantı ile devam ediliyor.");
                }

                if (se.Connected)
                {
                    L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() --> Bağlantı şuan açık.");
                    if (se.ShellOpened)
                    {
                        L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() --> Shell şuan açık.");
                        if (se.ShellConnected)
                        {
                            L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() --> Shell Bağlantı kurdu.");
                            try
                            {
                                //We can collect data.
                                strR = GetData(se);
                            }
                            catch (Exception ex)
                            {
                                L.Log(LogType.FILE, LogLevel.ERROR, " timer1_Tick() --> Data Getirirken hata ile karşılaşıldı.");
                                L.Log(LogType.FILE, LogLevel.ERROR, " timer1_Tick() --> Hata: " + ex.ToString());
                            }
                        }
                    }
                }
                else
                {
                    L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() --> Bağlantı kopuk!!!!");
                }

                while ((line = strR.ReadLine()) != null)
                {
                    L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() --> Line to Parse : " + line);
                    rec.Description = line;
                    String[] arr = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

                    if (arr.Length >= 8)
                    {
                        rec.Description = line;

                        if (arr[0].Contains("."))
                        {
                            rec.ComputerName = remoteHost;
                            rec.CustomStr1 = arr[0];
                            tempMac = arr[1];

                            tempMac = tempMac.Remove(0, 2);
                            string[] arrMac = tempMac.Split('.');
                            tempMac = "";
                            for (int i = 0; i < arrMac.Length; i++)
                            {
                                tempMac += arrMac[i];
                            }
                            rec.CustomStr2 = tempMac.Trim();
                            rec.EventType = arr[7];

                            for (int i = 2; i < 6; i++)
                            {
                                tempDate += arr[i] + " ";
                            }
                            tempDate = tempDate.Trim();

                            if (arr[6] == "PM" && Convert.ToDateTime(tempDate).Hour != 12)
                            {
                                rec.Datetime = Convert.ToDateTime(tempDate, CultureInfo.InvariantCulture).AddDays(-8).AddHours(12).ToString("dd/MM/yyyy HH:mm:ss");
                            }
                            else
                            {
                                rec.Datetime = Convert.ToDateTime(tempDate, CultureInfo.InvariantCulture).AddDays(-8).ToString("dd/MM/yyyy HH:mm:ss");
                            }

                            if (Convert.ToDateTime(tempDate).Date.Day == DateTime.Now.Date.Day)
                            {
                                rec.CustomStr3 = "Removed";
                            }

                            tempDate = "";
                            recList.Add(rec);
                        }
                    }
                }

                L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() --> Çekilen veriler tarihlerine bakılarak kaydedilecek.");
                L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() --> Kalınan LastPosition: " + Local_LastPosition);

                lastDate = new DateTime();
                bool lastPositionSetted = DateTime.TryParse(Local_LastPosition, out lastDate);

                foreach (Rec r in recList)
                {
                    if (!lastPositionSetted)
                    {
                        //İlk kayıtlar.
                        CustomServiceBase s = base.GetInstanceService("Security Manager Remote Recorder");
                        s.SetData(Dal, virtualhost, r);
                        L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() --> LastPosition is null. Veri ataması gerçekleştirildi.");
                    }
                    else
                    {
                        //Daha önce kayıt alınmış.
                        if (Convert.ToDateTime(r.Datetime) > lastDate)
                        {
                            //Değişen kayıt olmuş. Veritabanına eklenmeli.
                            CustomServiceBase s = base.GetInstanceService("Security Manager Remote Recorder");
                            s.SetData(Dal, virtualhost, r);
                            L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() --> Record Date daha büyük. Veri ataması gerçekleştirildi. : " + r.Datetime);
                        }
                        else
                        {
                            //Tarih aynı. Kayıt işlemi yapılmayacak.
                            L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() --> Record Date daha küçük. Kaydedilmedi!. Tarih: " + r.Datetime);
                        }
                    }
                }

                L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() --> En büyük tarih belirleniyor.");
                maxDate = lastDate.ToString();

                foreach (Rec r in recList)
                {
                    if (Convert.ToDateTime(r.Datetime) > Convert.ToDateTime(maxDate))
                    {
                        maxDate = r.Datetime;
                    }
                }

                CustomServiceBase ser = base.GetInstanceService("Security Manager Remote Recorder");
                ser.SetReg(ID, maxDate, "", "", "");
                Local_LastPosition = maxDate;
                L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() --> Yeni LastPosition atandı. LastPostion: " + Local_LastPosition);

            }
            catch (Exception ex)
            {
                L.Log(LogType.FILE, LogLevel.ERROR, " timer1_Tick() --> Recorder Main Exception: " + ex.ToString());
            }
            finally
            {
                if (se != null)
                {
                    if (se.Connected)
                    {
                        se.Close();
                        L.Log(LogType.FILE, LogLevel.INFORM, " timer1_Tick() --> Bağlantı kesildi.");
                    }
                }

                se = null;
                tempDate = "";
                tempMac = "";
                timer1.Enabled = true;
                L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() --> Timer'ın işi bitti. Time: " + DateTime.Now.ToLongTimeString());
                L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() --> Bir Sonraki Tetikleme " + timer_interval / 60000 + " dakika sonra gerçekleşecektir.");
                L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() --> =============================================== ");
                recList.Clear();
            }
        }
        private StringReader GetData(SshShell se)
        {
            StringReader strR = null;
            string commandToRead = "show ip dhcp binding";
            Stream st = null;
            StreamReader sr = null;
            StringBuilder strB = new StringBuilder();
            bool morePage = false;

            while (true)
            {
                se.WriteLine(commandToRead);
                se.WriteLine(commandToRead);
                L.Log(LogType.FILE, LogLevel.DEBUG, " GetData() --> Komut çalıştırıldı : " + commandToRead);

                st = se.GetStream();
                sr = new StreamReader(st);

                int cnt = 0;
                int asci = 0;
                string lineStr = "";
                string previousStr = "";

                while ((asci = sr.Read()) != 0)
                {
                    char karakter = Convert.ToChar(asci);

                    if (karakter == '#')
                    {
                        if (cnt > 0)
                        {
                            break;
                        }
                        cnt++;
                    }

                    if (karakter != '\n')
                    {
                        lineStr += karakter.ToString();
                    }
                    else
                    {
                        L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() | GetData() --> Read Line : " + lineStr);
                        if (lineStr.ToLower().Contains("more"))
                        {
                            L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() | GetData() --> There is more ip to be read.");
                            L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() | GetData() --> We will read more page.");
                            L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() | GetData() --> Last ip line we have got : " + previousStr);
                            morePage = true;
                        }
                        else
                        {
                            previousStr = lineStr;
                            strB.Append(lineStr);
                        }
                        lineStr = "";
                    }
                }

                if (morePage)
                {
                    commandToRead = "show ip dhcp binding | begin " + previousStr;
                    morePage = false;
                }
                else
                {
                    L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() | GetData() --> All ip has got.");
                    break;
                }
            }

            strR = new StringReader(strB.ToString());

            if (se.Connected)
            {
                se.WriteLine("exit");
                L.Log(LogType.FILE, LogLevel.DEBUG, " timer1_Tick() | GetData() --> Komut çalıştırıldı. exit");

                se.Close();
                L.Log(LogType.FILE, LogLevel.INFORM, " timer1_Tick() | GetData() --> Bağlantı kesildi.");
            }

            st.Close();
            sr.Close();

            return strR;
        }