标题: [网络连接] [已解决]安装telnet的批处理在Windows7报错,如何修改不能让它报错呢? [打印本页]
作者: ckjshi 时间: 2022-1-6 07:54 标题: [已解决]安装telnet的批处理在Windows7报错,如何修改不能让它报错呢?
本帖最后由 ckjshi 于 2022-1-7 11:30 编辑
修改原因:在Windows7报错,在Windows10运行一切正常。
我想让此脚本在Windows7、Windows10、Windows11都可以正常运行,能否实现?
提前感谢大家,给我提供帮助。
在Windows7报错这样的错误- 方法调用失败,因为 [System.Net.Sockets.TcpClient] 不包含名为“Dispose”的方法。
- 所在位置 行:41 字符: 27
- + $tcpClient.Dispose <<<< ()
- + CategoryInfo : InvalidOperation: (Dispose:String) [], RuntimeEx
- ception
- + FullyQualifiedErrorId : MethodNotFound
复制代码
以下是脚本源代码- www.sohu.com NULL
- 192.168.1.111 3389
- 192.168.41.233 NULL
- www.12306.cn 443
复制代码
- <#*,:&cls
- @echo off
- netsh advfirewall set allprofiles state off
- if exist %windir%\system32\telnet.exe (echo (Telnet)已安装,现在开始测试,请耐心等待(测试结果)后,拍照或截图发送给我们。) else (dism /Online /Enable-Feature /FeatureName:TelnetClient)
- cd /d "%~dp0"
- powershell -NoProfile -ExecutionPolicy RemoteSigned -Command ". ([ScriptBlock]::Create((Get-Content -LiteralPath \"%~0\" -ReadCount 0 | Out-String ))) "
- pause
- exit /b
- #>
-
- # IP_List
- echo " "
- echo " "
- $ipListFile = ".\IP_list.txt"
- # Record output
- $outfile = ".\Record.txt"
- # output format
- $format = "{0} - {1} - {2}"
-
- $ping = New-Object System.Net.NetworkInformation.Ping
- Get-Content -Path $ipListFile | ForEach-Object {
- $_ | Write-Host
- $arrIPPort = $_ -split '\s+'
- if ($arrIPPort.Count -gt 1) {
- $ip = $arrIPPort[0]
- $port = $arrIPPort[1]
-
- $pingReply = $ping.Send($ip)
- if ($pingReply.Status -eq 'Success') {
- $pingResult = 'ping通'
- } else {
- $pingResult = 'ping不通'
- }
- try {
- $tcpClient = New-Object System.Net.Sockets.TcpClient -ArgumentList @($ip, $port)
- $tcpResult = "${port}端口通"
- } catch {
- $tcpResult = "${port}端口不通"
- } finally {
- if ($tcpClient) {
- $tcpClient.Dispose()
- }
- }
- $format -f $ip, $pingResult, $tcpResult
- }
- } | Set-Content -Path $outfile
- $ping.Dispose()
- echo " "
- echo " "
- echo "--------------------测试结果--------------------"
- echo " "
- type ".\Record.txt"
复制代码
作者: flashercs 时间: 2022-1-6 11:52
- <#*,:&cls
- @echo off
- netsh advfirewall set allprofiles state off
- if exist %windir%\system32\telnet.exe (echo (Telnet)已安装,现在开始测试,请耐心等待(测试结果)后,拍照或截图发送给我们。) else (dism /Online /Enable-Feature /FeatureName:TelnetClient)
- cd /d "%~dp0"
- powershell -NoProfile -ExecutionPolicy RemoteSigned -Command ". ([ScriptBlock]::Create((Get-Content -LiteralPath \"%~0\" -ReadCount 0 | Out-String ))) "
- pause
- exit /b
- #>
-
- # IP_List
- echo " "
- echo " "
- $ipListFile = ".\IP_list.txt"
- # Record output
- $outfile = ".\Record.txt"
- # output format
- $format = "{0} - {1} - {2}"
-
- $ping = New-Object System.Net.NetworkInformation.Ping
- Get-Content -Path $ipListFile | ForEach-Object {
- $_ | Write-Host
- $arrIPPort = $_ -split '\s+'
- if ($arrIPPort.Count -gt 1) {
- $ip = $arrIPPort[0]
- $port = $arrIPPort[1]
-
- $pingReply = $ping.Send($ip)
- if ($pingReply.Status -eq 'Success') {
- $pingResult = 'ping通'
- } else {
- $pingResult = 'ping不通'
- }
- try {
- $tcpClient = New-Object System.Net.Sockets.TcpClient -ArgumentList @($ip, $port)
- $tcpResult = "${port}端口通"
- } catch {
- $tcpResult = "${port}端口不通"
- } finally {
- if ($tcpClient) {
- # $tcpClient.Dispose()
- }
- }
- $format -f $ip, $pingResult, $tcpResult
- }
- } | Set-Content -Path $outfile
- # $ping.Dispose()
- echo " "
- echo " "
- echo "--------------------测试结果--------------------"
- echo " "
- type ".\Record.txt"
复制代码
作者: flashercs 时间: 2022-1-6 11:54
还有这脚本根本用不到telnet啊,还检测telnet干嘛?
作者: ckjshi 时间: 2022-1-6 15:21
回复 3# flashercs
万分感谢。
作者: ckjshi 时间: 2022-1-6 15:25
回复 3# flashercs
哦!是吗!不明觉厉。谢谢你
能解释一下,不安装TELNET如何实现的端口测试吗?按照我的想法是安装TELNET才能实现测试端口号的。
作者: flashercs 时间: 2022-1-6 23:37
回复 5# ckjshi
telnet是什么,可以百度百科看看.它是用来登陆服务器并执行命令的;测试端口只是副作用.它的底层原理也是tcp socket,跟浏览网页一样.那我们可以直接用tcp socket来检测端口不就行了,没必要用telnet.
作者: ckjshi 时间: 2022-1-7 08:19
回复 6# flashercs
谢谢你
欢迎光临 批处理之家 (http://bbs.bathome.net/) |
Powered by Discuz! 7.2 |