符明
摘要:隨著學校的信息化建設的逐步推進,各類服務器和網絡系統規模不斷增大,對服務器的運行維護也愈發重要。該文通過將Powershell命令運用到NET MVC 5架構網站中,來實現一種遠程服務器無須安裝額外軟件即可進行服務器遠程監 控的方法。
關鍵詞:PowerShell;遠程;MVC 5;Visual Basic
中圖分類號:TP393
文獻標識碼:A
文章編號:1009-3044(2020)04-0007-02
收稿日期:2019-12-05
隨著信息化建設的快速發展,各類應用系統以及網絡系統的規模不斷加大,各種操作系統、數據庫、應用軟件、中間件的數量和類型也不斷增多,IT信息系統越來越復雜,學校逐漸意 識到IT運維監控的重要性。
1 Powershell的簡單介紹
PowerShell是構建于.NET上基于任務的命令行shell和腳本語言。PowerSheU可幫助系統管理員和高級用戶快速自動執行用于管理操作系統(Linux、macOS和Windows)和流程的任務。
一個校園局域網內部有若干WEB.SQL服務器,利用Invoke-Command命令,在執行命令后不保持連接,遠程主機上的Power Shell進程立即退岀。通過在命令后面添加腳本塊,執行后將其推送到目標主機上,然后從目標主機返回結果。
Windows PowerShell遠程管理使用WS-Management協議,Windows PowerShell遠程處理可以在一臺或多臺遠程計算機上運行任何Windows PowerShell命令。用戶可以建立持久連接、啟動交互會話并在遠程計算機上運行腳本。Invoke-Command可在本地和遠程計算機運行,返回包括錯誤在內的所有信息。
2MVC5的簡單介紹
ASP.NET MVC 5是Microsoft開發的一種網站應用程序框架,是使用“模型Model-視圖View-控制器Controller”設計模式構建Web應用和API的豐富框架。模型Model表示應用程序特定方面的狀態。控制器Controller處理交互并更新模型以反映應用程序狀態的更改,然后將信息傳遞給視圖Viewo視圖View接受來自控制器Controller的必要信息,并呈現用戶界面以顯示該信息。
3?以獲得服務器CPU負載為例的實現
頁面前端釆用JQuery的setlnterval()與$普.getJSON()方法。以10秒為間隔,不刷新頁面獲得所需參數。
3.1 前端HTML的JavaScript
$(document).ready(function (){
setlnterval(function (){
$ .getJSON(“home/cpuload? ip=yourServerIP&usemame=administrator&password=yourpassword
",function (data){
$("#cpuloading").text(data.cpuloading);
});
},10000)
})
其中:
1)setlnterval(code,millisec[,”lang”])方法可按照指定的周期(以毫秒計)來調用函數或計算表達式。setlnterval()方法會不停地調用函數,直到clearlnterval()被調用或窗口被關閉。
2)jQuery.getJSON(url,data,success(data,status,xhr))通過HTTP GET請求載入JSON數據。
3.2?前端 HTML
DIV標簽中的文本直接由Javascript讀取Json數據包不刷新頁面寫入。
3.3?后端MVC Control控制器
代碼釆用Visual Basic.NET調用系統PowerShell進行遠程控制。網站必須首先調用PowerShell模塊,才能在項目中運行 PowerShell命令。在代碼上中首先調用如下DLL:
Imports System.Collections.ObjectModel
Imports System.Management.Automation
Imports System.Management.Automation.Runspaces
Function CPULoad(ByVal ip As String,ByVai username As String,ByVai password As String)As JsonResult
'Your Codes Here
Return Json(New With {.cpuloading=cpuLoading),JsonRequestBehavior.AllowGet)
End Function
代碼將返回Json數據包給前端頁面JavaScript顯示。
3.4 遠程獲得Windows和Ubuntu服務器CPU負載信息
3.4.1 創建PowerShell遠程登錄Windows 2016憑證
遠程服務器操作系統為Windows 2016,原生PowerShell版 本為5.1。
Dim SecuredPass=New SecureString
For Each c As Char In password
SecuredPass.AppendChar(c)
Next
Dim Credential As PSCredential=New PSCredential(user-name,SecuredPass)
3.4.2 PowerShell 中 Get-WmiObject-Class Win32_Proces-sorlselect LoadPercentage 獲得 Windows 2016 服務器 CPU信息
PowerShell 中利用 Invoke-Command [[-ComputerName] < String[] >] [-Credential
完成功能代碼如下:
Dim setRemoteRights=True
Dim psi As PowerShell
Dim initial As InitialSessionState=InitialSessionState.Creat-eDefault()
Dim runspace As Runspace 二 RunspaceFactory.CreateRuns-pace(initial)
runspace.Open()
psi=PowerShell .Create
psi.Runspace=runspace
Dim SecuredPass=New SecureString
For Each c As Char In password
SecuredPass.AppendChar(c)
Next
Dim Credential As PSCredential=New PSCredential(user-name,SecuredPass)
myScript="Get-WmiObject-Class Win32_Processorlselect LoadPercentage "
psi.AddStatement。
psi.AddCommand(" Invoke—C ommand ")
psi.AddParameter("ComputerName",ip)
Dim block As ScriptBlock=ScriptBlock.Create(myScript)
psi.AddParameter('*ScriptBlock'*,block)
psi.AddParameter("Authentication","Negotiate")psi.AddParameter("Credential",Credential)
Dim myResult As Collection(Of PSObject)=psi.Invoke
Dim cpuLoading=myResult.FirstQ.Members(" LoadPercentage").Value
runspace.CloseQ runspace.Dispose。
3.4.3 PowerShell 利用 POSH-SSH 執行 Bash 命令獲得 Ubuntu 18.04服務器CPU信息
直到PowerShell 6.0,Microsoft才支持在Linux中直接運行 PowerShell命令。PowerShell 5.1 則需要安裝第三方POSH-SSH 模塊,通過SSH協議在遠程Linux執行Bash命令,以獲得服務器
CPU負載信息。Ubuntu服務器需要安裝SSH Server服務。
POSH-SSH 中使用 New-SSHSession [-ComputerName] < string[]〉[-Credential]
1)配置本地訪問權限,調用第三方POSH-SSH模塊
Dim scriptinvoker As Runspacelnvoke=New Runspaceln-voke(runspace)
scriptinvoker.Invoke("Set—ExecutionPolicy —Scope Curren-
tUser Unrestricted")
psi.AddCommand("Import—Module")
psi.AddParameter(”Name”,"Posh-SSH")
2)完整功能代碼如下:
Dim mybash As String=String.Empty
Dim setRemoteRights=True
Dim psi As PowerShell
Dim initial As InitialSessionState=InitialSessionState.Creat-eDefault()
Dim runspace As Runspace=RunspaceFactory.CreateRuns-pace(initial)
runspace.OpenQ
Dim scriptinvoker As Runspacelnvoke=New Runspaceln-voke(runspace)
scriptinvoker.Invoke("Set—ExecutionPolicy —Scope Curren-tUser Unrestricted")
psi=PowerShell.Create
psi.Runspace=runspace
Dim SecuredPass=New SecureString
For Each c As Char In password
SecuredPass.AppendChar(c)
Next
Dim Credential As PSCredential=New PSCredential(username,SecuredPass)
psi.AddStatementQ psi.AddCommand("Import—Module")psi.AddParameter(”Name”,"Posh-SSH")psi.AddStatement()psi.AddCommand("New-SSHSession")psi.AddParameterC'ComputerName",ip)psi.AddParameter(' * Credential'',Credential)psi.AddParameter("Force")psi.AddStatement()
psi.AddC ommand("Invoke—SSHC ommand ")
psi.AddParameter("SessionId",0)psi.AddParameter("Command","top-bnllgrep,Cpu(s)/")Dim myResult As Collection(Of PSObject)=psi.Invoke Dim cpuloding As StringQ=myResult.Last().Members("Out-put").Value
Dim cpuload As String=cpuloding(O)
Dim match As Match=Regex.Match(cpuload,"\d*\.\d",Re-gexOptions.IgnoreCase)
Dim outcpuload=match.Groups(0).Value
runspace.CloseQ
runspace.Dispose。
4 研究結論
方法優點:MVC網頁服務器通過運行不同的Powershell和Bash命令就能對遠程計算機(Windows、Linux、MacOS)進行遠程監控。在強大的Jquery第三方圖像控件的支持下,可以圖形化數據,使得遠程服務器無須安裝額外軟件即可被監控。
方法缺點:無法實時監控遠程計算機信息。遠程計算機的信息是通過PowerShell對遠程計算機執行命令所獲取。通過對虛擬機的觀察,當對遠程計算機命令執行設置刷新時間為1秒時,則遠程服務器CPU負載將直接上升20%-30%,這個后果是不合適的。因此設置一個適當的命令刷新速度是必要的。
發展方向:Microsoft公司已把PowerShell6.0開源,促使應用能跨平臺使用。今后PowerShell將可以在Windows,Linux,MacOS服務器中發揮更大的作用。
參考文獻:
[1] DonJones,JefferyHick.WindowsPowerShell實戰指南[M].宋沃劍,譯.北京:人民郵電出版社,2016.
[2] 朱育發.jQuery與jQueryMobile開發完全技術寶典[M].北京:中國鐵道出版社,2014.
[3] AdamFreeman.精通ASP.NETMVC5[M].北京:人民郵電出版社,2016.
[通聯編輯:謝媛媛]