29

What do people use when Telnet is not installed to check a port is open and reachable? E.g. I used to use the technique of telnet <destination> and know it was there, even if telnet could not interact with the system on the other end.

With Windows 2008 telnet is not installed so I've been a bit lost. So what can I use instead. And something if its not there in Linux or Solaris, too please.

I am a consultant who works on different sites. For a number of reasons (access rights, change control times, if I install it someone uses it next year we have some liability, etc) I cannot install on someone else's server. But a USB or other self contained, non-installed tool would be wonderful ...

10
  • 3
    Why don't you just install it? Dec 9, 2013 at 8:54
  • 2
  • 2
    Its a shame this was closed as Off Topic - I have just used one of the answers provided. Luckily the solution was not a product or service that I could not get past the firewall. I did learn something though so maybe it could be considered learning material. But if so no question could ever be posted - someone learns something from each of them.
    – Karl
    Aug 5, 2014 at 23:37
  • 1
    See: check status of one port on remote host at SO
    – kenorb
    Feb 11, 2016 at 11:34
  • 1
    How is this possibly off topic?
    – Timmmm
    Apr 15, 2021 at 15:47

2 Answers 2

41

Use Powershell like a boss


Basic code

$ipaddress = "4.2.2.1"
$port = 53
$connection = New-Object System.Net.Sockets.TcpClient($ipaddress, $port)

if ($connection.Connected) {
    Write-Host "Success"
}
else {
    Write-Host "Failed"
}

One Liner

PS C:\> test-netconnection -ComputerName 4.2.2.1 -Port 53

Turn it into a cmdlet

[CmdletBinding()]
Param(
  [Parameter(Mandatory=$True,Position=1)]
   [string]$ip,
    
   [Parameter(Mandatory=$True,Position=2)]
   [int]$port
)

$connection = New-Object System.Net.Sockets.TcpClient($ip, $port)
if ($connection.Connected) {
    Return "Connection Success"
}
else {
    Return "Connection Failed"
}

Save as a script and use all the time

Then you use the command in your powershell or cmd prompt like so:

PS C:\> telnet.ps1 -ip 8.8.8.8 -port 53

or

PS C:\> telnet.ps1 8.8.8.8 53

4
  • 7
    You can also just execute: New-Object System.Net.Sockets.TcpClient("IP or DomainName", 80) And you'll get an error if it can't connect, or information on the created object if the connection succeeded.
    – CB-Dan
    Dec 13, 2013 at 22:14
  • 1
    How does that work in terms of closing the socket? For example I know if I open a webrequest to IIS, but don't close the connection, it will reach a limit and I won't be able to test anymore. Dec 15, 2013 at 22:10
  • Good question. I imagine it does the same thing as in your answer (since you/we don't dispose of it or close the socket manually); The TcpClient should get garbage collected at some point, or the socket will hit a read timeout before that. Just a guess, haven't tested it!
    – CB-Dan
    Dec 16, 2013 at 1:32
  • 1
    If you don't want to worry about leaving the the socket open for a while you can do this: (New-Object System.Net.Sockets.TcpClient($ip, $port)).Close Same deal as before; Error if you can't connect, but it'll print this out if it connected then closed the port successfully: void Close()
    – CB-Dan
    Dec 16, 2013 at 1:36
53

Here are several different ways to test a TCP port without telnet.

BASH (man page)

# cat < /dev/tcp/127.0.0.1/22
SSH-2.0-OpenSSH_5.3
^C

# cat < /dev/tcp/127.0.0.1/23
bash: connect: Connection refused
bash: /dev/tcp/127.0.0.1/23: Connection refused


cURL

# curl -v telnet://127.0.0.1:22
* About to connect() to 127.0.0.1 port 22 (#0)
*   Trying 127.0.0.1... connected
* Connected to 127.0.0.1 (127.0.0.1) port 22 (#0)
SSH-2.0-OpenSSH_5.3
^C

# curl -v telnet://127.0.0.1:23
* About to connect() to 127.0.0.1 port 23 (#0)
*   Trying 127.0.0.1... Connection refused
* couldn't connect to host
* Closing connection #0
curl: (7) couldn't connect to host


Python

# python
Python 2.6.6 (r266:84292, Oct 12 2012, 14:23:48)
[GCC 4.4.6 20120305 (Red Hat 4.4.6-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import socket
>>> clientsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>>> clientsocket.connect(('127.0.0.1', 22))
>>> clientsocket.send('\n')
1
>>> clientsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>>> clientsocket.connect(('127.0.0.1', 23))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1, in connect
socket.error: [Errno 111] Connection refused


Perl

# perl
use IO::Socket::INET;
$| = 1;
my $socket = new IO::Socket::INET(
  PeerHost => '127.0.0.1',
  PeerPort => '22',
  Proto => 'tcp',
);
die "cannot connect to the server $!\n" unless $socket;
print "connected to the server\n";
^D
connected to the server
2
  • 1
    +1 Was not aware of the curl method, very cool! :)
    – blong
    Nov 3, 2016 at 18:10
  • this helped me notice that I have curl in vanilla windows 10 so thank you
    – nurettin
    Oct 26, 2021 at 8:09

Not the answer you're looking for? Browse other questions tagged .