国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > .NET > 正文

VB.NET開(kāi)發(fā)掃描客戶端服務(wù)工具

2024-07-10 13:02:40
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
  在大中型企業(yè)信息系統(tǒng)中,對(duì)客戶端pc的管理,往往是容易出現(xiàn)問(wèn)題的環(huán)節(jié)。因此,很多大公司引入了各種分布式的管理系統(tǒng),例如防病毒方面的norton antivirus,blackice防火墻,微軟的 sms (system management server),等等,這些系統(tǒng)都會(huì)在客戶端安裝相應(yīng)的客戶端軟件,一般都是以服務(wù)的形式出現(xiàn),但是由于種種原因,這些服務(wù)會(huì)停止運(yùn)行或者該客戶機(jī)根本沒(méi)有安裝這些客戶端服務(wù),這樣管理系統(tǒng)就會(huì)出現(xiàn)疏漏,有可能造成問(wèn)題,如因無(wú)法防御病毒而成為病毒源,無(wú)法為該客戶端發(fā)布軟件,無(wú)法管理客戶pc等等。在此,我們提供一個(gè)方案,可以定時(shí)按照ip地址掃描網(wǎng)絡(luò),報(bào)告出特定的服務(wù)的狀態(tài)。

  這個(gè)方案使用了microsoft.net技術(shù),同時(shí)也用到了.net framework中的ado.net ,wmi management,xml。其核心是一個(gè)由vb.net寫(xiě)的程序以及它的兩個(gè)配置文件,配置文件為xml格式,該程序按ip掃描網(wǎng)絡(luò),得到每個(gè)系統(tǒng)的服務(wù) 的狀態(tài),如果ip地址沒(méi)有對(duì)應(yīng)系統(tǒng),則忽略該ip,針對(duì)沒(méi)有安裝服務(wù)或服務(wù)停止的系統(tǒng)我們?cè)诹硪粋€(gè)線程中運(yùn)行nbtstat命令,得到其機(jī)器名,用戶名,mac地址域等信息,以便我們找到機(jī)器解決問(wèn)題。其次為了保存掃描的結(jié)果,我們需要一個(gè)很小的數(shù)據(jù)庫(kù)ms-access或ms-sql server都可以,本文使用sql2000 。最后為了呈現(xiàn)出掃描的結(jié)果,以便我們采取行動(dòng),這里我們使用網(wǎng)頁(yè)的形式把數(shù)據(jù)庫(kù)中的結(jié)果展現(xiàn)出來(lái)。

  1. vb.net程序

  該程序使用兩個(gè)xml格式的配置文件,當(dāng)程序啟動(dòng)時(shí)會(huì)讀入這些配置。其中一個(gè)文件定義了需要掃描的網(wǎng)段,包括排除在外的地址段。另一個(gè)文件定義了連接數(shù)據(jù)庫(kù)的信息,以及數(shù)據(jù)表的定義。這兩個(gè)文件的內(nèi)容如下:

<iplist>

<ip lanid="192.168.100." ><exp l=”1” h=”30”/></ip>

<ip lanid="192.168.101." />

<ip lanid="192.168.102." />

<ip lanid="192.168.103." />

<ip lanid="192.168.104." ><exp l=”1” h=”40”/></ip>

</iplist>


  該文件定義將要掃描5個(gè)網(wǎng)段,其中兩個(gè)網(wǎng)段有些地址需要排除在外(分配給打印機(jī)等設(shè)備),對(duì)于192.168.100段,我們排除從1到30,對(duì)于192.168.104段我們排除1到40。

<dbinfo>

<server>dbserver</server>

<database>db</database>

<uid>report</uid>

<pwd>report</pwd>

<service table=”service”>service</service>

</dbinfo>

  該文件定義了連接數(shù)據(jù)庫(kù)所需的信息

tag meaning
<server> scanservice數(shù)據(jù)庫(kù)的服務(wù)器名
<database> scanservice數(shù)據(jù)庫(kù)名
<uid> 用于更新scanservice數(shù)據(jù)庫(kù)的數(shù)據(jù)庫(kù)用戶名
<pwd> 用于更新scanservice數(shù)據(jù)庫(kù)的數(shù)據(jù)庫(kù)用戶的密碼
<service> 該tag的 inner 定義了我們希望掃描的service的名字,
這里我們假定希望掃描服務(wù)名為service。
該tag的屬性定義了數(shù)據(jù)庫(kù)中表名,該表用于保存掃描結(jié)果。

  ‘首先我們定義一個(gè)類,主要用于得到某個(gè)ip地址的service的狀態(tài)信息,并在服務(wù)狀態(tài)不正常時(shí)觸發(fā)另一線程得到該系統(tǒng)的詳細(xì)信息。

imports system.serviceprocess

imports system.xml

imports system.threading

public class getstatus

private iservicename as string ‘服務(wù)的名稱

private imachineip as string ‘ip地址

private itable as string ‘在dateset中的表名

‘構(gòu)造函數(shù)

sub new(byval ip as string, byval svcname as string, byval updatetable as string)

imachineip = ip

iservicename = svcname

itable = updatetable

end sub

‘每個(gè)線程所運(yùn)行的方法,用于得到服務(wù)的狀態(tài),如果狀態(tài)不正常則觸發(fā)另一線程得到該ip的信息

sub getstausf()

dim servicep as new servicecontroller() ‘實(shí)例化一個(gè)servicecontroller類

servicep.machinename = imachineip

servicep.servicename = iservicename

dim myrow as datarow

dim status as string

dim run as boolean = false

myrow = ds.tables(itable).newrow

try

if servicep.status.tostring <> "running" then

status = servicep.status.tostring‘如果狀態(tài)不是running則將狀態(tài)賦予字符串變量

else

run = true ‘如果狀態(tài)為running,則不做任何事

end if

catch er as exception ‘以下處理取得狀態(tài)時(shí)候發(fā)生的異常

status = left(er.message, 35)

if instr(status, "service control manager") = 0 then

status = "not installed or open service failed" ‘沒(méi)有安裝該服務(wù)

elseif instr(er.message, "manager") > 0 then

status = "can not detected" ‘服務(wù)的狀態(tài)不可得

end if

end try

servicep.close() ‘關(guān)閉servicecontroller實(shí)例

‘以下判斷如果狀態(tài)不是running,則記錄該系統(tǒng),并觸發(fā)線程得到它的詳細(xì)信息。

if not run then

myrow("msg") = status

myrow("ip") = imachineip

synclock gettype(addrow) ‘為保證多線程情況下,對(duì)dataset只有一個(gè)寫(xiě)操作,鎖定addrow類

dim addrowins as new addrow(myrow) ‘將ip和狀態(tài)通過(guò)我們自己寫(xiě)的addrow類插入dataset

end synclock

‘觸發(fā)另一線程取得機(jī)器信息

dim hostinfo2 as new hostinfo(imachineip)

dim hostthr2 as new thread(new threadstart(addressof hostinfo2.sysinfo))

hostthr2.start()

synclock gettype(hostinfothreadcounter)

hostinfothreadcounter.counter += 1 ‘啟動(dòng)線程數(shù)加1

end synclock

end if

synclock gettype(stoppcounter)

stopthr.addstop()

end synclock

end sub

end class

‘該類只有一個(gè)方法,就是將停止的線程數(shù)減1

class stoppcounter

sub addstop()

threadcounterstopped = threadcounterstopped + 1

end sub

end class

‘此類用于將已有的行插入dataset

class addrow

‘第一個(gè)構(gòu)造函數(shù),以構(gòu)造好的行為輸入?yún)?shù)

sub new(byval row as datarow)

try

ds.tables(0).rows.add(row)

catch ee as exception



end try

end sub

‘第二個(gè)構(gòu)造函數(shù),以機(jī)器名用戶名等字符串為參數(shù),更新已有的行

sub new(byval ip as string, byval user as string, byval hostname as string, byval mac as string, byval domain as string, byval timeout as char)

dim rowtimeout as datarow

try

for each rowtimeout in ds.tables(0).select("ip='" & ip & "'")

rowtimeout.item("lastuid") = user

rowtimeout.item("name") = hostname

rowtimeout.item("mac") = mac

rowtimeout.item("domain") = domain

rowtimeout.item("timeout") = timeout 'set timeout flag to this item

exit for 'just run once

next

catch er as exception



end try

end sub

end class

‘由于篇幅限制,這里省略了根據(jù)ip取得機(jī)器信息的類的代碼。

imports system.threading ‘用于支持多線程

imports system.xml ‘用于分析xml格式的參數(shù)文件

imports system.data ‘用于保存結(jié)果到數(shù)據(jù)庫(kù)

module module1

public ds as new dataset()

public conn1 as sqlclient.sqlconnection ‘?dāng)?shù)據(jù)庫(kù)連接

public ipf as string ‘ip列表文件名

public dbf as string ‘?dāng)?shù)據(jù)庫(kù)信息文件

public threadcounterstopped as integer

public stopthr as new stoppcounter()

sub main() ‘程序主程序

dim machineip as string

dim iplistf as new xml.xmldocument()

dim iplist as xml.xmlnode

dim ipitem as xml.xmlnode

dim dbinfof as new xml.xmldocument()

dim dbinfo as xml.xmlnode

dim lanid as string

dim i as integer

dim timestart as integer

dim threadcounterstarted as integer


threadcounterstarted = 0

threadcounterstopped = 0

dim server as string

dim database as string

dim uid as string

dim pwd as string

dim table as string

dim connstr, connstr1 as string

dim servicename as string

dim purgestr as string

try

dbinfof.load(dbf) ‘讀取數(shù)據(jù)庫(kù)信息文件

catch nodb as exception

msgbox(nodb.message & "wrong db info file name.")

exit sub

end try

try

iplistf.load(ipf) ‘讀取ip列表文件

catch noip as exception

msgbox(noip.message & "wrong ip list file name.")

exit sub

end try

‘分析數(shù)據(jù)庫(kù)信息文件

dbinfo = dbinfof.childnodes(0)

server = dbinfo.childnodes(0).innertext

database = dbinfo.childnodes(1).innertext

uid = dbinfo.childnodes(2).innertext

pwd = dbinfo.childnodes(3).innertext

servicename = dbinfo.childnodes(4).innertext

table = dbinfo.childnodes(4).attributes(0).value

‘根據(jù)分析所得,構(gòu)造連接字符串

connstr1 = "server=" & server & ";database=" & database & ";uid=" & uid & ";password=" & pwd

conn1 = new sqlclient.sqlconnection(connstr1) ‘實(shí)例化數(shù)據(jù)庫(kù)連接

conn1.open() ‘打開(kāi)數(shù)據(jù)庫(kù)連接

dim sa as sqlclient.sqldataadapter = new sqlclient.sqldataadapter("select * from " & table, conn1)

dim combu as new sqlclient.sqlcommandbuilder(sa)

sa.fill(ds, table) ‘填充dataset

ds.clear() ‘清空舊的數(shù)據(jù)

dim ipaddress as string

‘分析ip列表文件

iplist = iplistf.childnodes(0)

dim ai as integer

dim ipexcepcount as integer

dim ipexcep as xml.xmlnode

for each ipitem in iplist.childnodes

dim excep(2, 83) as integer

lanid = ipitem.attributes(0).value‘得到網(wǎng)絡(luò)id

for i = 2 to 254 ‘從2到254,根據(jù)每個(gè)網(wǎng)絡(luò)id構(gòu)造ip地址

ai = 0

‘以下判斷是為了跳過(guò)保留地址段

if ipitem.haschildnodes then

ipexcepcount = ipitem.childnodes.count

redim excep(2, ipexcepcount - 1)

for each ipexcep in ipitem.childnodes

excep(0, ai) = cint(ipexcep.attributes(0).value)

excep(1, ai) = cint(ipexcep.attributes(1).value)

ai = ai + 1

next

end if

for ai = 0 to ipexcepcount - 1

if i >= excep(0, ai) and i <= excep(1, ai) then

console.writeline("跳過(guò)保留地址: " & lanid & i.tostring)

goto skipip

end if

next

machineip = lanid & i.tostring ‘ip地址

‘以下觸發(fā)線程以,得到服務(wù)狀態(tài)

dim getst as new getstatus(machineip, servicename, table)

dim getstthread as new thread(new threadstart(addressof getst.getstausf))

getstthread.start()

threadcounterstarted = threadcounterstarted + 1‘啟動(dòng)線程數(shù)加1

console.writeline("線程" & machineip & " 啟動(dòng)。檢測(cè) " & servicename)

‘每啟動(dòng)100個(gè)線程,程序主線程停止15秒,避免太多線程造成內(nèi)存溢出

if (threadcounterstarted mod 100) = 0 then

console.writeline("等待 .......")

thread.currentthread.sleep(15000)

gc.collect() 'force garbage collection to aviod outofmemory when run with long ip list

end if

skipip:

next

next

console.writeline("exiting program ...") ‘所有線程都已觸發(fā)

finish:

thread.currentthread.sleep(5000) ‘以下程序等待所有線程結(jié)束

gc.collect()

if threadcounterstopped = threadcounterstarted and hostinfothreadcounter.counter = hostinfothreadcounter.counterstop then‘如果觸發(fā)線程等于結(jié)束線程

dim row as data.datarow

for each row in ds.tables(table).rows

row.item("systime") = now

next

purgestr = "delete " & table

dim com1 as new sqlclient.sqlcommand(purgestr, conn1)

com1.executenonquery() ‘刪除舊記錄

sa.insertcommand = combu.getinsertcommand

sa.update(ds, table) ' 將新記錄寫(xiě)入數(shù)據(jù)庫(kù)

else

goto finish ' goto finish and wait another 30 seconds

end if

end sub

  可以利用如下命令在dos窗口啟動(dòng)該程序。

scanservice –i iplist.xml –d dbinfo.xml


  2. scanservice 數(shù)據(jù)庫(kù)

  該數(shù)據(jù)庫(kù)保存保存程序運(yùn)行結(jié)果,以便用web等方式展現(xiàn)出來(lái)。以下是建立表的腳本,包含域名,用戶名,機(jī)器名,ip以及服務(wù)狀態(tài)。

create table [dbo].[service] (

[ip] [varchar] (50) null ,

[狀態(tài)] [varchar] (50) null ,

[用戶名] [varchar] (50) null ,

[機(jī)器名] [varchar] (50) null ,

[mac地址] [varchar] (50) null ,

[域] [varchar] (50) null ,

[超時(shí)] [varchar] (10) null ,

[時(shí)間安] [datetime] (8) null ,

)

  總結(jié):

  以上是一個(gè)完整的方法,也是比較簡(jiǎn)單明晰的解決方法,如果要求技巧和性能的話,還有一些地方可以做些改進(jìn),比如對(duì)線程池的使用。另外還有一些方面需要大家自己完成,比如將數(shù)據(jù)庫(kù)中的信息以web的方式展現(xiàn)出來(lái)。

商業(yè)源碼熱門下載www.html.org.cn

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 科尔| 平阴县| 嘉祥县| 信丰县| 寿光市| 云林县| 怀化市| 漳平市| 临洮县| 新蔡县| 安达市| 阳西县| 固阳县| 奉贤区| 阜新市| 沧州市| 万荣县| 密云县| 泾源县| 德令哈市| 顺平县| 久治县| 德保县| 安新县| 宝丰县| 莆田市| 开远市| 新巴尔虎右旗| 醴陵市| 巴青县| 通道| 故城县| 太仓市| 崇礼县| 定结县| 潢川县| 宁海县| 荔波县| 赤峰市| 常山县| 义马市|