简体   繁体   English

ip-address操作

[英]Operations with ip-address

I have some ip-addresses that a user gives: 我有一些用户提供的ip地址:

192.168.50.$i

now I want to do +200 with $i . 现在我想用$i+200 So if the user does: 因此,如果用户这样做:

bash script 52 20 56

The result must be 结果必须是

ping 192.168.50.252 , ...

I thought to do something like: 我想做类似的事情:

function
{
  $i = $i + 200
  ping 192.168.50.$i
}

And what can I do when it's higher then 255? 大于255怎么办?

You can do something like this... 你可以做这样的事情...

echo "192.168.50.$(( $i + 200 ))"

If you wanted to check if it is greater than 255 you will have to break it down a bit. 如果要检查它是否大于255,则必须将其分解。

fourth=$(( $i + 200 ))
if (( fourth > 255 ))
then
    echo "Greater than 255!"
    exit 1
fi

Question change Didn't see last part 问题变更 没有看到最后一部分

You could create a function but this wont work as the syntax is way out. 您可以创建一个函数,但是由于语法不可行,因此无法使用。

function
{
    $i = $i + 200
    ping 192.168.50.$i
}

You will need to do something like this... 您将需要执行以下操作...

# Function name
function ip_assess {

    # $1 takes the first input to the function
    # There is no $ when assigning to a variable
    # There is no spaces around the = when assigning
    i=$(( $1 + 200 ))

    # No need for $ when doing arithmetic comparisons
    if (( i > 255 ))
    then
        # Return an error code of 1 from this function
        return 1
    fi

    # -c 4 will get it to ping four times and return and not continuously
    # If the ping is a success return 0 (a pass). If no then 2 (different error code)
    ping "192.168.50.$i" -c 4 && return 0 || return 2

}

ip_assess 30
# Grab the error code
valid_ip=$?

case $valid_ip in
    0 )
        echo "Valid IP"
    ;;
    1 )
        echo "IP is to high"
    ;;
    2 )
        echo "IP not alive"
    ;;
esac

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

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