Google Grupper understøtter ikke længere nye Usenet-opslag eller -abonnementer. Tidligere indhold er fortsat synligt.

Powershell tcp listener

3.073 visninger
Gå til det første ulæste opslag

Frank

ulæst,
3. apr. 2008, 18.50.0003.04.2008
til
Hi,

Is there an easy way to create a simple Powershell TCP listener? I would
like to create one for tcp port receive testing.

Thanks in advance,


Oisin (x0n) Grehan [MVP]

ulæst,
3. apr. 2008, 19.51.1803.04.2008
til

function listen-port ($port) {
$endpoint = new-object System.Net.IPEndPoint ([ipaddress]::any,
$port)
$listener = new-object System.Net.Sockets.TcpListener $ep
$listener.start()
$listener.AcceptTcpClient() # will block here until connection
$listener.stop()
}


ps> listen-port 12345

Now, in another instance of powershell (or cmd.exe):

C:\> telnet localhost 12345

and that's it! the function will spit out connection info and return
control to you.

hope this helps,

- Oisin

PowerShell MVP
http://www.nivot.org/

Oisin (x0n) Grehan [MVP]

ulæst,
3. apr. 2008, 19.52.4603.04.2008
til
On Apr 3, 7:51 pm, "Oisin (x0n) Grehan [MVP]" <ois...@gmail.com>
wrote:

oops,

$listener = new-object System.Net.Sockets.TcpListener $ep

should be

$listener = new-object System.Net.Sockets.TcpListener $endpoint

sorry!

- Oisin

Frank

ulæst,
3. apr. 2008, 22.45.0103.04.2008
til

I get the following error, am I suppose to put in the IPaddress for the
"from" host or can I just put a wildcard to let any host come in?

Unable to find type [ipaddress]: make sure that the assembly containing this
type is loaded.
At line:2 char:60
+ $endpoint = new-object System.Net.IPEndPoint ([ipaddress]: <<<<
:any,$port)
New-Object : Constructor not found. Cannot find an appropriate constructor
for type System.Net.Sockets.TcpListener.
At line:3 char:25
+ $listener = new-object <<<< System.Net.Sockets.TcpListener $endpoint
You cannot call a method on a null-valued expression.
At line:4 char:18
+ $listener.start( <<<< )
You cannot call a method on a null-valued expression.
At line:5 char:28
+ $listener.AcceptTcpClient( <<<< ) # will block here until connection
You cannot call a method on a null-valued expression.
At line:6 char:17
+ $listener.stop( <<<< )

Steven Murawski

ulæst,
4. apr. 2008, 00.56.1404.04.2008
til
Frank,

It should be


function listen-port ($port)
{
$endpoint = new-object System.Net.IPEndPoint

[system.net.ipaddress]::any, $port)
$listener = new-object System.Net.Sockets.TcpListener $endpoint


$listener.start()
$listener.AcceptTcpClient() # will block here until connection
$listener.stop()
}

Steven MurawskiC

steven....@gmail.com

ulæst,
4. apr. 2008, 01.00.5204.04.2008
til
Frank,

The function should be:


function listen-port ($port) {
$endpoint = new-object System.Net.IPEndPoint

([system.net.ipaddress]::any, $port)
$listener = new-object System.Net.Sockets.TcpListener $endpoint


$listener.start()
$listener.AcceptTcpClient() # will block here until connection
$listener.stop()
}

Steven Murawski
Host - Powershell Basics
http://powershell-basics.com

> > - Oisin- Hide quoted text -
>
> - Show quoted text -

RichS [MVP]

ulæst,
4. apr. 2008, 03.45.0004.04.2008
til
If you are interested in working at the network level with PowerShell have
you seen the netcmdlets from /n software

They have a new beat available for testing

http://www.nsoftware.com/powershell/netcmdlets/v2.aspx
--
Richard Siddaway
Please note that all scripts are supplied "as is" and with no warranty
Blog: http://richardsiddaway.spaces.live.com/
PowerShell User Group: http://www.get-psuguk.org.uk

Oisin (x0n) Grehan [MVP]

ulæst,
4. apr. 2008, 08.00.3204.04.2008
til
> >> - Oisin- Hide quoted text -
>
> - Show quoted text -

Thanks Steven,

I'm using PowerShell v2, and it appears to import System.Net so for
me, [ipaddress] works. :)

- Oisin

Oisin (x0n) Grehan [MVP]

ulæst,
4. apr. 2008, 09.17.2604.04.2008
til
On Apr 4, 8:00 am, "Oisin (x0n) Grehan [MVP]" <ois...@gmail.com>

Having thought about this a bit more, it seems this might be a bit of
a needless breaking change in v2. It's a pretty insidious kind of
break too. While I find the import really useful in terms of saving
the extra typing, it's easy enough to avoid the newer cmdlets in order
to keep scripts backwards compatible, but this kind of thing is
extremely difficult to weed out.

Thoughts, anyone?

- Oisin

Kiron

ulæst,
4. apr. 2008, 11.53.4304.04.2008
til
I'm also using v2 CTP but can't access [ipaddress], have to append 'net.' to access it. I don't think is a v2 feature. Could it be a Snapin?
 
# have you tried?
powershell -nop [ipaddress]

--
Kiron

Oisin (x0n) Grehan [MVP]

ulæst,
4. apr. 2008, 12.32.1304.04.2008
til

Hi Kiron - I'm not using the same v2 as you - it's an internal build
(mvp privelege). It looks like this feature was added after the first
public CTP.

- Oisin

Frank

ulæst,
4. apr. 2008, 13.42.0004.04.2008
til
Now I get this error:

[C:\]: listen-port 12345


New-Object : Constructor not found. Cannot find an appropriate constructor

for type System.Net.IPEndPoint.
At line:2 char:26


+ $endpoint = new-object <<<< System.Net.IPEndPoint


IPAddressToString : 0.0.0.0
Address : 0
AddressFamily : InterNetwork
ScopeId :
IsIPv6Multicast : False
IsIPv6LinkLocal : False
IsIPv6SiteLocal : False

12345


New-Object : Constructor not found. Cannot find an appropriate constructor
for type System.Net.Sockets.TcpListener.

At line:4 char:26


+ $listener = new-object <<<< System.Net.Sockets.TcpListener $endpoint
You cannot call a method on a null-valued expression.

At line:5 char:19


+ $listener.start( <<<< )
You cannot call a method on a null-valued expression.

At line:6 char:29


+ $listener.AcceptTcpClient( <<<< ) # will block here until connection
You cannot call a method on a null-valued expression.

At line:7 char:18
+ $listener.stop( <<<< )

Steven Murawski

ulæst,
4. apr. 2008, 14.29.5804.04.2008
til
Frank,

I get the same error with the function if I do not enclose the
parameters for the $endpoint variable in parenthesis like below:

$endpoint = New-Object ([System.Net.IPEndPoint]::any, $port)

Steven Murawski
Host - PowerShell Basics
http://www.powershell-basics.com

Oisin (x0n) Grehan [MVP]

ulæst,
4. apr. 2008, 14.41.4504.04.2008
til
On Apr 4, 2:29 pm, Steven Murawski <steven.muraw...@gmail.com> wrote:
> Frank,
>
> I get the same error with the function if I do not enclose the
> parameters for the $endpoint variable in parenthesis like below:
>
> $endpoint = New-Object ([System.Net.IPEndPoint]::any, $port)
>
> Steven Murawski
> Host - PowerShell Basicshttp://www.powershell-basics.com
> >>> - Show quoted text -- Hide quoted text -

>
> - Show quoted text -

The problem is that the line is getting wrapped - it should be:

$endpoint = new-object System.Net.IPEndPoint `
([system.net.ipaddress]::any, $port)

I just placed a back-tick line continuation character above there.

- Oisin

Frank

ulæst,
4. apr. 2008, 17.42.0004.04.2008
til
That works! thank you. Can I easliy modify this to echo out what I type in
from the telnet session?

eric...@gmail.com

ulæst,
23. jul. 2013, 14.11.1823.07.2013
til
To re-cap: Running PowerShell V2
Code:

function listen-port ($port) {
$endpoint = New-Object System.Net.IPEndPoint ([system.net.ipaddress]::any, $port)
$listener = new-object System.Net.Sockets.TcpListener $endpoint
$listener.start()
$listener.AcceptTcpClient() # will block here until connection
$listener.stop()
}

This worked, you have to be careful with wordwrap. This answered once and then the function stops. I am working on a nice confirmation on the source end that it worked and would like to revive this thread

Thanks,

Eric
0 nye opslag