nc -zv -w 5 127.0.0.1 22
range:
nc -zv 127.0.0.1 22-23
$ curl -v telnet://127.0.0.1:22
$ curl -v smtp://127.0.0.1:25
if [[ $(wget -q -t 1 --spider --dns-timeout 3 --connect-timeout 10 127.0.0.1:22; echo $?) -eq 0 ]]; then echo "OK"; else echo "FAIL"; fi
$ cat < /dev/tcp/127.0.0.1/22
$ cat < /dev/tcp/127.0.0.1/80
$ cat < /dev/udp/host/53
</dev/tcp/localhost/11211 && echo Port is open || echo Port is closed
with timeout:
(timeout 1 bash -c '</dev/tcp/127.0.0.1/17500 && echo PORT OPEN || echo PORT CLOSED') 2>/dev/null
import socket
clientsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
clientsocket.connect(('127.0.0.1', 80))
clientsocket.send('GET\n')
1
clientsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
clientsocket.connect(('127.0.0.1', 80))
# perl
use IO::Socket::INET;
$| = 1;
my $socket = new IO::Socket::INET(
PeerHost => '127.0.0.1',
PeerPort => '22',
Proto => 'tcp',
);
die "can't connect to port 22\n" unless $socket;
print "connected to port 22";
oneliner:
perl -MIO::Socket::INET -e 'exit(! defined( IO::Socket::INET->new("127.0.0.1:22")))'
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
int main(int argc, char *argv[])
{
int portno = 4244;
char *hostname = "localhost";
int sockfd;
struct sockaddr_in serv_addr;
struct hostent *server;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
printf("Cant open socket");
exit(0);
}
server = gethostbyname(hostname);
if (server == NULL) {
fprintf(stderr,"no such host\n");
exit(0);
}
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr,
(char *)&serv_addr.sin_addr.s_addr,
server->h_length);
serv_addr.sin_port = htons(portno);
if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0) {
printf("Port is closed");
} else {
printf("Port is active");
}
close(sockfd);
return 0;
}