简体   繁体   English

IP地址计算器脚本

[英]IP address calculator script

I need to write a Bash script that converts IP address from CIDR format to quad-style. 我需要编写一个Bash脚本,将IP地址从CIDR格式转换为四元样式。

I must enter the IP address in the following style: 我必须以以下方式输入IP地址:

10.10.10.10/24

If I entered it in this style: 如果我以这种方式输入它:

10.10.10.10 255.255.255.0  

an error message should appear. 将会出现一条错误消息。

I tried this script: 我尝试了以下脚本:

#!/bin/bash
echo "enter you ip"
read ip
case $ip in
*.*.*.*/*)
b=`echo $ip | cut -d/ -f1`
a=`echo $ip | cut -d/ -f2`

if [ $a -eq 24 ];then
   echo "$b 255.255.255.0"
elif [ $a -eq 25 ];then
   echo "$b 255.255.255.128"
elif [ $a -eq 26 ];then
   echo "$b 255.255.255.192"
elif [ $a -eq 27 ];then
   echo "$b 255.255.255.224"
elif [ $a -eq 28 ];then
   echo "$b 255.255.255.240"
elif [ $a -eq 29 ];then
   echo "$b 255.255.255.248"
elif [ $a -eq 30 ];then
   echo "$b 255.255.255.252"
elif [ $a -eq 31 ];then
   echo "$b 255.255.255.254"
elif [ $a -eq 32 ];then
   echo "$b 255.255.255.255"
fi


case $ip in
 *.*.*.* *.*.*.*)
echo "enter a valid address"

esac

but I get an error 但我得到一个错误

./ipcalculater2.sh: line 32: syntax error near unexpected token ` . ./ipcalculater2.sh:第32行:意外标记`附近的语法错误 . . ' '

./ipcalculater2.sh: line 32: ` . ./ipcalculater2.sh:第32行:` . . . . . )' )'

What is wrong with my script? 我的脚本有什么问题?

Here's an example of four ways one might convert from CIDR to netmask notation in bash. 这是bash中从CIDR转换为网络掩码表示法的四种方式的示例。

#!/usr/bin/env bash

if [[ "$1" != *.*.*.*/* ]]; then
  echo "Usage: ${0##*/} ip.ad.dr.ess/bits" >&2
  exit 1
fi

# separate our input into network and mask using IFS
IFS=/ read network bits <<<"$1"

# build a temporary variable $s that we'll use to build $bitmask
# for the first three variants...
read zeros <<< $(printf '%032s' 0)
s=${zeros//0/1}${zeros}

# convert the mask into a 32-digit binary number
bitmask=${s:$((32-bits)):32}

# Four different methods for generating the netmask...

# The first two use `bc` and `dc`. One is likely installed on your system.
read mask1 <<<$( dc -e "2i $(fold -w8 <<<"$bitmask " | paste -sdp -)" | paste -sd. - )
read mask2 <<<$( fold -w8 <<<"$bitmask" | paste - | bc -e 'ibase=2' | paste -sd. - )

# And if dc and bc are unavailable, or you prefer not to spawn subshells, or
# risk missed dependencies, you can do this in pure bash with a few more lines.
unset mask3
for ((i=0;i<4;i++)); do
  mask3+="${mask3:+.}$((2#${bitmask:$((8*i)):8}))"
done

# And finally, instead of a loop, you can do the same thing with fancy math:
# This variant avoides the need for $bitmask, set above.
mask4="$(( 2**32 - (1 << (32-$bits)) ))"
mask4=$(( mask4/2**24 )).$(( mask4/2**16 %256 )).$(( mask4/2**8 % 256 )).$(( mask4 % 256 ))

# Print the results, obviously.
echo "network=$network"
echo "netlength=$bits"
echo "netmask via 'dc': $mask1"
echo "netmask via 'bc': $mask2"
echo "netmask via loop: $mask3"
echo "netmask via math: $mask4"

I've included code that works in each of dc and bc , since I can't predict which calculator will be available on your system. 我已经包含了适用于dcbc ,因为我无法预测哪个计算器将在您的系统上可用。 These calculators are used for base conversion. 这些计算器用于基本转换。 If you don't mind your script being a little longer, you can avoid spawning external tools (like fold and paste and the calculator) using the method that generates $netmask3 . 如果您不介意脚本$netmask3 ,可以使用生成$netmask3的方法避免产生外部工具(如foldpaste以及计算器)。

Note that in the third case, this usage of bc depends on availability of a -e option which exists in *BSD. 请注意,在第三种情况下, bc用法取决于* BSD中存在的-e选项的可用性。 Use another variant if you're using GNU bc (ie you're in Linux.) 如果您使用的是GNU bc,则使用另一个变体(例如,您在Linux中)。

You can of course adjust the output so that it meets your needs, as well as trim the parts of the script you're not using. 当然,您可以调整输出,使其满足您的需求,并修剪不使用的脚本部分。

Please check for this corrections: 请检查以下更正:

#!/bin/bash
echo "enter you ip"
read ip
case $ip in
*.*.*.*/*)
b=`echo $ip | cut -d/ -f1`
a=`echo $ip | cut -d/ -f2`

if [ $a -eq 24 ];then
   echo "$b 255.255.255.0"
elif [ $a -eq 25 ];then
   echo "$b 255.255.255.128"
elif [ $a -eq 26 ];then
   echo "$b 255.255.255.192"
elif [ $a -eq 27 ];then
   echo "$b 255.255.255.224"
elif [ $a -eq 28 ];then
   echo "$b 255.255.255.240"
elif [ $a -eq 29 ];then
   echo "$b 255.255.255.248"
elif [ $a -eq 30 ];then
   echo "$b 255.255.255.252"
elif [ $a -eq 31 ];then
   echo "$b 255.255.255.254"
elif [ $a -eq 32 ];then
   echo "$b 255.255.255.255"
fi
;;

 *.*.*.*\ *.*.*.*)
echo "enter a valid address"
;;

esac

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM