# Need to check a Socket but with a  source IP

**URL:** <https://forums.powershell.org/t/need-to-check-a-socket-but-with-a-source-ip/2814>\
**Category:** PowerShell Help\
**Created:** [July 11, 2014, 8:52pm UTC](https://forums.powershell.org/t/need-to-check-a-socket-but-with-a-source-ip/2814 "2014-07-11T20:52:50Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![windows-liveuser25](https://avatars.discourse-cdn.com/v4/letter/w/ac8455/32.png) [@windows-liveuser25](https://forums.powershell.org/u/windows-liveuser25)\
**Post date:** [July 11, 2014, 8:52pm UTC](https://forums.powershell.org/t/need-to-check-a-socket-but-with-a-source-ip/2814/1 "2014-07-11T20:52:50Z")

</div>

$socketC = New-object Net.Sockets.TcpClient  
$socketC.Connect(“10.224.34.1”,636)  
$socket.Connected

Need to check a Socket but with a source IP. Above code is connected form “localhost”. Please help how to put remote IP instead of localhost?

---

<div class="post-metadata">

**Author:** ![daniel-krebs](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/daniel-krebs/32/484_2.png) [@daniel-krebs](https://forums.powershell.org/u/daniel-krebs)\
**Post date:** [July 11, 2014, 9:03pm UTC](https://forums.powershell.org/t/need-to-check-a-socket-but-with-a-source-ip/2814/2 "2014-07-11T21:03:40Z")

</div>

Biswajit,

Please elaborate what you’re trying to do. I assume from your example code that you’re trying to execute port checks towards Active Directory Domain Controllers.

Please keep in mind that every connection leaving your test machine to a remote IP will have the source IP of the network adapter that has an explicit route or default gateway. The localhost IP (127.0.0.1) is not used to communicate with remote machines (ever).

Best,  
Daniel

---

<div class="post-metadata">

**Author:** ![richard-siddaway](https://avatars.discourse-cdn.com/v4/letter/r/d78d45/32.png) [@richard-siddaway](https://forums.powershell.org/u/richard-siddaway)\
**Post date:** [July 11, 2014, 9:24pm UTC](https://forums.powershell.org/t/need-to-check-a-socket-but-with-a-source-ip/2814/3 "2014-07-11T21:24:10Z")

</div>

If you are trying to connect to machine C from machine B and your local machine is machine A I don’t think that will work they way you want. My understanding of the Connect method is that you connect to a remote machine from your local machine - [http://msdn.microsoft.com/en-us/library/fkbhht5w(v=vs.110).aspx](http://msdn.microsoft.com/en-us/library/fkbhht5w(v=vs.110).aspx)

If you can explain what you are trying to achieve we may be able to help

---

<div class="post-metadata">

**Author:** ![matthieu-buisson](https://avatars.discourse-cdn.com/v4/letter/m/a9a28c/32.png) [@matthieu-buisson](https://forums.powershell.org/u/matthieu-buisson)\
**Post date:** [July 11, 2014, 9:30pm UTC](https://forums.powershell.org/t/need-to-check-a-socket-but-with-a-source-ip/2814/4 "2014-07-11T21:30:28Z")

</div>

The “Client” property of the object type System.Net.Sockets.TcpClient contains useful information, as you can see below :

```
$socketC | Select-Object -ExpandProperty Client

Available : 0
LocalEndPoint : 10.0.0.9:49611
RemoteEndPoint : 10.0.0.1:80
Handle : 1760
Blocking : True
UseOnlyOverlappedIO : False
Connected : True
AddressFamily : InterNetwork
SocketType : Stream
ProtocolType : Tcp
IsBound : True
ExclusiveAddressUse : False
ReceiveBufferSize : 8192
SendBufferSize : 8192
ReceiveTimeout : 0
SendTimeout : 0
LingerState : System.Net.Sockets.LingerOption
NoDelay : False
Ttl : 128
DontFragment : True
MulticastLoopback :
EnableBroadcast :
DualMode :
```

So, if you just want the source and remote endpoints :

```
$socketC | Select-Object -ExpandProperty client | Select-Object -Property LocalEndPoint, RemoteEndPoint

LocalEndPoint RemoteEndPoint

10.0.0.9:49611 10.0.0.1:80
```

Or, if you want only the source and remote IP addresses, without the port numbers :

```
$socketC | Select-Object -Property @{N="LocalIP";E={$_.Client.LocalEndPoint.Address }}, @{N="RemoteIP";E={$_.Client.RemoteEndPoint.Address }}

LocalIP RemoteIP

10.0.0.9 10.0.0.1
```

---

<div class="post-metadata">

**Author:** ![windows-liveuser25](https://avatars.discourse-cdn.com/v4/letter/w/ac8455/32.png) [@windows-liveuser25](https://forums.powershell.org/u/windows-liveuser25)\
**Post date:** [July 11, 2014, 10:02pm UTC](https://forums.powershell.org/t/need-to-check-a-socket-but-with-a-source-ip/2814/5 "2014-07-11T22:02:52Z")

</div>

Team,

I am trying to check a Socket to machine C(IP & port) from machine B (IP) and my local machine is machine A.

---

<div class="post-metadata">

**Author:** ![richard-siddaway](https://avatars.discourse-cdn.com/v4/letter/r/d78d45/32.png) [@richard-siddaway](https://forums.powershell.org/u/richard-siddaway)\
**Post date:** [July 11, 2014, 10:42pm UTC](https://forums.powershell.org/t/need-to-check-a-socket-but-with-a-source-ip/2814/6 "2014-07-11T22:42:00Z")

</div>

Your code would have to run on machine B. I don’t think remoting will work for you - you’re going to hit the second hop problem. Why does machine C have to be checked from B? Why not run the code on A?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [July 11, 2014, 10:45pm UTC](https://forums.powershell.org/t/need-to-check-a-socket-but-with-a-source-ip/2814/7 "2014-07-11T22:45:02Z")

</div>

Second hop won’t be an issue here; there’s no authentication happening to “machine C”. As long as you have remoting enabled on “machine B”, you can just use Invoke-Command.

---

<div class="post-metadata">

**Author:** ![windows-liveuser25](https://avatars.discourse-cdn.com/v4/letter/w/ac8455/32.png) [@windows-liveuser25](https://forums.powershell.org/u/windows-liveuser25)\
**Post date:** [July 11, 2014, 11:13pm UTC](https://forums.powershell.org/t/need-to-check-a-socket-but-with-a-source-ip/2814/8 "2014-07-11T23:13:58Z")

</div>

Invoke-Command will be connected via remote System. Need something from locally.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [July 11, 2014, 11:40pm UTC](https://forums.powershell.org/t/need-to-check-a-socket-but-with-a-source-ip/2814/9 "2014-07-11T23:40:14Z")

</div>

That doesn’t make any sense. You’re saying you want to test a connection from both a remote machine, and “from locally.” What does that mean, exactly?

---

<div class="post-metadata">

**Author:** ![windows-liveuser25](https://avatars.discourse-cdn.com/v4/letter/w/ac8455/32.png) [@windows-liveuser25](https://forums.powershell.org/u/windows-liveuser25)\
**Post date:** [July 11, 2014, 11:54pm UTC](https://forums.powershell.org/t/need-to-check-a-socket-but-with-a-source-ip/2814/10 "2014-07-11T23:54:56Z")

</div>

Need to check a connection COMA(web server)------COMB(Client PC). Need to check from COMC without " Enter-PSSession" or “Invoke-Command”

---

<div class="post-metadata">

**Author:** ![richard-siddaway](https://avatars.discourse-cdn.com/v4/letter/r/d78d45/32.png) [@richard-siddaway](https://forums.powershell.org/u/richard-siddaway)\
**Post date:** [July 12, 2014, 12:25am UTC](https://forums.powershell.org/t/need-to-check-a-socket-but-with-a-source-ip/2814/11 "2014-07-12T00:25:48Z")

</div>

why?

---

<div class="post-metadata">

**Author:** ![richard-siddaway](https://avatars.discourse-cdn.com/v4/letter/r/d78d45/32.png) [@richard-siddaway](https://forums.powershell.org/u/richard-siddaway)\
**Post date:** [July 12, 2014, 12:29am UTC](https://forums.powershell.org/t/need-to-check-a-socket-but-with-a-source-ip/2814/12 "2014-07-12T00:29:28Z")

</div>

I’ve seen all sorts of problems trying to work against a third machine so I’m not 100% convinced that there isn’t any issue

---

<div class="post-metadata">

**Author:** ![windows-liveuser25](https://avatars.discourse-cdn.com/v4/letter/w/ac8455/32.png) [@windows-liveuser25](https://forums.powershell.org/u/windows-liveuser25)\
**Post date:** [July 12, 2014, 12:29am UTC](https://forums.powershell.org/t/need-to-check-a-socket-but-with-a-source-ip/2814/13 "2014-07-12T00:29:48Z")

</div>

Need to check for 6000 PC so want to aviod invoke-command 6000 time. Will try some foreach if we can define the source IP.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [July 12, 2014, 12:44am UTC](https://forums.powershell.org/t/need-to-check-a-socket-but-with-a-source-ip/2814/14 "2014-07-12T00:44:13Z")

</div>

> [Biswajit wrote:](https://powershell.org/forums/topic/need-to-check-a-socket-but-with-a-source-ip/#post-17051)Need to check for 6000 PC so want to aviod invoke-command 6000 time. Will try some foreach if we can define the source IP.

Well, good luck with that. 🙂 If you want to actually check the connection from 6000 source endpoints, then you need to make the code execute on each of them in some way. In PowerShell, the preferred way of doing that is Invoke-Command. You can try some other method, but it will basically amount to the same idea of executing code on the remote computers.

Test-Connection, for example, appears to let you ping a computer from a remote source via its -Source parameter, but all it’s doing is making a WMI call to that remote computer, and the WMI class then runs the ping. Different protocol, but in principle, the same idea as running Invoke-Command.

On a side note, you don’t have to run Invoke-Command 6000 times. You can run it once, and pass it 6000 computer names or PSSession objects. That will offer you better performance anyway, as Invoke-Command will do some number of the connections in parallel (32 at a time, by default.)

---

<div class="post-metadata">

**Author:** ![sam-boutros](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/sam-boutros/32/5_2.png) [@sam-boutros](https://forums.powershell.org/u/sam-boutros)\
**Post date:** [July 13, 2014, 11:21am UTC](https://forums.powershell.org/t/need-to-check-a-socket-but-with-a-source-ip/2814/15 "2014-07-13T11:21:57Z")

</div>

This should do the job:

```
# Running this script on Computer A
    $Account = "domain\user-with-eoungh-permissions-on-ComputerB"
    $ComputerB = "ComputerB_Name" # Source computer where we initiate the ip/port check
    $ComputerC = "10.224.34.1" # Target computer that we're checking ip/port on
    $Port = 636 # Target port
# $ComputerC = "google.com" 
# $Port = 80
    #
    if (!(Test-Path -Path ".\Cred.txt")) {
        Write-Output "Error: missing encrypted pwd file .\Cred.txt, enter the pwd to be encrypted and saved to .\Cred.txt for future script use:" 
        Read-Host 'Enter the pwd to be encrypted and saved to .\Cred.txt for future script use:' -AsSecureString | ConvertFrom-SecureString | Out-File .\Cred.txt
    }
    $Pwd = Get-Content .\Cred.txt | ConvertTo-SecureString 
    $Cred = New-Object System.Management.Automation.PSCredential($Account,$Pwd)
    $ComputerCdata = Invoke-Command -ComputerName $ComputerB -Credential $Cred -ScriptBlock { param($ComputerC,$Port) 
        $socketC = New-object Net.Sockets.TcpClient
        $socketC.Connect($ComputerC,$Port) 
        $data = $socketC.Connected
        return $data
    } -ArgumentList $ComputerC,$Port
    if ($ComputerCdata) {Write-Output "Computer $ComputerC socket $Port connected successfully from computer $ComputerB"}
        else {Write-Output "Failed to connect to Computer $ComputerC socket $Port from computer $ComputerB"}
```

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:47pm UTC](https://forums.powershell.org/t/need-to-check-a-socket-but-with-a-source-ip/2814/16 "2024-05-16T20:47:47Z")

</div>


