how can write powershell script to telnet multiple ip addresses

Viewed 36

this is my script to telnet multiple IP addresses

$server_list = @('1.1.1.1:443', '10.100.8.22:3389', '10.100.8.21:22')
Foreach ($t in $server_list)
{
  $source = $t.Split(':')[0]
  $port = $t.Split(':')[1]
  Write-Host "Connecting to $source on port $port" | Out-File 'output.txt' -Append
  try
  {
    $socket = New-Object System.Net.Sockets.TcpClient($source, $port)
  }
  catch [Exception]
  {
    Write-Host $_.Exception.GetType().FullName | Out-File 'output.txt' -Append
    Write-Host $_.Exception.Message | Out-File 'output.txt' -Append
  }
  Write-Host "Connected`n" | Out-File 'output.txt' -Append
}

i want this output to be greean line when status is connected???

1 Answers

As per my comment. This line is invalid.

Write-Host "Connecting to $source on port $port" | Out-File 'output.txt'

It will simply error on any PS version. Now, on a file, it would just be empty. So, that is the reason I am showing a string as a reference error. If you'd use Write-Output, then the file would get populated, but then you'd need another Write-Host to show the screen text in color or use ANSI code.

$PSVersionTable

Name                           Value
----                           -----
PSVersion                      5.1.19041.1682
...

Write-Host 'Left side of the pipeline' | Out-String 'Right side of the pipeline'
Out-String : A positional parameter cannot be found that accepts argument 'Right side of the pipeline'.
...

$PSVersionTable

Name                           Value
----                           -----
PSVersion                      7.2.6
...

 Write-Host 'Left side of the pipeline' | Out-String 'Right side of the pipeline'
Out-String: A positional parameter cannot be found that accepts argument 'Right side of the pipeline'.

BTW: There already exist many PowerShell port scanner scripts all over the web to leverage as is or learn from.

However, here is a quick refactor of your posted code.

$Error.Clear()
Clear-Host
'1.1.1.1:443', '10.100.8.22:3389', '10.100.8.21:22' | 
ForEach-Object {
    If ((Test-NetConnection -ComputerName ($PSitem -split(':'))[0] -Port ($PSitem -split(':'))[1]).TcpTestSucceeded -eq 'True')
    {
        Write-Host "Connection successful to $(($PSItem -split ':')[0]) on port $(($PSItem -split ':')[1])" -ForegroundColor Green
        "Connection successful to $PSItem" | 
        Out-File -FilePath 'D:\temp\PortScanOutput.txt' -Append
    }
    Else 
    {
        Write-Warning "Connection error to $PSitem"

        "Connection failed to $PSItem", 
        ((($Error)[0]).GetType()).FullName, 
        ((($Error)[0]).Exception.Message) | 
        ForEach-Object {
            $PSitem  | 
            Out-File -FilePath 'D:\temp\PortScanOutput.txt' -Append
        }
    }
}
# Results
<#
Connection successful to 1.1.1.1 on port 443
WARNING: TCP connect to (10.100.8.22 : 3389) failed
WARNING: Ping to 10.100.8.22 failed with status: TimedOut
WARNING: Connection error to 10.100.8.22:3389
WARNING: TCP connect to (10.100.8.21 : 22) failed
WARNING: Ping to 10.100.8.21 failed with status: TimedOut
WARNING: Connection error to 10.100.8.21:22
#>

Live demo: enter image description here

Related