结构化iis的配置项、解析xml到数据库
立志于做一个C#开发宝典,拿来即用,整理个人工作中遇到的问题,服务器运维、开发、控件、开发小工具、大数据等领域,C#开发宝典 开发宝典预览版
当我们遇到一个iis有大于20个以上的站点时,主要是自己内部测试或者日常的平台部署,站点比较多了,此时我们可以对配置项结构化到数据库中:
主要用途:
1、站点扫描,获取文件列表:
类似于safe dog 安全狗iis版等,根据对站点的文件进行扫描,我们容易取到当前站点的文件信息;
2、批量获取用户等信息
我们可以把配置项更新到数据库,对数据库的用户表进行读取或者同步,修改用户信息等,当然也可以使用接口的方式,这里指的是读取链接字符串来修改用户表;
3、批量修改数据库密码
服务器定期的数据库安全,我们可能会不定期的修改sa密码,那么我们就需要每个平台都去修改连接字符; 我们可以找到20多个站点的配置文件信息,例如Web.config,直接一键打开,修改密码
其他功能发掘中。。。欢迎交流。。。。
首先我们要知道打开iis的快捷命令,inetmgr,就可以
iis的程序目录在 C:\Windows\System32\inetsrv
直观的我们打开iis就查看到了如下界面:
表结构如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
CREATE TABLE dbo.tool_IIS_Config
(
configId INT IDENTITY NOT NULL ,
name NVARCHAR (500),
id INT ,
serverAutoStart NVARCHAR (500),
virtualDirectory NVARCHAR (500),
applicationPool NVARCHAR (500),
applicationPoolPath NVARCHAR (500),
bindingInformation NVARCHAR (500),
physicalPath NVARCHAR (500),
status NVARCHAR (500),
createTime DATETIME CONSTRAINT DF_tool_IIS_Config_createTime DEFAULT (getdate()) NOT NULL ,
webconfigPath NVARCHAR (500),
webType NVARCHAR (500),
CONSTRAINT PK_tool_IIS_Config PRIMARY KEY (configId)
)
GO
|
首先建一个类,用户接受解析后的结果
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
///
/// iis网站model
///
public class IIsWebSiteModel
{
///
/// 序号
///
public int id { get ; set ; }
///
/// 站点名称
///
public string name { get ; set ; }
///
/// 路径
///
public string path { get ; set ; }
///
/// 应用程序池
///
public string applicationPool { get ; set ; }
///
/// 应用程序池 2
///
public string protocol { get ; set ; }
///
/// 绑定域名和端口信息
///
public string bindingInformation { get ; set ; }
///
/// 物理路径
///
public string physicalPath { get ; set ; }
///
/// (自定义字段)网站类型
///
public string webType { get ; set ; }
///
/// 配置文件路径,自己判断是否是web.config
///
public string webConfigPath { get ; set ; }
///
/// 服务自动启动
///
public string serverAutoStart { get ; set ; }
}
|
然后解析的核心代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
public static List
{
XmlDocument xmlDoc = new XmlDocument();
string iisConfigFile = @"C:\Windows\System32\inetsrv\config\applicationHost.config" ;
if (!File.Exists(iisConfigFile))
{
iisConfigFile = Application.StartupPath + "\\applicationHost.config" ;
}
if (!File.Exists(iisConfigFile))
{
return null ;
}
List< string > webSiteList = new List< string >(); //网站目录
List new List
if (System.IO.File.Exists(iisConfigFile))
{
xmlDoc.LoadXml(System.IO.File.ReadAllText(iisConfigFile));
//string docNodes = "//site//virtualDirectory";//文档数据
string docNodes = "//system.applicationHost//sites//site" ; //所有网站节点
foreach (XmlNode item in xmlDoc.SelectNodes(docNodes))
{
string name = item.Attributes[ "name" ] != null ? item.Attributes[ "name" ].Value : string .Empty;
string id = item.Attributes[ "id" ].Value;
var siteModel = new IIsWebSiteModel();
string serverAutoStart = item.Attributes[ "serverAutoStart" ] != null
? item.Attributes[ "serverAutoStart" ].Value
: string .Empty;
siteModel.serverAutoStart = serverAutoStart;
XmlNodeList applicationNoteList = item.SelectNodes( "application" );
XmlNodeList bindingsNoteList = item.SelectNodes( "bindings" );
if (applicationNoteList != null && applicationNoteList.Count > 0)
{
XmlNode sitemNode = applicationNoteList[0].SelectNodes( "virtualDirectory" )[0];
if (sitemNode != null )
{
string path = sitemNode.Attributes[ "path" ].Value;
string physicalPath = sitemNode.Attributes[ "physicalPath" ].Value;
if (path == "/" )
{
siteModel.name = name;
siteModel.id = Convert.ToInt32(id);
siteModel.physicalPath = physicalPath;
siteModel.path = path;
}
string webConfigPath = "" ;
if (!physicalPath.EndsWith( "\\" ))
{
physicalPath = physicalPath + "\\" ;
}
// webSiteList.Add(physicalPath);
siteModel.webConfigPath = webConfigPath;
siteModel.webType = webType;
}
}
if (bindingsNoteList != null && bindingsNoteList.Count > 0)
{
XmlNode sitemNode = bindingsNoteList[0].SelectNodes( "binding" )[0];
if (sitemNode != null )
{
//此处是端口信息的获取,为了避免非程序员上车,请自己解析,完整代码,请下载开发宝典,哈哈哈。开发宝典网页版,http://code.51diysoft.com/
}
}
siteList.Add(siteModel);
}
siteList = siteList.OrderBy(p => p.id).ToList(); //根据id升序
}
return siteList;
}
|
我们下一节 讲:如何备份和还原iis,平台众多,如果操作系统崩溃了那就麻烦了,必要时对网站进行备份,然后再恢复是个不错的选择。先来个工具截图。