[英]How to pass arguments in netstat command?
I am using a netstat
command in VBScript to find out the ports which are listening. 我在VBScript中使用
netstat
命令找出正在监听的端口。 As a process, I am reading the IP address and port number from two variables. 作为一个过程,我正在从两个变量中读取IP地址和端口号。
A file contains an IP address, port number which were separated by commas as shown below: 一个文件包含一个IP地址,端口号,它们之间用逗号分隔,如下所示:
10.x.x.x,3389
Like there are multiple IPs and port numbers existing in a file. 就像文件中存在多个IP和端口号一样。 By using array concept I am fetching the IP address and port number from the file using
myarray(0)
and myarray(1)
variables. 通过使用数组概念,我使用
myarray(0)
和myarray(1)
变量从文件中获取IP地址和端口号。
My code looks like this: 我的代码如下所示:
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = WScript.CreateObject("WScript.Shell")
Dim port_number, port_status, port_conn_ip, portread, portfile, PSExec, PCIExec
objShell.CurrentDirectory = "C:\Program Files (x86)\GnuWin32\bin"
Set objfile = objFSO.openTextFile("C:\IBM\port_mon\list_of_ports.txt")
Do until objFile.AtEndOfStream
portread = objfile.ReadLine
portfile = Split(portread, ",")
Set PSExec = objShell.Exec("%comspec% /k ""netstat -an |find ""myvalue(0) ""|find ""myvalue(1)""|awk ""{print $4}""""")
port_status = PSExec.Stdout.ReadLine
WScript.Echo port_status
But here myvalue(0)
and myvalue(1)
are not updating with IP address or port number. 但是在这里,
myvalue(0)
和myvalue(1)
不会使用IP地址或端口号进行更新。 I also tried to provide like " & myvalue(0) & "
but this also didn't give me any result. 我也尝试提供
" & myvalue(0) & "
但这也没有给我任何结果。 Always the output is empty. 输出始终为空。
As a background work, I have verified the connectivity of IP address and port number both are in connected and established state. 作为背景工作,我已经验证了IP地址和端口号的连通性都处于已连接和已建立状态。
The string concatenation approach was the right thing to do. 字符串串联方法是正确的做法。 VBScript doesn't expand variables inside strings.
VBScript不会在字符串内扩展变量。 However, the name of your array variable is
portfile
, not myvalue
(or myarray
). 但是,数组变量的名称是
portfile
,而不是myvalue
(或myarray
)。 Put Option Explicit
at the beginning of your script to detect mistakes like that. 将
Option Explicit
放在脚本的开头,以检测类似的错误。
Change your code to something like this, and it should do what you expect: 将您的代码更改为这样的代码,它应该可以实现您所期望的:
cmd = "%comspec% /k ""netstat -an | find """ & portfile(0) & """ | find """ & _
portfile(1) & """ | awk ""{print $4}"""""
Set PSExec = objShell.Exec(cmd)
BTW, you don't need to call find
twice for address and port (assuming that both refer to a remote host). 顺便说一句,您不需要为地址和端口调用
find
两次(假设它们都引用远程主机)。 Just filter for IP:port
: 只需过滤
IP:port
:
cmd = "%comspec% /k ""netstat -an | find """ & portfile(0) & ":" & _
portfile(1) & """ | awk ""{print $4}"""""
Set PSExec = objShell.Exec(cmd)
And as I mentioned in the comments to your previous question, you also don't need to print a column with awk
and then read and compare the output. 正如我在上一个问题的注释中提到的那样,您也不需要打印带有
awk
的列,然后阅读并比较输出。 Just use the Run
method, add another find
step to check if the line contains the text "ESTABLISHED", and then check the exit code of the command. 只需使用
Run
方法,添加另一个find
步骤来检查该行是否包含文本“ ESTABLISHED”,然后检查命令的退出代码。
cmd = "%comspec% /c ""netstat -an | find """ & portfile(0) & ":" & _
portfile(1) & """ | find ""ESTABLISHED"""""
rc = objShell.Run(cmd, 0, True)
If rc = 0 Then
'connection established
Else
'no established connection found
End If
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.