ปกติเวลาเราจะตรวจสอบว่า port อะไรเปิดบ้าง เรามักจะใช้คำสั่งที่ชื่อว่า telnet ทีนี้ถ้าเราไม่ได้ติดตั้ง telnet ไว้ในเครื่อง Linux ล่ะ เราจะใช้อะไรแทนได้บ้าง มาดูกันครับ
BASH
1 2 3 4 5 6 7 8 |
# 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 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
# 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 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
# 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 |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
# 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 |