用4种语言编写端口扫描器(JavaCPythonGo)
Posted @carefree
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用4种语言编写端口扫描器(JavaCPythonGo)相关的知识,希望对你有一定的参考价值。
Java
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class PortScanner
private String ip;
private int startPort;
private int endPort;
private ExecutorService executorService;
public PortScanner(String ip, int startPort, int endPort)
this.ip = ip;
this.startPort = startPort;
this.endPort = endPort;
this.executorService = Executors.newCachedThreadPool();
public void startScan()
for (int port = startPort; port <= endPort; port++)
Runnable runnable = new Runnable()
@Override
public void run()
try
Socket socket = new Socket();
socket.connect(new InetSocketAddress(ip, port), 1000);
System.out.println("端口 " + port + " 已打开");
socket.close();
catch (Exception e)
System.out.println("端口 " + port + " 未打开");
;
executorService.execute(runnable);
public static void main(String[] args)
PortScanner portScanner = new PortScanner("192.168.1.1", 1, 65535);
portScanner.startScan();
C
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#define PORT_SRC 8000
#define PORT_DEST 8001
int main()
int sock_src, sock_dest, new_sock;
int optval = 1;
char buffer[1024] = 0;
struct sockaddr_in src_addr, dest_addr;
int addr_len = sizeof(src_addr);
// Create source socket
if ((sock_src = socket(AF_INET, SOCK_STREAM, 0)) == 0)
perror("socket failed");
exit(EXIT_FAILURE);
src_addr.sin_family = AF_INET;
src_addr.sin_addr.s_addr = INADDR_ANY;
src_addr.sin_port = htons(PORT_SRC);
setsockopt(sock_src, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &optval, sizeof(optval));
if (bind(sock_src, (struct sockaddr *)&src_addr, sizeof(src_addr))<0)
perror("bind failed");
exit(EXIT_FAILURE);
if (listen(sock_src, 3) < 0)
perror("listen");
exit(EXIT_FAILURE);
// Create destination socket
if ((sock_dest = socket(AF_INET, SOCK_STREAM, 0)) == 0)
perror("socket failed");
exit(EXIT_FAILURE);
dest_addr.sin_family = AF_INET;
dest_addr.sin_addr.s_addr = INADDR_ANY;
dest_addr.sin_port = htons(PORT_DEST);
setsockopt(sock_dest, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &optval, sizeof(optval));
if (bind(sock_dest, (struct sockaddr *)&dest_addr, sizeof(dest_addr))<0)
perror("bind failed");
exit(EXIT_FAILURE);
if (listen(sock_dest, 3) < 0)
perror("listen");
exit(EXIT_FAILURE);
// Accept connection from source
if ((new_sock = accept(sock_src, (struct sockaddr *)&src_addr,
(socklen_t*)&addr_len))<0)
perror("accept");
exit(EXIT_FAILURE);
while (1)
// Receive data from source
int read_size = read(new_sock, buffer, 1024);
if (read_size == 0)
printf("Connection closed\\n");
close(new_sock);
break;
else
// Send data to destination
send(sock_dest, buffer, read_size, 0);
memset(buffer, 0, 1024);
return 0;
python
import socket
import threading
import time
#定义端口扫描函数
def port_scan(host, port):
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))
print('[+] ', port, ' open')
s.close()
except:
pass
#定义主函数
def main():
host = '127.0.0.1'
#定义端口范围
port_range = range(1, 65535)
#多线程
for port in port_range:
t = threading.Thread(target=port_scan, args=(host, port))
t.start()
time.sleep(0.1)
if __name__ == '__main__':
main()
go
package main
import (
"fmt"
"net"
)
func main()
// 要扫描的IP
ip := "192.168.1.1"
// 要扫描的端口范围
for port := 1; port <= 65535; port++
// 根据IP和端口拼接出一个地址
address := fmt.Sprintf("%s:%d", ip, port)
// 拨号
conn, err := net.Dial("tcp", address)
// 如果拨号成功,说明端口是开放的
if err == nil
fmt.Printf("%d open\\n", port)
// 关闭连接
conn.Close()
以上是关于用4种语言编写端口扫描器(JavaCPythonGo)的主要内容,如果未能解决你的问题,请参考以下文章