Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Powershell tcp listener

3,103 views
Skip to first unread message

Frank

unread,
Apr 3, 2008, 6:50:00 PM4/3/08
to
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]

unread,
Apr 3, 2008, 7:51:18 PM4/3/08
to

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]

unread,
Apr 3, 2008, 7:52:46 PM4/3/08
to
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

unread,
Apr 3, 2008, 10:45:01 PM4/3/08
to

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

unread,
Apr 4, 2008, 12:56:14 AM4/4/08
to
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

unread,
Apr 4, 2008, 1:00:52 AM4/4/08
to
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]

unread,
Apr 4, 2008, 3:45:00 AM4/4/08
to
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]

unread,
Apr 4, 2008, 8:00:32 AM4/4/08
to
> >> - 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]

unread,
Apr 4, 2008, 9:17:26 AM4/4/08
to
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

unread,
Apr 4, 2008, 11:53:43 AM4/4/08
to
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]

unread,
Apr 4, 2008, 12:32:13 PM4/4/08
to

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

unread,
Apr 4, 2008, 1:42:00 PM4/4/08
to
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

unread,
Apr 4, 2008, 2:29:58 PM4/4/08
to
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]

unread,
Apr 4, 2008, 2:41:45 PM4/4/08
to
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

unread,
Apr 4, 2008, 5:42:00 PM4/4/08
to
That works! thank you. Can I easliy modify this to echo out what I type in
from the telnet session?

eric...@gmail.com

unread,
Jul 23, 2013, 2:11:18 PM7/23/13
to
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 new messages