首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >批处理文件以检查远程服务器的特定端口的可用性。

批处理文件以检查远程服务器的特定端口的可用性。
EN

Stack Overflow用户
提问于 2014-11-21 09:35:09
回答 1查看 2.3K关注 0票数 1

嗨,我正在尝试制作一个bat文件来检查从不同服务器到一个特定服务器的可用性,以便检查它们之间是否存在防火墙,或者某种阻止连接的东西。问题是我不能使用第三方软件像portqry,nmap .所以我被困在这里,每一个小费都会有帮助的。

提前感谢XD,

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-11-21 19:28:37

可以手动使用telnet。

代码语言:javascript
复制
echo.|telnet google.com 80

但是telnet输出不能被处理,所以它对脚本没有用处。

这里有一个解决方案,它或多或少是欺骗(因为它创建了一个.exe文件),但不是下载。因为它将二进制数据包含在十六进制字符串中,所以它有点长,所以我不得不使用pastebin.com

另一个way.Using jscript.net/批处理混合(应该保存为bat),它还创建了一个小的exe文件。但是,设置套接字连接超时并不是那么简单的任务,因此如果端口不可用,它将等待太长时间:

代码语言:javascript
复制
@if (@X)==(@Y) @end /****** silent line that start jscript comment ******

@echo off
::::::::::::::::::::::::::::::::::::
:::       compile the script    ::::
::::::::::::::::::::::::::::::::::::
setlocal
::if exist portchk.exe goto :skip_compilation

set "frm=%SystemRoot%\Microsoft.NET\Framework\"
:: searching the latest installed .net framework
for /f "tokens=* delims=" %%v in ('dir /b /s /a:d /o:-n "%SystemRoot%\Microsoft.NET\Framework\v*"') do (
    if exist "%%v\jsc.exe" (
        rem :: the javascript.net compiler
        set "jsc=%%~dpsnfxv\jsc.exe"
        goto :break_loop
    )
)
echo jsc.exe not found && exit /b 0
:break_loop


%jsc% /nologo /out:"portchk.exe" "%~dpsfnx0"
::::::::::::::::::::::::::::::::::::
:::       end of compilation    ::::
::::::::::::::::::::::::::::::::::::
:skip_compilation

:: download the file


 ::::just change the link and the file::::
 portchk.exe google.com 80
 portchk.exe google.com 666


exit /b 0


****** end of jscript comment ******/

import System;
import System.Net.Sockets;
import System.Timers;

var arguments:String[] = Environment.GetCommandLineArgs();



var remote_host=arguments[1];
var remote_port=arguments[2];

//var ipa= (IPAddress) Dns.GetHostAddresses(remote_host)[0];

var sock=new System.Net.Sockets.Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);

try {
    sock.Connect(remote_host, remote_port);
    //sock.ConnectAsync(remote_host, remote_port);
    timer_connection = new Timer();

    Console.WriteLine("connected to something");

}catch (e) {

    Console.WriteLine("Port is probably not available");
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27058173

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档