ip地址扫描

自己写的一个ip地址扫描的shell脚本,功能是输入ip地址和掩码,通过ping检测整个网段的ip地址,输出ping的结果。

主要的几个函数如下:

1、ip地址转化为数值,方便计算

ip2num()
{
    ip=$1
    a=`echo $ip |awk -F '.' '{print $1}'`
    b=`echo $ip |awk -F '.' '{print $2}'`
    c=`echo $ip |awk -F '.' '{print $3}'`
    d=`echo $ip |awk -F '.' '{print $4}'`
    num=$(($a*256*256*256+$b*256*256+$c*256+$d))
    echo $num
    echo "obase=10;(($num))"bc
}

2、数值转化为ip

num2ip()
{
    num=$1
    a=$(($num/(256*256*256)))
    #echo $a
    b=$(($num/(256*256)-$a*256))
    #echo $b
    c=$(($num/256-$a*256*256-$b*256))
    #echo $c
    d=$(($num-$a*256*256*256-$b*256*256-$c*256))
    #echo $d
    echo $a.$b.$c.$d >>ip.dat
    iptmp=$a.$b.$c.$d
}

3、cdr掩码转化为ip格式

cdr2mask ()
{
   # Number of args to shift, 255..255, first non-255 byte, zeroes
   set -- $(( 5 - ($1 / 8) )) 255 255 255 255 $(( (255 << (8 - ($1 % 8))) & 255 )) 0 0 0
   [ $1 -gt 1 ] && shift $1 || shift
   #echo ${1-0}.${2-0}.${3-0}.${4-0}
   mask=${1-0}.${2-0}.${3-0}.${4-0}
}

4、ip格式掩码转化为cdr

mask2cdr ()
{
   # Assumes there's no "255." after a non-255 byte in the mask
   local x=${1##*255.}
   set -- 0^^^128^192^224^240^248^252^254^ $(( (${#1} - ${#x})*2 )) ${x%%.*}
   x=${1%%$3*}
   echo $(( $2 + (${#x}/4) ))
}

5、由ip地址和掩码计算网络地址

net ()
{
    
    ip=$1
    a=`echo $ip |awk -F '.' '{print $1}'`
    b=`echo $ip |awk -F '.' '{print $2}'`
    c=`echo $ip |awk -F '.' '{print $3}'`
    d=`echo $ip |awk -F '.' '{print $4}'`

    mask=$2
    m=`echo $mask |awk -F '.' '{print $1}'`
    n=`echo $mask |awk -F '.' '{print $2}'`
    o=`echo $mask |awk -F '.' '{print $3}'`
    p=`echo $mask |awk -F '.' '{print $4}'`
    
    n1=$(($a&$m))
    n2=$(($b&$n))
    n3=$(($c&$o))
    n4=$(($d&$p))
    
    net=$((n1*256*256*256+n2*256*256+n3*256+n4))
#    echo $net
}

3、4两个函数直接用的网上大神的,写的很简洁,对比自己写的,感觉自己low爆了,哈哈

 

转载于:https://www.cnblogs.com/wangle0529/p/8981866.html

以下是使用C++实现IP地址扫描的示例代码: ```c++ #include <iostream> #include <string> #include <vector> #include <thread> #include <mutex> #include <chrono> #include <atomic> #include <cstdlib> #include <cstdio> #include <cstring> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <unistd.h> using namespace std; mutex mtx; atomic<int> cnt(0); void scan_ip(const string& ip, int port) { int sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) { perror("socket"); return; } struct sockaddr_in addr; memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_port = htons(port); addr.sin_addr.s_addr = inet_addr(ip.c_str()); if (connect(sockfd, (struct sockaddr*)&addr, sizeof(addr)) == 0) { mtx.lock(); cout << ip << ":" << port << " is open" << endl; mtx.unlock(); } close(sockfd); cnt++; } void scan_ips(const string& prefix, int start, int end, int port) { for (int i = start; i <= end; i++) { string ip = prefix + to_string(i); thread t(scan_ip, ip, port); t.detach(); } } int main(int argc, char* argv[]) { if (argc != 4) { cout << "Usage: " << argv[0] << " <prefix> <start> <end>" << endl; return 0; } string prefix = argv[1]; int start = atoi(argv[2]); int end = atoi(argv[3]); int port = 80; auto start_time = chrono::steady_clock::now(); scan_ips(prefix, start, end, port); while (cnt < (end - start + 1)) { this_thread::sleep_for(chrono::milliseconds(100)); } auto end_time = chrono::steady_clock::now(); auto elapsed_time = chrono::duration_cast<chrono::milliseconds>(end_time - start_time).count(); cout << "Scan completed in " << elapsed_time << "ms" << endl; return 0; } ``` 该示例代码使用了C++的标准库和POSIX socket API,实现了一个简单的IP地址扫描器。用户可以通过命令行参数指定要扫描IP地址范围和端口号,程序将会并发地扫描指定范围内的所有IP地址和指定端口,输出开放的端口号。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值