Windows下确定使用某端口的程序名

主要思路是使用netstat输出端口号对应的pid及程序名,再使用其他程序解析输出。

使用grep+tlist过滤(使用grep需安装cygwin)

netstat -ano|grep -ine "port_number" #Get pid.
tlist /fi "pid eq pid_number" #Get process name.

使用gawk过滤(需安装cygwin)

netstat -anbo | gawk "!/9150/{matchLine=$0} /9150/{print matchLine}"

使用指定var=val对,简化匹配模式的输入:

netstat -anbo | gawk -v pid="9150" '$0~pid{print matchLine}{matchLine=$0}'
#######简短写法:将var=val对放在后面########
netstat -anbo | gawk '\$0~pid{print matchLine}{matchLine=\$0}' pid="9150"

使用grep过滤(需安装cygwin)

netstat -anbo|grep -B1 -ine “port_number” #Get process name.

使用sed过滤(需安装cygwin)

netstat -anbo | sed -n “/9150/{x;p;d};x”

注意!

由于netstat输出格式的问题,本文方法可能不准确!需要进一步改进
如以下netstat输出片段,用本文方法将无法获得预期结果。

[Microsoft.VsHub.Server.HttpHostx64.exe] TCP [::1]:57386
[::1]:49154 TIME_WAIT 0 TCP [::1]:57392
[::1]:49154 TIME_WAIT 0 TCP [::1]:57421
[::1]:49155 TIME_WAIT 0 TCP [::1]:57422
[::1]:49155 TIME_WAIT 0 TCP [::1]:57424
[::1]:49154 ESTABLISHED 1692

refs:
1. http://stackoverflow.com/questions/17943738/grep-print-line-before-dont-print-match
2. http://stackoverflow.com/questions/4891383/awk-print-matching-line-and-line-before-the-matched
3. http://unix.stackexchange.com/questions/206886/print-previous-line-after-a-pattern-match-using-sed

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值