通过PowerShell发送TCP请求
很多时候我们需要通过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请求的更多相关文章
- jmeter测试TCP服务器/模拟发送TCP请求
jmeter测试TCP服务器,使用TCP采样器模拟发送TCP请求. TCP采样器:打开一个到指定服务器的TCP / IP连接,然后发送指定文本并等待响应. jmeter模拟发送TCP请求的方法: 1. ...
- jmeter ---测试TCP服务器/模拟发送TCP请求
jmeter测试TCP服务器/模拟发送TCP请求 jmeter测试TCP服务器,使用TCP采样器模拟发送TCP请求. TCP采样器:打开一个到指定服务器的TCP / IP连接,然后发送指定文本并等待响 ...
- jmeter测试TCP服务器/模拟发送TCP请求 设置16进制发送(转)
转载留存:http://blog.sina.com.cn/s/blog_46d0362d0102v8ii.html 性能测试需要模拟多种场景,经常受制于资源限制,没办法建立贴近实际部署环境的场景.因而 ...
- PowerShell收发TCP消息包
PowerShell收发TCP消息包 https://www.cnblogs.com/fuhj02/archive/2012/10/16/2725609.html 在上篇文章中,我们在PSNet包中创 ...
- go tcp发送网络请求
//发送http请求 package main import ( "fmt" "net" "io" ) func main () { //使 ...
- Android系列之网络(一)----使用HttpClient发送HTTP请求(通过get方法获取数据)
[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...
- shell脚本一条命令直接发送http请求(xjl456852原创)
我们知道nc命令是一个网络工具.可以连接tcp/udp.也能模拟发送http请求. 现在介绍通过shell脚本,一条命令直接发送http请求. 命令如下,可以对下面的地址等信息自行修改: #!/bin ...
- (一)----使用HttpClient发送HTTP请求(通过get方法获取数据)
(一)----使用HttpClient发送HTTP请求(通过get方法获取数据) 一.HTTP协议初探: HTTP(Hypertext Transfer Protocol)中文 “超文本传输协议”,是 ...
- 【ESP8266】发送HTTP请求
一.ESP8266简介 ESP8266 是深圳安信可科技有限公司开发的基于乐鑫ESP8266的超低功耗的UART-WIFI模块的模组,可以方便进行二次元开发,接入云端服务,实现手机3/4G全球随时随地 ...
随机推荐
- PHP会话Session
<?php //开启会话,PHP会话也提供多种存储方式,文件.数据库等 session_start(); if(isset($_GET['user'])) { $_SESSION['user'] ...
- SQLSERVER truncate table之后是否会重置表的自增值
SQLSERVER truncate table之后是否会重置表的自增值 今天清理业务库数据的时候,开发人员说可以使用truncate table把两个表的所有数据清理掉 这两个表都有自增ID,都做了 ...
- Eclipse的常用快捷键、旁门左道、系统错误小贴士
转自:http://usenrong.iteye.com/blog/1113700 eclipse是一个开源产品,目前比较流行和普遍的JAVA IDE(集成开发环境)它使用工程化方式管理.虽然ecli ...
- 在VS中自定义代码段
这个功能不怎么实用,但毕竟是VS存在的一个功能点嘛,知道一点也好!说它不怎么实用是有原因的,因为现在强大的VS编辑器拥有不计其数的插件,而且这些插件也有很多很强大的!比如Resharper,Code ...
- javascript中的call()和apply()方法的使用
1.方法定义 call方法: 语法:call([thisObj[,arg1[, arg2[, [,.argN]]]]]) 定义:调用一个对象的一个方法,以另一个对象替换当前对象. 说明: call ...
- AJAX跨域调用相关知识-CORS和JSONP(引)
AJAX跨域调用相关知识-CORS和JSONP 1.什么是跨域 跨域问题产生的原因,是由于浏览器的安全机制,JS只能访问与所在页面同一个域(相同协议.域名.端口)的内容. 但是我们项目开发过程中,经常 ...
- python 守护进程 daemon
python 守护进程 daemon # -*-coding:utf-8-*- import sys, os '''将当前进程fork为一个守护进程 注意:如果你的守护进程是由inetd启动的,不要这 ...
- Java基础的一次总结
二重循环核心:就是我们学习的内容中,一直在不停的打印图形,当我们写完所有打印图形的题目后,我们站在一个 新的高度上来看二重循环解决图形问题的时候.我们知道核心思想就是:就是寻找外层循环变量i和内层循环 ...
- JavaScript事件详解-zepto的事件实现
zepto的event 可以结合上一篇JavaScript事件详解-原生事件基础(一)综合考虑源码暂且不表,github里还有中文网站都能下到最新版的zepto.整个event模块不长,274行,我们 ...
- java利用16进制来辨别png格式的图片
很多人知道利用.png的字符串结尾可以判断前端传入的图片是否为png格式,但是这只是潜意识的判断!那么如何利用png读写的特殊内容来深意识地判断图片格式呢?最近在做东西的时候遇到了点问题,在加载图片的 ...