Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Friday, June 21, 2019 9:02 AM
I want to check whether telnet connection to a SMTP server on port 25 is working or not from a machine. Below is the script i've written to do it.
Now when i run the telnet command on a machine manually it is saying "421 4.3.2 Service not available.... Connection to host lost." so telnet connection is not working from that machine. that is fine.
but if i run the below script on the same machine where telnet failed with above said error message, it is saying "Success". Not sure what is wrong with my script, please help
$Socket = New-Object Net.Sockets.TcpClient
# Suppress error messages
$ErrorActionPreference = 'SilentlyContinue'
# Try to connect
$Socket.Connect("124.140.118.209", "25")
# Make error messages visible again
$ErrorActionPreference = 'Continue'
# Determine if we are connected.
if ($Socket.Connected) {
Write-Output "Success"
$Socket.Close()
}
else {
Write-Output "Failed"
}
# Apparently resetting the variable between iterations is necessary.
$Socket = $null
All replies (3)
Friday, June 21, 2019 12:13 PM ✅Answered | 1 vote
Posting to SMTP servers can be done with sockets. Using a socket to test that the smtp service is running requires reading the banner line. This is what we see when using a telnet client. Just opening the port (per Marco) doesn't tell us anything other than some process has the port "listening" at the other end. To validate SMTP we need to retrieve the banner.
The following can be used to validate an SMTP server. The default settings work from anywhere.
function Test-TCPPort{
Param(
$HostAddress = 'smtp.gmail.com',
$HostPort = 587
)
$socket = New-Object Net.Sockets.TcpClient
$blen = 80
$buffer = [byte[]]::New($blen)
Try{
$socket.Connect($HostAddress, $HostPort)
$strm = $socket.GetStream()
$l = $strm.Read($buffer,0, $blen)
[System.Text.Encoding]::ASCII.GetString($buffer)
}
Catch{
Write-Host $_
}
Finally{
$socket.Close()
$socket.Dispose()
}
}
Test-TCPPort
We can also add code to handshake with the server to validate that it is ready to accept mail. A simple test message can be easily sent.
\(ツ)_/
Friday, June 21, 2019 9:33 AM
$socket = New-Object Net.Sockets.TcpClient
Try{
$socket.Connect('124.140.118.209', 25)
Write-Host Success -fore green
}
Catch{
Write-Host $_
}
Finally{
$socket.Close()
$socket.Dispose()
}
None of this has anything to do with Telnet. Telnet is a protocol used to establish an interactive session. The code here just opens a TCP port.
\(ツ)_/
Friday, June 21, 2019 11:42 AM
The problem here is that you are *only* opening a TCP connection (as JRV says). Using telnet is different, for example, this might help (Google: "powershell telnet" or similar):
https://thesurlyadmin.com/2013/04/04/using-powershell-as-a-telnet-client/
The "421 4.3.2" is getting returned from the TCP application layer (from Exchange), but just opening a TCP connection, like you're doing, doesn't get you there.