結構化iis的配置項、解析xml到資料庫
阿新 • • 發佈:2022-03-16
立志於做一個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 |
/// <summary>
/// iis網站model
/// </summary>
public class IIsWebSiteModel
{
/// <summary>
/// 序號
/// </summary>
public int id { get ; set ; }
/// <summary>
/// 站點名稱
/// </summary>
public string name { get ; set ; }
/// <summary>
/// 路徑
/// </summary>
public string path { get ; set ; }
/// <summary>
/// 應用程式池
/// </summary>
public string applicationPool { get ; set ; }
/// <summary>
/// 應用程式池 2
/// </summary>
public string protocol { get ; set ; }
/// <summary>
/// 繫結域名和埠資訊
/// </summary>
public string bindingInformation { get ; set ; }
/// <summary>
/// 物理路徑
/// </summary>
public string physicalPath { get ; set ; }
/// <summary>
/// (自定義欄位)網站型別
/// </summary>
public string webType { get ; set ; }
/// <summary>
/// 配置檔案路徑,自己判斷是否是web.config
/// </summary>
public string webConfigPath { get ; set ; }
/// <summary>
/// 服務自動啟動
/// </summary>
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<IIsWebSiteModel> GetIIsConfigList()
{
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<IIsWebSiteModel> siteList = new List<IIsWebSiteModel>();
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,平臺眾多,如果作業系統崩潰了那就麻煩了,必要時對網站進行備份,然後再恢復是個不錯的選擇。先來個工具截圖。