It doesn't work in shell script, but in javascript, it works fine, what's wrong with that?
I just want to filter out all IPs that are not in the range of 2~254, for example, ignore the
255
shell script
I want to filter out the IP
192.168.18.255only using thestandard regular expressions.
- current useful input IPs: - 192.168.18.195 192.168.18.255
- wanted output IP: - 192.168.18.195
#!/usr/bin/env bash
IPs=$(ifconfig | grep -oE '(192\.168\.1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])\.(1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])')
echo $IPs
# 192.168.18.195 192.168.18.255 ❌
IPs1=$(ifconfig | grep -oE '192\.168\.(1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])\.([2-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-4])')
echo $IPs1
# 192.168.18.195 192.168.18.25 ❌
IPs2=$(ifconfig | grep -oE '192\.168\.(1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])\.([2-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-4])$')
echo $IPs2
# ❌
# wanted, ignore 192.168.18.255 ❓
# IPs=$(ifconfig | grep -oE '❓')
# echo $IPs
# 192.168.18.195
This minimal reproducible version input just for testing.
en1: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
        options=6463<RXCSUM,TXCSUM,TSO4,TSO6,CHANNEL_IO,PARTIAL_CSUM,ZEROINVERT_CSUM>
        ether a4:83:e7:91:62:79 
        inet6 fe80::1ca2:3b0a:df9d:465f%en1 prefixlen 64 secured scopeid 0x7 
        inet 192.168.18.195 netmask 0xffffff00 broadcast 192.168.18.255
        inet6 fd80:eae6:1258:0:37:7544:1d1:7b08 prefixlen 64 autoconf secured 
        nd6 options=201<PERFORMNUD,DAD>
        media: autoselect
        status: active
$ ifconfig
# The output is too long, ignore it here, Please see the link below
The full version output of ifconfig link: https://gist.github.com/xgqfrms/a9e98b17835ddbffab07dde84bd3caa5
javascript
This is just used for test ignore the IP
255works well.
function test(n) {
  let reg = /192\.168\.(1?[0-9][0-9]?|2[0-4][0-9]|25[0-5])\.([2-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-4])$/;
   for (let i = 0; i < n; i++) {
     let result = reg.test(`192.168.18.${i}`);
     if(result) {
       // console.log(`192.168.18.${i} ✅`, i, result)
     } else {
       console.log(`192.168.18.${i} ❌`, i, result)
     }
   }
}
test(256);
192.168.18.0 ❌ 0 false
192.168.18.1 ❌ 1 false
192.168.18.255 ❌ 255 false
      


 
     
    


