private void 重启ToolStripMenuItem_Click(object sender, EventArgs e)
 {
     if (listView1.SelectedItems != null)
     {
         int id = int.Parse(this.listView1.SelectedItems[0].SubItems[0].Text);
         log.writeLog(System.DateTime.Now + "  开始重启tomcat...");
         //根据Id获取tomcat信息
         DataTable dt;
         string    str;
         dt = db.ExecuteQuery("SELECT tl.id,tl.lujing,tl.tomcatname,tl.lujingstop,ts.ipaddress,ts.port,ts.name,ts.operatingsystem FROM tomcatlist tl, serverslist ts WHERE tl.serverid=ts.id AND  tl.id=" + id, CommandType.Text);
         foreach (DataRow dr2 in dt.Rows)
         {
             //发送关闭命令
             string sendStr = null;
             log.writeLog(System.DateTime.Now + "  开始连接服务端...");
             sc = new SCHelper(int.Parse(dr2["port"].ToString()), dr2["ipaddress"].ToString());
             log.writeLog(System.DateTime.Now + "  向服务端发送消息:  hi");
             str = sc.sendCommond("hi");
             log.writeLog(System.DateTime.Now + "  服务端返回消息消息:  " + str);
             if (str != null && str.Equals("hi"))
             {
                 log.writeLog(System.DateTime.Now + "  向服务端发送消息:  executeCommand");
                 str = sc.sendCommond("executeCommand");
                 log.writeLog(System.DateTime.Now + "  服务端返回消息消息:  " + str);
                 if (str != null && str.StartsWith("ok"))
                 {
                     //tomcat路径
                     str = dr2["lujingstop"].ToString();
                     //获取关闭tomcat命令
                     sendStr = new CDHelper(dr2["operatingsystem"].ToString()).getMakeCommond().getStopTomcatCommond(str);
                     log.writeLog(System.DateTime.Now + "  向服务端发送消息:  " + sendStr);
                     str = sc.sendCommond(sendStr);
                     log.writeLog(System.DateTime.Now + "  服务端返回消息消息:  " + str);
                 }
             }
             System.Threading.Thread.Sleep(5000);
             //发送启动命令
             str = dr2["lujing"].ToString();
             //获取tomcat启动命令
             sendStr = new CDHelper(dr2["operatingsystem"].ToString()).getMakeCommond().getStartTomcatCommond(str);
             log.writeLog(System.DateTime.Now + "  向服务端发送消息:  " + sendStr);
             str = sc.sendCommond(sendStr);
             log.writeLog(System.DateTime.Now + "  服务端返回消息消息:  " + str);
             log.writeLog(System.DateTime.Now + "  向服务端发送消息:  exit");
             str = sc.sendCommond("exit");
             log.writeLog(System.DateTime.Now + "  服务端返回消息消息:  " + str);
             sc.socketClose();
         }
     }
 }
Пример #2
0
        //开启tomcat
        private void button1_Click(object sender, EventArgs e)
        {
            DataTable dt;

            dt = db.ExecuteQuery("SELECT tl.id,tl.lujing,tl.tomcatname,tl.lujingstop,ts.ipaddress,ts.port,ts.name,ts.operatingsystem FROM tomcatlist tl, serverslist ts WHERE tl.serverid=ts.id", CommandType.Text);
            foreach (DataRow dr2 in dt.Rows)
            {
                string str  = null;
                int    port = int.Parse(dr2["port"].ToString());
                string host = dr2["ipaddress"].ToString();
                log.writeLog(System.DateTime.Now + "  开始连接服务端...");
                sc = new SCHelper(port, host);
                log.writeLog(System.DateTime.Now + "  向服务端发送消息:  hi");
                str = sc.sendCommond("hi");
                log.writeLog(System.DateTime.Now + "  服务端返回消息消息:  " + str);
                if (str != null && str.Equals("hi"))
                {
                    log.writeLog(System.DateTime.Now + "  向服务端发送消息:  executeCommand");
                    str = sc.sendCommond("executeCommand");
                    log.writeLog(System.DateTime.Now + "  服务端返回消息消息:  " + str);
                    if (str != null && str.StartsWith("ok"))
                    {
                        //tomcat路径
                        str = dr2["lujing"].ToString();
                        //获取tomcat启动命令
                        string sendStr = new CDHelper(dr2["operatingsystem"].ToString()).getMakeCommond().getStartTomcatCommond(str);
                        log.writeLog(System.DateTime.Now + "  向服务端发送消息:  " + sendStr);
                        //发送启动命令
                        string reslut = sc.sendCommond(sendStr);
                        log.writeLog(System.DateTime.Now + "  服务端返回消息消息:  " + reslut);
                        textBox1.AppendText("开启tomcat:[" + dr2["tomcatname"] + "]命令已发出。\r\n");
                        System.Threading.Thread.Sleep(1000);
                    }
                }
                log.writeLog(System.DateTime.Now + "  向服务端发送消息:  exit");
                str = sc.sendCommond("exit");
                log.writeLog(System.DateTime.Now + "  服务端返回消息消息:  " + str);
                sc.socketClose();
            }
        }
        //关机
        private void shudownServer(object sender, EventArgs e)
        {
            string str = null;
            int    id  = int.Parse(this.listView1.SelectedItems[0].SubItems[0].Text);
            //根据Id获取tomcat信息
            DataTable dt;

            dt = db.ExecuteQuery("SELECT id,ipaddress,port,name,operatingsystem FROM serverslist WHERE id=" + id, CommandType.Text);
            foreach (DataRow dr2 in dt.Rows)
            {
                log.writeLog(System.DateTime.Now + "  开始连接服务端...");
                sc = new SCHelper(int.Parse(dr2["port"].ToString()), dr2["ipaddress"].ToString());
                log.writeLog(System.DateTime.Now + "  向服务端发送消息:  hi");
                str = sc.sendCommond("hi");
                log.writeLog(System.DateTime.Now + "  服务端返回消息消息:  " + str);
                if (str != null && str.Equals("hi"))
                {
                    log.writeLog(System.DateTime.Now + "  向服务端发送消息:  executeCommand");
                    str = sc.sendCommond("executeCommand");
                    log.writeLog(System.DateTime.Now + "  服务端返回消息消息:  " + str);
                    if (str != null && str.StartsWith("ok"))
                    {
                        log.writeLog(System.DateTime.Now + "  向服务端发送消息:  shutdown -s -t 1");
                        //获取关机命令
                        string sendStr = new CDHelper(dr2["operatingsystem"].ToString()).getMakeCommond().getStopServer();
                        str = sc.sendCommond(sendStr);
                        MessageBox.Show("关闭服务器命令已发出", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    log.writeLog(System.DateTime.Now + "  服务端返回消息消息:  " + str);
                }
            }
            log.writeLog(System.DateTime.Now + "  向服务端发送消息:  exit");
            str = sc.sendCommond("exit");
            log.writeLog(System.DateTime.Now + "  服务端返回消息消息:  " + str);
            sc.socketClose();
        }
        private string checkTomcatStatus(int id)
        {
            //根据Id获取tomcat信息
            log.writeLog(System.DateTime.Now + "  开始刷新tomcat状态...");
            DataTable dt;
            string    str;

            dt = db.ExecuteQuery("SELECT tl.id,tl.lujing,tl.tomcatname,tl.lujingstop,ts.ipaddress,ts.port,ts.name,ts.operatingsystem FROM tomcatlist tl, serverslist ts WHERE tl.serverid=ts.id AND  tl.id=" + id, CommandType.Text);
            foreach (DataRow dr2 in dt.Rows)
            {
                //发送关闭命令
                log.writeLog(System.DateTime.Now + "  开始连接服务端...");
                sc = new SCHelper(int.Parse(dr2["port"].ToString()), dr2["ipaddress"].ToString());
                log.writeLog(System.DateTime.Now + "  向服务端发送消息:  hi");
                str = sc.sendCommond("hi");
                log.writeLog(System.DateTime.Now + "  服务端返回消息消息:  " + str);
                if (str != null && str.Equals("hi"))
                {
                    log.writeLog(System.DateTime.Now + "  向服务端发送消息:  getresponse");
                    str = sc.sendCommond("getresponse");
                    log.writeLog(System.DateTime.Now + "  服务端返回消息消息:  " + str);
                    if (str != null && str.StartsWith("ok"))
                    {
                        //tomcat路径
                        str = dr2["lujingstop"].ToString();
                        //str = "D:\\apache-tomcat-7.0.62\\conf\\";
                        //获取tocat配置文件
                        string sendStr = new CDHelper(dr2["operatingsystem"].ToString()).getMakeCommond().getTomcatServerXml(str);
                        log.writeLog(System.DateTime.Now + "  向服务端发送消息:  " + sendStr);
                        str = sc.sendCommond(sendStr);
                        log.writeLog(System.DateTime.Now + "  服务端返回消息:  " + str);
                        //提取端口信息
                        string          pattern = @"<Connector port=""(\d+)""";
                        Regex           r       = new Regex(pattern, RegexOptions.IgnoreCase);
                        Match           m       = r.Match(str.ToString());
                        MatchCollection matchs  = r.Matches(str);
                        //如果匹配的tomcat为空,则返回未知
                        if (matchs.Count <= 0)
                        {
                            return("未知");
                        }
                        for (int i = 0; i < matchs.Count; i++)
                        {
                            Match match = matchs[i];
                            //match.Value是匹配的内容
                            // Console.WriteLine(match.Value);
                            string name = match.Groups[1].Value;
                            log.writeLog(System.DateTime.Now + "  匹配到端口:  " + name);
                            log.writeLog(System.DateTime.Now + "  开始验证端口是否开放:  " + name);
                            sendStr = "command" + "netstat -ano | findstr " + name;
                            log.writeLog(System.DateTime.Now + "  向服务端发送消息:  " + sendStr);
                            str = sc.sendCommond(sendStr);
                            log.writeLog(System.DateTime.Now + "  服务端返回消息:  " + str);
                            //判断端口是否被监听
                            bool l = Regex.IsMatch(str, @"([\s\S]*?):" + name + "(.*?)LISTENING");
                            if (!l)
                            {
                                return("关闭");
                            }
                        }
                    }
                }
                log.writeLog(System.DateTime.Now + "  向服务端发送消息:  exit");
                str = sc.sendCommond("exit");
                log.writeLog(System.DateTime.Now + "  服务端返回消息消息:  " + str);
                sc.socketClose();
            }
            return("开启");
        }