Powershell脚本从txt文件扫描给定服务器的端口 [英] Powershell script Scan ports of given servers from txt file

查看:92
本文介绍了Powershell脚本从txt文件扫描给定服务器的端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究一个项目,只是为了学习Powershell。我遇到的一个问题是设置一个可以从机器扫描端口的脚本。我使用的脚本使用txt文件作为端口和服务器。

I have been working on a project off and on just to learn Powershell. One issue I am running into is setting up a script that can scan ports from machines. The script I have uses txt files for for the ports and servers.

我希望我的脚本要做的是从txt文件扫描机器并确定端口是否打开,然后转到下一台机器并根据它扫描它查看该端口是否打开的下一行。有点像一个循环。现在,该脚本只能使用每个txt文件中的一行来生成
,但如果我添加更多,则会缩短.b

What I would like my script to do is scan a machine from the txt file and determine if the port is open, then go to the next machine and scan it based on the next line to see if that port is open. Kind of like a loop. Right now the script will only work with a single line from each txt file but if I add more it craps out.

以下是我原始脚本的副本。

Below is a copy of my original script.

############################ ############################################ $ $ $ b#

#  Powershell NetMon端口监视器脚本

# 

# 脚本扫描端口以检查状态是否可用




######################### ##############################################



####################################### ##################################
#  HTML变量

######################################## ################################ $
$
$ dated = "< CENTER>"

$ date = $ dated +(Get-Date -format F)

$ a ="< META HTTP-EQUIV =刷新CONTENT = 15>" b $ b $ a a = $ a +"< style>"

$ a = $ a +" BODY {background-color :#646D7E;}"
$
$ a = $ a +" TABLE {border-width:1px; border-style:solid; border-color:black; border-collapse:collapse;} "
$
$ a = $ a +" TH {border-width:1px; padding:3px; border-style:solid; border-color:black;}"

$ a = $ a +" TD {border-width:1px; padding:6px; border-style:solid; border-color:black;}"

$ a = $ a +"< / style>"
$


####################### ################################################ <无线电通信/>
# 从配置文件中获取内容

##################################### ###################################
$
$ hostid =(get-content  C:\Scripts\cfg \ porthosts.cfg | Where {-not($ _.StartsWith('#'))})

$ portnumber = (get-content C:\Scripts\portcfg.cfg | Where {-not($ _.StartsWith('#'))})





############################################ ############################
# 检查端口的状态

###################################### ################################## $
$
$ socket = new-object Net.Sockets.TcpClient



$ socketresults = $ socket.Connect($ hostid,$ portnumber)



if($ socket.Connected){

$ status ="Port Open"

$ socket.Close()

}
$
其他{

$ status ="港口关闭"

}



$ b = @"

< CENTER>

$日期< BR />

< TABLE>

< TR bgcolor =#2554C7>

< TD>主机< / TD> < TD>端口号< / TD> < TD>状态< / TD>

< / TR>

< TR bgcolor =#5CB3FF>

< TD> $主机标识< / TD> < TD> $端口号< / TD> < TD> $ status< / TD>

< / TR>

< / TABLE>

< / CENTER>

" @



######################## ###############################################

#Export to HTML

################################# ####################################### $

$ statusupdate | ConvertTo-HTML -head $ a -body $ b | out-file C:\Scripts\gen\porthosts.htm


#######################################################################
#
#  Powershell NetMon Port Monitor Script
#  
#  Script scans port to check status if available
#
#######################################################################

#######################################################################
#  HTML Variables
#######################################################################

$dated = "<CENTER>"
$dated = $dated + (Get-Date -format F)
$a = "<META HTTP-EQUIV=refresh CONTENT=15>"
$a = $a + "<style>"
$a = $a + "BODY{background-color:#646D7E;}"
$a = $a + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}"
$a = $a + "TH{border-width: 1px;padding: 3px;border-style: solid;border-color: black;}"
$a = $a + "TD{border-width: 1px;padding: 6px;border-style: solid;border-color: black;}"
$a = $a + "</style>"

#######################################################################
#  Get content from config files
#######################################################################

$hostid = (get-content  C:\Scripts\cfg\porthosts.cfg | Where {-not ($_.StartsWith('#'))})
$portnumber = (get-content C:\Scripts\portcfg.cfg | Where {-not ($_.StartsWith('#'))})


#######################################################################
#  Check status of ports
#######################################################################

$socket = new-object Net.Sockets.TcpClient

$socketresults = $socket.Connect($hostid, $portnumber)

if ($socket.Connected) {
$status = "Port Open"
$socket.Close()
}
else {
$status = "Port Closed"
}

$b = @"
<CENTER>
$dated<BR />
<TABLE>
<TR bgcolor=#2554C7>
<TD>Hosts</TD> <TD>Port Number</TD> <TD>Status</TD>
</TR>
<TR bgcolor=#5CB3FF>
<TD>$hostid</TD> <TD>$portnumber</TD> <TD>$status</TD>
</TR>
</TABLE>
</CENTER>
"@

#######################################################################
#Export to HTML
#######################################################################

$statusupdate | ConvertTo-HTML -head $a -body $b | out-file C:\Scripts\gen\porthosts.htm

我想这可能是我的最后一个主要部分需要调整的网络监控脚本。是的我知道有成千上万的网络监控工具,但这对我学习Powershell及其语法非常有用。一旦完成,
我将把它公开,以便其他的I.T.专业人士可以玩它。

I think this is probably the last major part of my network monitoring scripts that needs tweaking. Yes I know there are hundreds of thousands of network monitoring tools but this is pretty useful for me to learn Powershell and its syntax. Once this is complete I will make it public so that other I.T. professionals can play with it.

提前感谢您的帮助!

推荐答案

通常我们会使用PortQry,因为它可以执行查询端口身份等操作,并且可以检查RPC以查看有关端口的其他项目。

Normally we would use PortQry because it can do things like query the port for identity and it can check RPC to see other items about a port.

PortQry可以兼容的方式输出到PowerShell。

PortQry can output to PowerShell in a compatible way.

http://www.microsoft.com/en-us/download/details.aspx?id=17148


这篇关于Powershell脚本从txt文件扫描给定服务器的端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆