Linux에서 포트가 열려 있는지 효율적으로 테스트합니까?
bash 스크립트에서 포트 445
가 서버에서 열려 있는지 / 듣지 않는지 어떻게 빨리 알 수 있습니까 ?
몇 가지 옵션을 시도했지만 빠른 것을 원합니다.
1. lsof -i :445
(초 복용)
2. netstat -an |grep 445 |grep LISTEN
(초 복용)
3. telnet
(돌아 오지 않음)
4. nmap
, netcat
서버에서 사용할 수 없습니다
먼저 열거하지 않고 그 후에 greps하는 방법을 아는 것이 좋습니다.
내가 최근에 알게 된 놀람은 Bash가 파일 기술자로서 tcp 연결을 기본적으로 지원한다는 것이다. 쓰다:
exec 6<>/dev/tcp/ip.addr.of.server/445
echo -e "GET / HTTP/1.0\n" >&6
cat <&6
0,1,2는 stdin, stdout 및 stderr이므로 파일 설명 자로 6을 사용하고 있습니다. Bash는 자식 프로세스 에 5를 사용하기도 하므로 3,4,6,7,8 및 9는 안전해야합니다.
아래 주석 에 따라 스크립트에서 로컬 서버 에서 수신 대기하는지 테스트하십시오 .
exec 6<>/dev/tcp/127.0.0.1/445 || echo "No one is listening!"
exec 6>&- # close output connection
exec 6<&- # close input connection
누군가가 듣고 있는지 확인하려면 루프백으로 연결을 시도하십시오. 실패하면 포트가 닫히거나 액세스가 허용되지 않습니다. 그런 다음 연결을 닫으십시오.
이메일 전송, 실패시 스크립트 종료 또는 필요한 서비스 시작과 같은 사용 사례에 맞게이를 수정하십시오.
여기에 "빠른 답변"이 있습니다. 셸 스크립트에서 원격 TCP 포트가 열려 있는지 테스트하는 방법은 무엇입니까?
$ nc -z <host> <port>; echo $?
"원격"주소로 127.0.0.1과 함께 사용합니다.
훨씬 빠른 결과를 위해 netstat를이 방법으로 사용할 수 있습니다.
Linux에서 :
netstat -lnt | awk '$6 == "LISTEN" && $4 ~ /\.445$/'
Mac에서 :
netstat -anp tcp | awk '$6 == "LISTEN" && $4 ~ /\.445$/'
포트에서 수신 대기중인 프로세스 목록 (이 예에서는 445)을 출력하거나 포트가 비어 있으면 아무 것도 출력하지 않습니다.
이를 위해 netcat을 사용할 수 있습니다.
nc ip port < /dev/null
서버에 연결하고 직접 연결을 닫습니다. netcat이 연결할 수 없으면 0이 아닌 종료 코드를 리턴합니다. 종료 코드는 변수 $?에 저장됩니다. 예로서,
nc ip port < /dev/null; echo $?
netcat이 포트에 성공적으로 연결할 수있는 경우에만 0을 반환합니다.
그것들은 / proc / net / tcp에 나열되어 있습니다.
":"다음의 두 번째 열입니다 (16 진수).
> cat /proc/net/tcp
sl local_address rem_address st tx_queue rx_queue tr tm->when retrnsmt uid timeout inode
0: 00000000:0016 00000000:0000 0A 00000000:00000000 00:00000000 00000000 0 0 10863 1 ffff88020c785400 99 0 0 10 -1
1: 0100007F:0277 00000000:0000 0A 00000000:00000000 00:00000000 00000000 0 0 7983 1 ffff88020eb7b3c0 99 0 0 10 -1
2: 0500010A:948F 0900010A:2328 01 00000000:00000000 02:00000576 00000000 1000 0 10562454 2 ffff88010040f7c0 22 3 30 5 3
3: 0500010A:E077 5F2F7D4A:0050 01 00000000:00000000 02:00000176 00000000 1000 0 10701021 2 ffff880100474080 41 3 22 10 -1
4: 0500010A:8773 16EC97D1:0050 01 00000000:00000000 02:00000BDC 00000000 1000 0 10700849 2 ffff880104335440 57 3 18 10 -1
5: 0500010A:8772 16EC97D1:0050 01 00000000:00000000 02:00000BF5 00000000 1000 0 10698952 2 ffff88010040e440 46 3 0 10 -1
6: 0500010A:DD2C 0900010A:0016 01 00000000:00000000 02:0006E764 00000000 1000 0 9562907 2 ffff880104334740 22 3 30 5 4
7: 0500010A:AAA4 6A717D4A:0050 08 00000000:00000001 02:00000929 00000000 1000 0 10696677 2 ffff880106cc77c0 45 3 0 10 -1
그래서 :50
세 번째 열에 있는 것 중 하나는 stackoverflow이어야합니다. :)
man 5 proc
자세한 내용을 살펴보십시오 . sed 등으로 따로 따로 따로 따로 따로 따로 따로 읽는 것은 온화한 독자를위한 운동으로 남습니다 ...
bash를 사용하여 Spencer Rathbun의 답변을 기반으로합니다.
true &>/dev/null </dev/tcp/127.0.0.1/$PORT && echo open || echo closed
ss -tl4 '( sport = :22 )'
2ms는 충분히 빠르다?
Add the colon and this works on Linux
Here's one that works for both Mac and Linux:
netstat -aln | awk '$6 == "LISTEN" && $4 ~ "[\\.\:]445$"'
nc -l 8000
Where 8000 is the port number. If the port is free, it will start a server that you can close easily. If it isn't it will throw an error:
nc: Address already in use
I wanted to check if a port is open on one of our linux test servers. I was able to do that by trying to connect with telnet from my dev machine to the test server. On you dev machine try to run:
$ telnet test2.host.com 8080
Trying 05.066.137.184...
Connected to test2.host.com
In this example I want to check if port 8080 is open on host test2.host.com
tcping is a great tool with a very low overhead.It also has a timeout argument to make it quicker:
[root@centos_f831dfb3 ~]# tcping 10.86.151.175 22 -t 1
10.86.151.175 port 22 open.
[root@centos_f831dfb3 ~]# tcping 10.86.150.194 22 -t 1
10.86.150.194 port 22 user timeout.
[root@centos_f831dfb3 ~]# tcping 1.1.1.1 22 -t 1
1.1.1.1 port 22 closed.
You can use netcat command as well
[location of netcat]/netcat -zv [ip] [port]
or
nc -zv [ip] [port]
-z – sets nc to simply scan for listening daemons, without actually sending any data to them.
-v – enables verbose mode.
nmap
is the right tool. Simply use nmap example.com -p 80
You can use it from local or remote server. It also helps you identify if a firewall is blocking the access.
If you're using iptables try:
iptables -nL
or
iptables -nL | grep 445
참고URL : https://stackoverflow.com/questions/9609130/efficiently-test-if-a-port-is-open-on-linux
'Programing' 카테고리의 다른 글
_csv. 오류 : 필드 제한보다 큰 필드 (131072) (0) | 2020.05.14 |
---|---|
Angular를 사용하여 데이터 속성을 작성하는 방법 (0) | 2020.05.14 |
Sublime Text 2의 각 선택 항목에 선택 항목 당 한 번씩 숫자를 추가하십시오. (0) | 2020.05.14 |
Xcode 9에서 열린 여러 시뮬레이터에서 단일 시뮬레이터를 종료하거나 닫는 방법은 무엇입니까? (0) | 2020.05.14 |
XML에서 "&"를 이스케이프 처리하는 방법은 무엇입니까? (0) | 2020.05.14 |