示例#1
0
文件: Util.cs 项目: kuki89/IP_Lab
    /// <summary>
    /// 从IP信息查询到服务器的相应信息
    /// </summary>
    /// <param name="lab_ip">ip地址</param>
    /// <returns></returns>
    public static Server_Information GetServerInfoFromIP(string lab_ip)
    {
        Server_Information server = new Server_Information();

        string strsql = "select * from dbo.lab where lab_ip = '" + lab_ip + "'";
        DbCommand cmd = sql.GetSqlStringCommond(strsql);

        using (DbDataReader reader = sql.ExecuteReader(cmd))
        {
            if (reader.Read())
            {
                server.serverip = lab_ip;
                server.usrname = reader["lab_username"].ToString();
                server.pwd = reader["lab_password"].ToString();

                server.innerservip = reader["lab_switch_serverip"].ToString();
                server.innerusrname = reader["lab_switch_username"].ToString();
                server.innerpwd = reader["lab_switch_password"].ToString();

                server.sysname = reader["lab_username"].ToString() + reader["lab_sysname"].ToString();
                server.innersysname = reader["lab_switch_sysname"].ToString();
            }
        }

        return server;
    }
示例#2
0
    public override string ExecuteCommand(string command, string data)
    {
        try
        {
            // 获取服务器信息
            XDocument xser = XDocument.Parse(command);
            string lab_ip = xser.Element("IPLab").Element("LabIP").Value;
            si = Util.GetServerInfoFromIP(lab_ip);

            XDocument xdoc = XDocument.Parse(data);

            XElement filelist = xdoc.Element("IP_Lab").Element("StartList");
            XElement floder = xdoc.Element("IP_Lab").Element("Floder");
            ParseXMLtoList(filelist);

            SshShell shell = new SshShell(si.serverip, si.usrname, si.pwd);
            shell.Connect();
            if (shell.Expect(new Regex(si.sysname), 5) == null)
                throw new Exception("连接服务器出错!");

            if (CiscoSwitchList.Count > 0)
            {
                // 登录到设备上
                Util.SshWriteAndExpect(shell, "cd ~", si.sysname);
                Util.SshWriteAndExpect(shell, "ssh " + si.innerusrname + "@" + si.innerservip, "Password:"******" 启动失败!";
                }

                Util.SshWriteAndExpect(shell, "cd ~", si.innersysname);
                Util.SshWriteAndExpect(shell, "exit", si.sysname);
            }

            Util.SshWriteAndExpect(shell, "cd ~", si.sysname);

            foreach (FileBase file in FileList)
            {
                StartDevice sd = new StartDevice(file, shell, si.sysname);
                if (sd.Start())
                    continue;
                else
                    return sd.GetFileName() + " 启动失败!";
            }

            return "启动设备成功!";
        }
        catch (System.Exception ex)
        {
            return ex.Message;
        }
    }
示例#3
0
    public override string ExecuteCommand(string command, string data)
    {
        try
        {
            // 获取服务器信息
            XDocument xser = XDocument.Parse(command);
            string lab_ip = xser.Element("IPLab").Element("LabIP").Value;
            si = Util.GetServerInfoFromIP(lab_ip);

            // 数据信息
            XDocument xdoc = XDocument.Parse(data);

            XElement filelist = xdoc.Element("IP_Lab").Element("FileList");
            XElement floder = xdoc.Element("IP_Lab").Element("Floder");
            FileList = ParseXMLtoList(filelist);

            SshShell shell = new SshShell(si.serverip, si.usrname, si.pwd);
            shell.Connect();
            if (shell.Expect(new Regex(si.sysname), 5) == null)
                throw new Exception("连接服务器出错!");

            // 首先,删除这个文件夹
            string flodername = floder.Value;

            Util.SshWriteAndExpect(shell, "cd ~", si.sysname);
            Util.SshWriteAndExpect(shell, "cd lab_script/", si.sysname);
            Util.SshWriteAndExpect(shell, "rm -rf " + flodername, si.sysname);

            CiscoSwitchList = new List<FileBase>();
            foreach (FileBase file in FileList)
            {
                if (file.deviceType == (int)DeviceType.DEVICE_TYPE_CISCOSWITCHER)
                {
                    // 对思科交换机的设备另行处理
                    CiscoSwitchList.Add(file);
                    continue;
                }
                FileToServer fts = new FileToServer(file, shell, si.sysname);
                if (fts.WriteFileToServer())
                    continue;
                else
                    return file.fileName + " 生成失败 !";
            }

            // 生成公共文件
            if (!GenerateCommonFile(xdoc.Element("IP_Lab"), shell))
                return "公共文件生成失败";

            // 存在思科交换机设备时,需要登录到其服务器
            if (CiscoSwitchList.Count > 0)
            {
                // 登录到设备上
                Util.SshWriteAndExpect(shell, "cd ~", si.sysname);
                Util.SshWriteAndExpect(shell, "ssh " + si.innerusrname + "@" + si.innerservip, "Password:"******"rm -rf ./iou/" + flodername, si.innersysname);
                // 生成配置文件
                foreach (FileBase file in CiscoSwitchList)
                {
                    FileToServer fts = new FileToServer(file, shell, si.innersysname);
                    if (fts.WriteFileToServer())
                        continue;
                    else
                        return file.fileName + " 生成失败 !";
                }

                string res = Util.SshWriteAndExpect(shell, "cd ~/iou/" + flodername, si.innersysname);
                if (!res.Contains("cd: can't cd to"))
                {
                    Util.SshWriteAndExpect(shell, "echo >> NETMAP", si.innersysname);
                    Util.SshWriteAndExpect(shell,
                        "cp /root/iou/lab_enterprise/IFMAP /root/iou/public/iou2net.pl /root/iou/public/iourc /root/iou/public/stop.sh ./ ",
                        si.innersysname);
                }
                else
                    return "思科交换机公共文件生成失败";
            }

            return "配置文件生成成功!";
        }
        catch (System.Exception ex)
        {
            return ex.Message;
        }
    }