Need to check a Socket but with a source IP

This topic contains 14 replies, has 6 voices, and was last updated by  Sam Boutros 3 years, 4 months ago.

  • Author
    Posts
  • #17038

    Biswajit
    Participant

    $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?

  • #17039

    Daniel Krebs
    Moderator

    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

  • #17040

    Richard Siddaway
    Moderator

    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

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

  • #17041

    Mathieu Buisson
    Participant

    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
    • #17042

      Biswajit
      Participant

      Team,

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

  • #17043

    Richard Siddaway
    Moderator

    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?

  • #17044

    Dave Wyatt
    Moderator

    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.

  • #17045

    Biswajit
    Participant

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

  • #17046

    Dave Wyatt
    Moderator

    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?

  • #17047

    Biswajit
    Participant

    Need to check a connection COMA(web server)——COMB(Client PC). Need to check from COMC without " [b]Enter-PSSession[/b]" or "[b]Invoke-Command[/b]"

  • #17048

    Richard Siddaway
    Moderator

    why?

  • #17050

    Richard Siddaway
    Moderator

    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

  • #17051

    Biswajit
    Participant

    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.

  • #17052

    Dave Wyatt
    Moderator

    Biswajit wrote: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.)

  • #17092

    Sam Boutros
    Participant

    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"}
    

You must be logged in to reply to this topic.