很多时候我们需要通过Socket发送特定的TCP请求给服务器的特定端口来实现探测服务器的指定端口所开启的服务。很多语言都有相应的方法实现上述需求,当然,PowerShell也不例外,比如我们要发送一个简单的http请求到指定的web服务器:

GET / HTTP/1.1
Host:cn.bing.com

这里我们想请求微软必应的中文首页,如果需要通过PowerShell向cn.bing.com服务器发送get请求,就需要创建一个System.Net.Sockets.TcpClient对象,向指定的服务器和端口发送请求。

具体代码如下:

        =====文件名:Send-TcpRequest.ps1=====
########################################
# Send-TcpRequest.ps1
## Send a TCP request to a remote computer, and return the response.
## If you do not supply input to this script (via either the pipeline, or the
## -InputObject parameter,) the script operates in interactive mode.
##
## Example:
##
## $http = @"
## GET / HTTP/1.1
## Host:cn.bing.com
## `n`n
## "@
##
## $http | .\Send-TcpRequest cn.bing.com 80
########################################
param(
[string] $remoteHost = "localhost",
[int] $port = 80,
[switch] $UseSSL,
[string] $inputObject,
[int] $commandDelay = 100
) [string] $output = "" ## Store the input into an array that we can scan over. If there was no input,
## then we will be in interactive mode.
$currentInput = $inputObject
if(-not $currentInput)
{
$SCRIPT:currentInput = @($input)
}
$scriptedMode = [bool] $currentInput function Main
{
## Open the socket, and connect to the computer on the specified port
if(-not $scriptedMode)
{
write-host "Connecting to $remoteHost on port $port"
} trap { Write-Error "Could not connect to remote computer: $_"; exit }
$socket = new-object System.Net.Sockets.TcpClient($remoteHost, $port) if(-not $scriptedMode)
{
write-host "Connected. Press ^D followed by [ENTER] to exit.`n"
} $stream = $socket.GetStream() if($UseSSL)
{
$sslStream = New-Object System.Net.Security.SslStream $stream,$false
$sslStream.AuthenticateAsClient($remoteHost)
$stream = $sslStream
} $writer = new-object System.IO.StreamWriter $stream while($true)
{
## Receive the output that has buffered so far
$SCRIPT:output += GetOutput ## If we're in scripted mode, send the commands,
## receive the output, and exit.
if($scriptedMode)
{
foreach($line in $currentInput)
{
$writer.WriteLine($line)
$writer.Flush()
Start-Sleep -m $commandDelay
$SCRIPT:output += GetOutput
} break
}
## If we're in interactive mode, write the buffered
## output, and respond to input.
else
{
if($output)
{
foreach($line in $output.Split("`n"))
{
write-host $line
}
$SCRIPT:output = ""
} ## Read the user's command, quitting if they hit ^D
$command = read-host
if($command -eq ([char] 4)) { break; } ## Otherwise, Write their command to the remote host
$writer.WriteLine($command)
$writer.Flush()
}
} ## Close the streams
$writer.Close()
$stream.Close() ## If we're in scripted mode, return the output
if($scriptedMode)
{
$output
}
} ## Read output from a remote host
function GetOutput
{
## Create a buffer to receive the response
$buffer = new-object System.Byte[] 1024
$encoding = new-object System.Text.AsciiEncoding $outputBuffer = ""
$foundMore = $false ## Read all the data available from the stream, writing it to the
## output buffer when done.
do
{
## Allow data to buffer for a bit
start-sleep -m 1000 ## Read what data is available
$foundmore = $false
$stream.ReadTimeout = 1000 do
{
try
{
$read = $stream.Read($buffer, 0, 1024) if($read -gt 0)
{
$foundmore = $true
$outputBuffer += ($encoding.GetString($buffer, 0, $read))
}
} catch { $foundMore = $false; $read = 0 }
} while($read -gt 0)
} while($foundmore) $outputBuffer
}
. Main

该脚本使用方法如下:

$http = @"

GET / HTTP/1.1

Host:cn.bing.com

`n`n

"@

$http | .\Send-TcpRequest cn.bing.com 80

执行效果如图所示:

需要说明的是,由于页面返回的内容太长了,这里至少是将返回的内容缓存在一个变量里,并只输出了变量的头10行。

有了这个脚本,我们就可以向指定的web服务器发送特定的请求,来实现模拟登陆和操作的功能了。

 

作者: 付海军

出处:http://fuhj02.cnblogs.com

版权:本文版权归作者和博客园共有

转载:欢迎转载,为了保存作者的创作热情,请按要求【转载】,谢谢

要求:未经作者同意,必须保留此段声明;必须在文章中给出原文连接且保证内容完整!否则必究法律责任!

个人网站: http://www.fuhaijun.com/

通过PowerShell发送TCP请求的更多相关文章

  1. jmeter测试TCP服务器/模拟发送TCP请求

    jmeter测试TCP服务器,使用TCP采样器模拟发送TCP请求. TCP采样器:打开一个到指定服务器的TCP / IP连接,然后发送指定文本并等待响应. jmeter模拟发送TCP请求的方法: 1. ...

  2. jmeter ---测试TCP服务器/模拟发送TCP请求

    jmeter测试TCP服务器/模拟发送TCP请求 jmeter测试TCP服务器,使用TCP采样器模拟发送TCP请求. TCP采样器:打开一个到指定服务器的TCP / IP连接,然后发送指定文本并等待响 ...

  3. jmeter测试TCP服务器/模拟发送TCP请求 设置16进制发送(转)

    转载留存:http://blog.sina.com.cn/s/blog_46d0362d0102v8ii.html 性能测试需要模拟多种场景,经常受制于资源限制,没办法建立贴近实际部署环境的场景.因而 ...

  4. PowerShell收发TCP消息包

    PowerShell收发TCP消息包 https://www.cnblogs.com/fuhj02/archive/2012/10/16/2725609.html 在上篇文章中,我们在PSNet包中创 ...

  5. go tcp发送网络请求

    //发送http请求 package main import ( "fmt" "net" "io" ) func main () { //使 ...

  6. Android系列之网络(一)----使用HttpClient发送HTTP请求(通过get方法获取数据)

    [声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...

  7. shell脚本一条命令直接发送http请求(xjl456852原创)

    我们知道nc命令是一个网络工具.可以连接tcp/udp.也能模拟发送http请求. 现在介绍通过shell脚本,一条命令直接发送http请求. 命令如下,可以对下面的地址等信息自行修改: #!/bin ...

  8. (一)----使用HttpClient发送HTTP请求(通过get方法获取数据)

    (一)----使用HttpClient发送HTTP请求(通过get方法获取数据) 一.HTTP协议初探: HTTP(Hypertext Transfer Protocol)中文 “超文本传输协议”,是 ...

  9. 【ESP8266】发送HTTP请求

    一.ESP8266简介 ESP8266 是深圳安信可科技有限公司开发的基于乐鑫ESP8266的超低功耗的UART-WIFI模块的模组,可以方便进行二次元开发,接入云端服务,实现手机3/4G全球随时随地 ...

随机推荐

  1. 平衡树模板 bzoj 3224

    program t3224; var tr:array[-1..1000000,1..2] of int64; num,fa,size,quan:array[-1..1000000] of int64 ...

  2. Java的历史

    1991 绿色计划 (Green Project) 1991 年 1 月 一个名为"Green Project"的项目启动.该项旨在为家用电器提供支持,使这些电器智能化并且能够彼此 ...

  3. Dynamic CRM 2013学习笔记(三十一)自定义用excel批量导入实体数据

    有一个实体的子表数据量太大,于是客户想用execel来导入实体数据.首先想到的是用系统自带的Import Data,客户嫌太麻烦,比如lookup字段要做map等. 下面是具体的实现步骤: 一.定义e ...

  4. SQL Server中DateTime与DateTime2的区别

    DateTime字段类型对应的时间格式是 yyyy-MM-dd HH:mm:ss.fff ,3个f,精确到1毫秒(ms),示例 -- ::15.433 . DateTime2字段类型对应的时间格式是  ...

  5. 检查密码复杂度的C#正则表达式

    在用户注册与修改.重置密码时,强制密码达到一定的复杂度,是减少盗号的有效措施之一. 而在代码中检查密码复杂度就需要用到正则表达式,比如要求密码必须包含数字.小写或大写字母.特殊字符.字符数在8-30之 ...

  6. Agile 是什么?

    也许你已经习惯了 “Agile” 这个词汇不断地在你耳边狂轰滥炸,诸如敏捷团队.敏捷UX.敏捷建模.敏捷需求管理.敏捷架构等等.而且,人们还在不断的将 “Agile” 和更多的词汇进行组合,比如,也曾 ...

  7. Fragment之间的通信

    在本节中,你会学到 1.定义接口 2.实现接口 3.将消息传递给fragment 为了重用Fragment UI 组件,在设计中你应该通过定义每一个fragemnt自己的layout和行为,让frag ...

  8. javascript高级程序设计阅读笔记(一)

    javascript高级程序设计阅读笔记(一) 工作之余开发些web应用作为兴趣,在交互方面需要掌握javascript和css.HTML5等技术,因此读书笔记是必要的. javascript简介 J ...

  9. Hadoop Capacity Scheduler源码实现剖析

    作者: 大圆那些事 | 文章可以转载,请以超链接形式标明文章原始出处和作者信息 网址: http://www.cnblogs.com/panfeng412/archive/2013/09/13/had ...

  10. Ubuntu SVN客户端安装

    查看系统版本: uname -a (Linux查看版本当前操作系统内核信息) cat /proc/version (Linux查看当前操作系统版本信息) 1.首先需要安装Ubuntu SVN.Ubun ...