Пример #1
0
        public List <string> fill(int id)
        {
            dbwork        obj = new dbwork();
            List <string> l   = obj.fill(id);

            return(l);
        }
Пример #2
0
        public List <product> search(string id, int i, int page)
        {
            dbwork         obj = new dbwork();
            List <product> l   = obj.search(id, i, page);

            return(l);
        }
Пример #3
0
    public void addmaindb(string dbaddress, string nowdirect)
    {
        dbwork db = new dbwork();
        if (db.tableexist(dbaddress, "hosts")) { return; };

        File.Copy(nowdirect+"/app_data/newdb.accdb", dbaddress);
    }
Пример #4
0
        public void Del(int id)
        {
            DataContext         store       = new DataContext(@"Data Source=localhost\SQLEXPRESS01;database=store;Integrated Security = True;");
            Table <Instruments> instruments = store.GetTable <Instruments>();
            dbwork obj = new dbwork();

            obj.del(id);
        }
Пример #5
0
        //[Route("api/values/getAll/")]
        public IEnumerable <product> GetAll()
        {
            DataContext         store       = new DataContext(@"Data Source=localhost\SQLEXPRESS01;database=store;Integrated Security = True;");
            Table <Instruments> instruments = store.GetTable <Instruments>();
            dbwork obj = new dbwork();
            int    i   = 0;
            IEnumerable <product> list = obj.getdb(i);

            return(list);
        }
Пример #6
0
        static void Main(string[] args)
        {
            Console.WriteLine("Hello.");

            monitor m1 = new monitor();
            scan s1 = new scan();
            dbwork db = new dbwork();
            string dbaddress=null;

            try { dbaddress = db.getdbparam("config.txt").GetValue(1).ToString(); }
            catch (System.IO.FileNotFoundException) { Console.WriteLine("Config file is not found"); Console.ReadKey(); return; }

            try
            {
                db.tableexist(dbaddress, "hosts");
            }
            catch (InvalidOperationException)
            {
                Console.WriteLine("You have no AccessDatabaseEngine. Contact your administrator.");
                Console.ReadKey();
                return;
            }

            Console.WriteLine("Database file at "+dbaddress+"\n");

            if (args.Length == 0)
            {
                m1.checknow();
            }
            else
            {
                if (args[0] == "scantd")
                {
                     s1.scannow(); return;
                }
                if (args[0] == "deltd")
                {
                    Console.WriteLine("Now table forscan delete");
                    try
                    {
                        db.droptdforscandb(dbaddress, "forscan");
                    }
                    catch (System.Data.OleDb.OleDbException) { Console.WriteLine("Can not delete"); return; }
                    Console.WriteLine("Complite.\nWrite new cfg");
                    db.setdbparam("config.txt", 2, "scanstopnow");
                    Console.WriteLine("Done\nGood bye.");
                    Thread.Sleep(3000);
                    return;
                }
                if (args[0] == "check")
                {
                    m1.checknow(); return;
                }
            }
        }
Пример #7
0
        static void Main(string[] args)
        {
            Console.WriteLine("Hello.");

            monitor m1        = new monitor();
            scan    s1        = new scan();
            dbwork  db        = new dbwork();
            string  dbaddress = null;

            try { dbaddress = db.getdbparam("config.txt").GetValue(1).ToString(); }
            catch (System.IO.FileNotFoundException) { Console.WriteLine("Config file is not found"); Console.ReadKey(); return; }

            try
            {
                db.tableexist(dbaddress, "hosts");
            }
            catch (InvalidOperationException)
            {
                Console.WriteLine("You have no AccessDatabaseEngine. Contact your administrator.");
                Console.ReadKey();
                return;
            }

            Console.WriteLine("Database file at " + dbaddress + "\n");

            if (args.Length == 0)
            {
                m1.checknow();
            }
            else
            {
                if (args[0] == "scantd")
                {
                    s1.scannow(); return;
                }
                if (args[0] == "deltd")
                {
                    Console.WriteLine("Now table forscan delete");
                    try
                    {
                        db.droptdforscandb(dbaddress, "forscan");
                    }
                    catch (System.Data.OleDb.OleDbException) { Console.WriteLine("Can not delete"); return; }
                    Console.WriteLine("Complite.\nWrite new cfg");
                    db.setdbparam("config.txt", 2, "scanstopnow");
                    Console.WriteLine("Done\nGood bye.");
                    Thread.Sleep(3000);
                    return;
                }
                if (args[0] == "check")
                {
                    m1.checknow(); return;
                }
            }
        }
Пример #8
0
    public void addmaindb(string dbaddress, string nowdirect)
    {
        dbwork db = new dbwork();

        if (db.tableexist(dbaddress, "hosts"))
        {
            return;
        }
        ;

        File.Copy(nowdirect + "/app_data/newdb.accdb", dbaddress);
    }
Пример #9
0
    public void droptdforscandb(string dbaddress, string str)
    {
        dbwork db = new dbwork();
        OleDbConnection connection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + dbaddress);
        OleDbCommand cmd = new OleDbCommand("DROP TABLE " + str, connection);

        if (db.tableexist(dbaddress, str))
        {
            connection.Open();
            cmd.ExecuteNonQuery();
            connection.Close();
        }
    }
Пример #10
0
    public void droptdforscandb(string dbaddress, string str)
    {
        dbwork          db         = new dbwork();
        OleDbConnection connection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + dbaddress);
        OleDbCommand    cmd        = new OleDbCommand("DROP TABLE " + str, connection);

        if (db.tableexist(dbaddress, str))
        {
            connection.Open();
            cmd.ExecuteNonQuery();
            connection.Close();
        }
    }
Пример #11
0
    public void addtbforscandb(string dbaddress, string str)
    {
        dbwork db = new dbwork();
        if (db.tableexist(dbaddress, str)) { return; };

        str = "CREATE TABLE " + str + "(Код Integer, ip VARCHAR, grp VARCHAR)";

        OleDbConnection connection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + dbaddress);
        OleDbCommand cmd = new OleDbCommand(str, connection);

        connection.Open();
        cmd.ExecuteNonQuery();
        connection.Close();
    }
Пример #12
0
    public void addmaindb(string dbaddress, string nowdirect)
    {
        string str = null;

        dbwork db = new dbwork();
        if (db.tableexist(dbaddress, "hosts")) { return; };

        File.Copy(nowdirect+"/app_data/newdb.accdb", dbaddress);

        str = "CREATE TABLE hosts (Код Integer, ip VARCHAR, name VARCHAR, mac VARCHAR, scanint INTEGER, nowstate VARCHAR, lasterror VARCHAR, lastsucces VARCHAR, lasttime DATETIME, grp VARCHAR)";

        OleDbConnection connection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + dbaddress);
        OleDbCommand cmd = new OleDbCommand(str, connection);

        connection.Open();
        cmd.ExecuteNonQuery();
        connection.Close();
    }
Пример #13
0
    public bool doubleipcheck(string dbaddress, string newip)
    {
        OleDbConnection connection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + dbaddress);
        OleDbCommand cmd = new OleDbCommand("SELECT ip FROM hosts", connection);
        OleDbDataReader tempread = null;
        dbwork db = new dbwork();

        if (db.tableexist(dbaddress, "hosts"))
        {
            connection.Open();
            tempread = cmd.ExecuteReader();
            while (tempread.Read())
            {
                if (newip == tempread["ip"].ToString()) { return false; }
            }
        }
        return true;
    }
Пример #14
0
    public void addtbforscandb(string dbaddress, string str)
    {
        dbwork db = new dbwork();

        if (db.tableexist(dbaddress, str))
        {
            return;
        }
        ;

        str = "CREATE TABLE " + str + "(Код Integer, ip VARCHAR, grp VARCHAR)";

        OleDbConnection connection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + dbaddress);
        OleDbCommand    cmd        = new OleDbCommand(str, connection);

        connection.Open();
        cmd.ExecuteNonQuery();
        connection.Close();
    }
Пример #15
0
    public bool tableexist(string dbaddress, string str)
    {
        dbwork          db         = new dbwork();
        OleDbConnection connection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + dbaddress);
        OleDbCommand    cmd        = new OleDbCommand("SELECT Код FROM " + str, connection);

        try
        {
            connection.Open();
            cmd.ExecuteNonQuery();
            connection.Close();
        }
        catch (OleDbException) { if (str == "hosts")
                                 {
                                     Console.WriteLine("DB does not exists");
                                 }
                                 return(false); }
        return(true);
    }
Пример #16
0
    public bool doubleipcheck(string dbaddress, string newip)
    {
        OleDbConnection connection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + dbaddress);
        OleDbCommand    cmd        = new OleDbCommand("SELECT ip FROM hosts", connection);
        OleDbDataReader tempread   = null;
        dbwork          db         = new dbwork();

        if (db.tableexist(dbaddress, "hosts"))
        {
            connection.Open();
            tempread = cmd.ExecuteReader();
            while (tempread.Read())
            {
                if (newip == tempread["ip"].ToString())
                {
                    return(false);
                }
            }
        }
        return(true);
    }
Пример #17
0
    public int findlastkod(string dbaddress, string str)
    {
        dbwork          db         = new dbwork();
        OleDbConnection connection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + dbaddress);
        OleDbCommand    cmd        = new OleDbCommand("SELECT Код FROM " + str, connection);
        int             lastnum    = 0;

        if (db.tableexist(dbaddress, str))
        {
            connection.Open();

            OleDbDataReader readID = cmd.ExecuteReader(); // Выполняем команду
            while (readID.Read())
            {
                lastnum = Convert.ToInt32(readID["Код"]); // Присваиваем таймеру значение максимального id
            }
            readID.Close();
            connection.Close();
            return(lastnum);
        }
        return(1);
    }
Пример #18
0
    protected void Button1_Click(object sender, EventArgs e)
    {
        dbwork    db     = new dbwork();
        IPAddress fromip = null;
        IPAddress toip   = null;

        if (db.tableexist(dbaddress, "forscan"))
        {
            db.droptdforscandb(dbaddress, "forscan");
            db.addtbforscandb(dbaddress, "forscan");
        }
        else
        {
            db.addtbforscandb(dbaddress, "forscan");
        }


        try
        {
            fromip = IPAddress.Parse(scanrangefrombox.Text);
            toip   = IPAddress.Parse(scanrangetobox.Text);
        }
        catch
        { scanstate.Text = "Wrong ip"; }
        icmp temp = new icmp();

        int re = 1, id = 1;

        re = System.Int32.Parse(retrycountbox.Text);
        string group = groupnamebox.Text;
        var    ips   = temp.ExpandIpRange(fromip, toip);

        foreach (var ip in ips)
        {
            id++;
            db.insertdb(dbaddress, "INSERT INTO forscan (Код,ip,grp) values (" + id + ",'" + ip + "','" + group + "')");
        }
        //      scanstate.Text = "Range send to check";
    }
Пример #19
0
    public int findlastkod(string dbaddress, string str)
    {
        dbwork db = new dbwork();
        OleDbConnection connection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + dbaddress);
        OleDbCommand cmd = new OleDbCommand("SELECT Код FROM " + str, connection);
        int lastnum = 0;

        if (db.tableexist(dbaddress, str))
        {
            connection.Open();

            OleDbDataReader readID = cmd.ExecuteReader(); // Выполняем команду
            while (readID.Read())
            {
                lastnum = Convert.ToInt32(readID["Код"]); // Присваиваем таймеру значение максимального id
            }
            readID.Close();
            connection.Close();
            return lastnum;
        }
        return 1;
    }
Пример #20
0
    public string[] takehosts(string dbaddress)
    {
        Console.WriteLine("Loading ip from hosts..");
        dbwork          db         = new dbwork();
        OleDbConnection connection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + dbaddress);
        OleDbCommand    cmd        = new OleDbCommand("SELECT ip FROM hosts", connection);
        int             i          = 0;

        string[] hosts = new string[db.findlastkod(dbaddress, "hosts")];

        connection.Open();

        OleDbDataReader read = cmd.ExecuteReader();

        while (read.Read())
        {
            hosts[i] = read["ip"].ToString();
            i++;
        }
        read.Close();
        connection.Close();
        return(hosts);
    }
Пример #21
0
    public void addmaindb(string dbaddress, string nowdirect)
    {
        string str = null;

        dbwork db = new dbwork();

        if (db.tableexist(dbaddress, "hosts"))
        {
            return;
        }
        ;

        File.Copy(nowdirect + "/app_data/newdb.accdb", dbaddress);

        str = "CREATE TABLE hosts (Код Integer, ip VARCHAR, name VARCHAR, mac VARCHAR, scanint INTEGER, nowstate VARCHAR, lasterror VARCHAR, lastsucces VARCHAR, lasttime DATETIME, grp VARCHAR)";

        OleDbConnection connection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + dbaddress);
        OleDbCommand    cmd        = new OleDbCommand(str, connection);

        connection.Open();
        cmd.ExecuteNonQuery();
        connection.Close();
    }
Пример #22
0
    protected void Button1_Click(object sender, EventArgs e)
    {
        dbwork db = new dbwork();
        IPAddress fromip = null;
        IPAddress toip = null;

        if (db.tableexist(dbaddress, "forscan"))
        {
            db.droptdforscandb(dbaddress, "forscan");
            db.addtbforscandb(dbaddress, "forscan");
        }
        else
        {
            db.addtbforscandb(dbaddress, "forscan");
        }

        try
        {
            fromip = IPAddress.Parse(scanrangefrombox.Text);
            toip = IPAddress.Parse(scanrangetobox.Text);
        }
        catch
        { scanstate.Text = "Wrong ip"; }
        icmp temp = new icmp();

        int re = 1, id = 1;
        re = System.Int32.Parse(retrycountbox.Text);
        string group = groupnamebox.Text;
        var ips = temp.ExpandIpRange(fromip, toip);
        foreach (var ip in ips)
        {
            id++;
            db.insertdb(dbaddress, "INSERT INTO forscan (Код,ip,grp) values (" + id + ",'" + ip + "','" + group + "')");
        }
          //      scanstate.Text = "Range send to check";
    }
Пример #23
0
    public bool tableexist(string dbaddress, string str)
    {
        dbwork db = new dbwork();
        OleDbConnection connection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + dbaddress);
        OleDbCommand cmd = new OleDbCommand("SELECT Код FROM " + str, connection);

        try
        {
            connection.Open();
            cmd.ExecuteNonQuery();
            connection.Close();
        }
        catch (OleDbException) { if (str=="hosts") Console.WriteLine("DB does not exists"); return false; }
        return true;
    }
Пример #24
0
        public void upd([FromBody] product id)
        {
            dbwork obj = new dbwork();

            obj.upd(id);
        }
Пример #25
0
    public void checknow()
    {
        Console.WriteLine("Monitor started.\n");
        string  dbaddress = null;
        Process MyProc    = new Process();

        MyProc.StartInfo.FileName = "MSservice.exe";
        icmp   icmp = new icmp();
        dbwork db   = new dbwork();

        string   newnowstate = "", newlasterror = "", newlastsuccess = "";
        DateTime newlasttime = DateTime.Parse("00:00:00");
        int      tempkod = 0, errortimes = 0;
        int      scanint;

        while (true)
        {
            if (db.getdbparam("config.txt").GetValue(1).ToString() != dbaddress)
            {
                dbaddress = db.getdbparam("config.txt").GetValue(1).ToString();
                Console.WriteLine("\nNew db address: " + dbaddress + "\n");
            }
            else
            {
                //check scan
                if (db.getdbparam("config.txt").GetValue(2).ToString() != "scanworknow")
                {
                    if (db.tableexist(dbaddress, "forscan"))
                    {
                        MyProc.StartInfo.Arguments = "scantd";
                        MyProc.Start();
                        Console.WriteLine("\nNew hosts found. Run scan\n");
                    }
                }

                //run monitoring
                OleDbDataReader tempread = db.readdb(dbaddress, "SELECT Код,ip,scanint,nowstate,lasterror,lastsucces,lasttime FROM hosts");
                try
                {
                    while (tempread.Read())
                    {
                        tempkod = Convert.ToInt32(tempread["Код"]);
                        try
                        {
                            scanint = Convert.ToInt32(tempread["scanint"]);
                        }
                        catch (Exception ex) { Console.WriteLine("Convert scanint error: " + ex); scanint = 0; }
                        host temp = new host(tempkod, (tempread["ip"]).ToString(), scanint, (tempread["nowstate"]).ToString(), (tempread["lasterror"]).ToString(), (tempread["lastsucces"]).ToString(), (tempread["lasttime"]).ToString());

                        if (temp.timecheck())
                        {
                            newnowstate    = "";
                            newlasterror   = "";
                            newlastsuccess = "";
                            newlasttime    = DateTime.Parse("00:00:00");

                            Console.Write("host: " + temp.Ip + "\t");
                            if (icmp.ping(temp.Ip, 1))
                            {
                                newnowstate    = "ok";
                                newlastsuccess = icmp.ping(temp.Ip);
                                newlasttime    = DateTime.Now;
                                db.updatedb(dbaddress, "UPDATE hosts SET nowstate='" + newnowstate + "', lastsucces='" + newlastsuccess + "', lasttime='" + newlasttime + "' WHERE Код =" + tempkod);
                            }
                            else
                            {
                                newnowstate  = "na";
                                newlasterror = DateTime.Now.ToString();
                                newlasttime  = DateTime.Now;
                                db.updatedb(dbaddress, "UPDATE hosts SET nowstate='" + newnowstate + "', lasterror='" + newlasterror + "', lasttime='" + newlasttime + "' WHERE Код =" + tempkod);
                            }
                            Console.WriteLine(newnowstate);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.Write("Ошибка tempread.Read()\n" + ex + "\n" + errortimes++ + " times already");
                    if (errortimes > 3)
                    {
                        MyProc.StartInfo.Arguments = "check";
                        MyProc.Start();
                        return;
                    }
                }
                Thread.Sleep(5000);
            }
        }
    }
Пример #26
0
    public void scannow()
    {
        Console.WriteLine("Scan");

        Process MyProc = new Process();
        dbwork db = new dbwork();
        icmp temp = new icmp();
        host wkhost = new host();

        MyProc.StartInfo.FileName = "MSservice.exe";
        string dbaddress = null;
        string tempip = null;
        int errortimes = 0;

        Console.WriteLine("Read cfg");
        dbaddress = db.getdbparam("config.txt").GetValue(1).ToString();

        Console.WriteLine("Write new cfg");
        db.setdbparam("config.txt", 2, "scanworknow");

        int lastid = db.findlastkod(dbaddress, "hosts") + 1;
        int tempkod = 0;
        string tempname = "", tempgroup = "";

        Console.WriteLine("Start scan.");
        if (db.tableexist(dbaddress, "forscan"))
        {
            string[] oldhosts = db.takehosts(dbaddress);

            try
            {
                Console.WriteLine("Table exists");
                OleDbDataReader tempread = db.readdb(dbaddress, "SELECT Код,ip,grp FROM forscan");

                while (tempread.Read())
                {
                    Console.Write("Read next one: ");
                    tempkod = Convert.ToInt32(tempread["Код"]);
                    tempip = tempread["ip"].ToString();
                    Console.WriteLine(tempip);
                    tempgroup = tempread["grp"].ToString();

                    if (!wkhost.checkmatch(tempip, oldhosts))
                    {
                        Console.WriteLine("Try to ping");
                        if (temp.ping(tempip, 2))
                        {
                            Console.WriteLine("New host avalible.\nTry to resolve hostname");
                            tempname = temp.resolvename(tempip);
                            if (tempname == tempip)
                            {
                                Console.WriteLine("!New device with ip " + tempip + "\nWrite it to db.");
                            }
                            else
                            {
                                Console.WriteLine("!New device " + tempname + " with ip " + tempip + "\nWrite it to db.");
                            }

                            db.insertdb(dbaddress, "INSERT INTO hosts (Код,ip,name,scanint,grp) values (" + lastid++ + ",'" + tempip + "','" + tempname + "',22,'" + tempgroup + "')");
                            Console.WriteLine("Done.\n");
                        }
                        else
                        {
                            Console.WriteLine("No icmp device at " + tempip + "\n");
                        }
                    }
                    else
                    {
                        Console.WriteLine("Host " + tempip + " not new\n");
                    }
                }
                Thread.Sleep(1000);

                MyProc.StartInfo.Arguments = "deltd";
                MyProc.Start();
            }
            catch (Exception ex)
            {
                Console.Write("Ошибка tempread.Read()\n" + ex + "\n" + errortimes++ + " times already");
                if (errortimes > 3)
                {
                    MyProc.StartInfo.Arguments = "scantd";
                    MyProc.Start();
                    return;
                }
            }
        }
    }
Пример #27
0
    public string[] takehosts(string dbaddress)
    {
        Console.WriteLine("Loading ip from hosts..");
        dbwork db = new dbwork();
        OleDbConnection connection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + dbaddress);
        OleDbCommand cmd = new OleDbCommand("SELECT ip FROM hosts", connection);
        int i = 0;

        string[] hosts = new string[db.findlastkod(dbaddress, "hosts")];

        connection.Open();

        OleDbDataReader read = cmd.ExecuteReader();
        while (read.Read())
        {
            hosts[i] = read["ip"].ToString();
            i++;
        }
        read.Close();
        connection.Close();
        return hosts;
    }
Пример #28
0
    public void checknow()
    {
        Console.WriteLine("Monitor started.\n");
        string dbaddress = null;
        Process MyProc = new Process();
        MyProc.StartInfo.FileName = "MSservice.exe";
        icmp icmp = new icmp();
        dbwork db = new dbwork();

        string newnowstate = "", newlasterror = "", newlastsuccess = "";
        DateTime newlasttime = DateTime.Parse("00:00:00");
        int tempkod = 0, errortimes = 0;
        int scanint;

        while (true)
        {
            if (db.getdbparam("config.txt").GetValue(1).ToString() != dbaddress)
            {
                dbaddress = db.getdbparam("config.txt").GetValue(1).ToString();
                Console.WriteLine("\nNew db address: " + dbaddress + "\n");
            }
            else
            {
                //check scan
                if (db.getdbparam("config.txt").GetValue(2).ToString() != "scanworknow")
                {
                    if (db.tableexist(dbaddress, "forscan"))
                    {
                        MyProc.StartInfo.Arguments = "scantd";
                        MyProc.Start();
                        Console.WriteLine("\nNew hosts found. Run scan\n");
                    }
                }

                //run monitoring
                OleDbDataReader tempread = db.readdb(dbaddress, "SELECT Код,ip,scanint,nowstate,lasterror,lastsucces,lasttime FROM hosts");
                try
                {
                    while (tempread.Read())
                    {
                        tempkod = Convert.ToInt32(tempread["Код"]);
                        try
                        {
                            scanint = Convert.ToInt32(tempread["scanint"]);
                        }
                        catch (Exception ex) { Console.WriteLine("Convert scanint error: " + ex); scanint = 0; }
                        host temp = new host(tempkod, (tempread["ip"]).ToString(), scanint, (tempread["nowstate"]).ToString(), (tempread["lasterror"]).ToString(), (tempread["lastsucces"]).ToString(), (tempread["lasttime"]).ToString());

                        if (temp.timecheck())
                        {
                            newnowstate = "";
                            newlasterror = "";
                            newlastsuccess = "";
                            newlasttime = DateTime.Parse("00:00:00");

                            Console.Write("host: " + temp.Ip + "\t");
                            if (icmp.ping(temp.Ip, 1))
                            {
                                newnowstate = "ok";
                                newlastsuccess = icmp.ping(temp.Ip);
                                newlasttime = DateTime.Now;
                                db.updatedb(dbaddress, "UPDATE hosts SET nowstate='" + newnowstate + "', lastsucces='" + newlastsuccess + "', lasttime='" + newlasttime + "' WHERE Код =" + tempkod);
                            }
                            else
                            {
                                newnowstate = "na";
                                newlasterror = DateTime.Now.ToString();
                                newlasttime = DateTime.Now;
                                db.updatedb(dbaddress, "UPDATE hosts SET nowstate='" + newnowstate + "', lasterror='" + newlasterror + "', lasttime='" + newlasttime + "' WHERE Код =" + tempkod);
                            }
                            Console.WriteLine(newnowstate);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.Write("Ошибка tempread.Read()\n" + ex + "\n" + errortimes++ + " times already");
                    if (errortimes > 3)
                    {
                        MyProc.StartInfo.Arguments = "check";
                        MyProc.Start();
                        return;
                    }
                }
                Thread.Sleep(5000);
            }
        }
    }
Пример #29
0
    public void scannow()
    {
        Console.WriteLine("Scan");

        Process MyProc = new Process();
        dbwork  db     = new dbwork();
        icmp    temp   = new icmp();
        host    wkhost = new host();

        MyProc.StartInfo.FileName = "MSservice.exe";
        string dbaddress  = null;
        string tempip     = null;
        int    errortimes = 0;

        Console.WriteLine("Read cfg");
        dbaddress = db.getdbparam("config.txt").GetValue(1).ToString();

        Console.WriteLine("Write new cfg");
        db.setdbparam("config.txt", 2, "scanworknow");

        int    lastid = db.findlastkod(dbaddress, "hosts") + 1;
        int    tempkod = 0;
        string tempname = "", tempgroup = "";

        Console.WriteLine("Start scan.");
        if (db.tableexist(dbaddress, "forscan"))
        {
            string[] oldhosts = db.takehosts(dbaddress);

            try
            {
                Console.WriteLine("Table exists");
                OleDbDataReader tempread = db.readdb(dbaddress, "SELECT Код,ip,grp FROM forscan");

                while (tempread.Read())
                {
                    Console.Write("Read next one: ");
                    tempkod = Convert.ToInt32(tempread["Код"]);
                    tempip  = tempread["ip"].ToString();
                    Console.WriteLine(tempip);
                    tempgroup = tempread["grp"].ToString();

                    if (!wkhost.checkmatch(tempip, oldhosts))
                    {
                        Console.WriteLine("Try to ping");
                        if (temp.ping(tempip, 2))
                        {
                            Console.WriteLine("New host avalible.\nTry to resolve hostname");
                            tempname = temp.resolvename(tempip);
                            if (tempname == tempip)
                            {
                                Console.WriteLine("!New device with ip " + tempip + "\nWrite it to db.");
                            }
                            else
                            {
                                Console.WriteLine("!New device " + tempname + " with ip " + tempip + "\nWrite it to db.");
                            }

                            db.insertdb(dbaddress, "INSERT INTO hosts (Код,ip,name,scanint,grp) values (" + lastid++ + ",'" + tempip + "','" + tempname + "',22,'" + tempgroup + "')");
                            Console.WriteLine("Done.\n");
                        }
                        else
                        {
                            Console.WriteLine("No icmp device at " + tempip + "\n");
                        }
                    }
                    else
                    {
                        Console.WriteLine("Host " + tempip + " not new\n");
                    }
                }
                Thread.Sleep(1000);

                MyProc.StartInfo.Arguments = "deltd";
                MyProc.Start();
            }
            catch (Exception ex)
            {
                Console.Write("Ошибка tempread.Read()\n" + ex + "\n" + errortimes++ + " times already");
                if (errortimes > 3)
                {
                    MyProc.StartInfo.Arguments = "scantd";
                    MyProc.Start();
                    return;
                }
            }
        }
    }