linux c编程 释放端口号,linux c 语言端口扫描

#include //printf函数要用的头文件

#include #include #include #include #include #include #include  #include  #include  #include  #include #include #include

void help();             //声明一个函数取名help,就是帮助函数

int main(int argc, char* argv[])      //主入口函数main()

{

//以下声明变量

//定义sockaddr_in结构

struct sockaddr_in sin;

int from_port;   //开始端口

int to_port;     //结束端口

int now_port;          //正在扫描的端口

char* host;          //要扫描的主机

int s;           //保存创建socket时的返回值 其实就是个整数…

int open_port = 0;         //开放端口个数

//如果命令行下参数不是4个(包括portscan.exe本身),提示正确的用法

if(argc != 4)

{

help(); //给出帮助

return -1;

}

//保存用户输入的要扫描的起始端口和结束端口

//由于用户输入的是char型,所以要先转成int型

from_port = atoi(argv[2]);

to_port = atoi(argv[3]);

host = argv[1];

//对用户输入的端口进行判断

if(from_port > to_port || from_port < 0 || from_port >65535 || to_port <0 || to_port >65535)

{

printf("起始端口不能大于结束端口,且范围为:1-65535!\n");

return 0;

}

printf("======= 开始扫描 =======\n");

//循环连结端口,以判断端口是否开放

for(now_port = from_port; now_port <= to_port; now_port++)

{

s = socket(AF_INET,SOCK_STREAM,0);

if(s < 0)

{

printf("创建socket()失败!\n");

}

//给结构成员赋值

sin.sin_family = AF_INET;

sin.sin_port = htons(now_port);

sin.sin_addr.s_addr = inet_addr(host);

//建立连结

if(connect(s,(struct sockaddr*)&sin,sizeof(sin)) < 0)

{

//printf("%s -> %d:未开放\n",host,now_port);

close(s);

}

else

{

printf("%s -> %d:开放\n",host,now_port);

open_port ++;

close(s);

}

}

printf("======= 扫描结果 =======\n");

printf("主机:%s 扫描到%d个端口开放\n",host,open_port);

//关闭socket

close(s);

return 0;

}

//以下为帮助函数内容

void help()

{

printf("Usage:\n");

printf("  portscan \n");

printf("Example:\n");

printf("  portscan 127.0.0.1 135 445\n");

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值