AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 461760
Accepted
yael
yael
Asked: 2012-12-31 02:54:28 +0800 CST2012-12-31 02:54:28 +0800 CST 2012-12-31 02:54:28 +0800 CST

shell:创建使用 IP 地址作为参数的快捷方式命令(别名或函数)

  • 772

我不确定我是否执行了以下非法或真实的

但我需要的是 - 创建一组准备好的命令
,所以如果我需要使用,例如将 IP 地址与 4 个八位字节匹配,我可以使用命令 - command_that_match_four_octet

如果我正确执行了以下操作,请提出建议,我想知道以下语法是否会引起麻烦。

[root@su1a /tmp]#  command_that_match_four_octet=" grep '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}' "

[root@su1a /tmp]# command_that_match_three_three_octet=" grep '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}' "

.

[root@su1a /tmp]# echo 23.4.5.1  | eval $command_that_match_four_octet
   23.4.5.1

[root@su1a /tmp]# echo 23.4.5 | eval $command_that_match_three_three_octet
   23.4.5
linux
  • 2 2 个回答
  • 510 Views

2 个回答

  • Voted
  1. Best Answer
    F. Hauri - Give Up GitHub
    2012-12-31T12:16:23+08:002012-12-31T12:16:23+08:00

    你所说shortcut的似乎是一种alias或更复杂的functions。

    为了回答你的问题,你可以:

    alias checkCommand="grep '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}'"
    echo 23.4.5.1  | checkCommand
    23.4.5.1
    

    或者

    function checkIsIp() {
        grep '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}'
    }
    echo 23.4.5.1  | checkIsIp
    

    有一个 bash 函数将检查 IP (v4) 和更多这将计算 32 位整数,如果参数是一个有效的 IP 并返回一个RC > 0if else:

    function check_Is_Ip() {
        local IFS=.
        set -- $1
        [ $# -eq 4 ] || return 2
        local var
        for var in $* ;do
            [ $var -lt 0 ] || [ $var -gt 255 ] && return 3
          done
        echo $(( ($1<<24) + ($2<<16) + ($3<<8) + $4))
    }
    

    比现在:

    if check_Is_Ip 1.0.0.1 >/dev/null; then echo It is. ;else echo There is not. ;fi
    It is.
    
    if check_Is_Ip 1.255.0.1 >/dev/null; then echo It is. ;else echo There is not. ;fi
    It is.
    
    if check_Is_Ip 1.256.0.1 >/dev/null; then echo It is. ;else echo There is not. ;fi
    There is not.
    

    可用于 IP 计算:

    有后台功能:

    int2ip() {
        echo $(($1>>24)).$(($1>>16&255)).$(($1>>8&255)).$(($1&255))
    }
    
    check_Is_Ip 255.255.255.0
    4294967040
    check_Is_Ip 192.168.1.31
    3232235807
    
    int2ip $((4294967040 & 3232235807))
    192.168.1.0
    

    因此,作为一种良好做法,您可以:

    function die() {
        echo "Error: $@" >&2
        exit 1
    }
    
    netIp="192.168.1.31"
    netMask="255.255.255.0"
    
    intIp=$(check_Is_Ip $netIp) || die "Submited IP: '$netIP' is not an IPv4 address."
    intMask=$(check_Is_Ip $netMask) || die "Submited Mask: '$netMask' not IPv4."
    netBase=$(int2ip $(( intIp & intMask )) )
    netBcst=$(int2ip $(( intIp | intMask ^ ( (1<<32) - 1 ) )) )
    printf "%-20s: %s\n" \
        Address $netIp Netmask $netMask Network $netBase Broadcast $netBcst
    
    Address             : 192.168.1.31
    Netmask             : 255.255.255.0
    Network             : 192.168.1.0
    Broadcast           : 192.168.1.255
    

    完成输入的检查、验证和转换的地方只有一个操作:

    intMask=$(check_Is_Ip $netMask) || die "Submited Mask: '$netMask' not IPv4."
    

    如果$netMask不匹配 IPv4,命令check_Is_Ip将失败,然后die将被执行。如果不是,转换结果将存储在intMask变量中。

    • 1
  2. Khaled
    2012-12-31T03:59:06+08:002012-12-31T03:59:06+08:00

    如果你想创建另一个命令的快捷方式,你可以使用内置的 shell 命令alias。有关更多信息,您可以查看man bash。

    例如,您可以使用:

    $ alias your_command_name='your complicated and long command'
    
    • 0

相关问题

  • Linux 主机到主机迁移

  • 如何在 Linux 机器上找到有关硬件的详细信息?

  • 如何在 Linux 下监控每个进程的网络 I/O 使用情况?

  • 在 RHEL4 上修改 CUPS 中的现有打印机设置

  • 为本地网络中的名称解析添加自定义 dns 条目

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve