1. ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:"​

命令解释如下:


  1. grep 'inet'             截取包含ip的行
  2. grep -v '127.0.0.1'     去掉本地指向的那行
  3. grep -v inet6           去掉包含inet6的行
  4. awk '{ print $2}'       $2 表示默认以空格分割的第二组 同理 $1表示第一组​
  5. tr -d "addr:            删除"addr:"这个字符串

输出结果:


  1. [root@master]# ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:"
  2. 192.168.168.200

在另外一台机器上的输出结果是:


  1. [root@master]# ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "add:"
  2. 10.147.197.32
  3. 192.168.122.1

192.*.*.*    和  10.*.*.* 这两个网段是不同的,现在要实现在不同网段的IP地址打印不同的输出,shell脚本如下:


  1. #!/bin/sh
  2. ip=`ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:"​`
  3. echo $ip
  4. if[[ $ip =="10."*]]
  5. then
  6. echo "该网段是10.*.*.*网段"
  7. else
  8. echo "该网段是192.*.*.*网段"
  9. fi

Linux Shell脚本中获取本机ip地址方法的更多相关文章

  1. shell脚本中获取本机ip地址的方法

    ipaddr='172.0.0.1' ipaddr=$(ip addr | awk '/^[0-9]+: / {}; /inet.*global/ {print gensub(/(.*)\/(.*)/ ...

  2. shell中获取本机ip地址

    shell中获取本机ip地址 方法一: /sbin/ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr ...

  3. Windows下获取本机IP地址方法介绍

    Windows下获取本机IP地址方法介绍 if((hostinfo = gethostbyname(name)) != NULL) { #if 1 ; printf("IP COUNT: % ...

  4. shell脚本中获取当前所在目录地址

    shell脚本中获取当前所在目录如下 #!/bin/bash work_path=$() cd ${work_path} work_path=$(pwd) cd ${work_path}/src

  5. [转]Shell脚本中获取SELECT结果值的方法

    http://blog.itpub.net/13885898/viewspace-1670297/ 有时候我们可能会需要在Shell脚本中执行SELECT语句,并将结果赋值给一个变量,对于这样的情形, ...

  6. 在unity 脚本中获取客户端的IP地址

    需要using System.Net.NetworkInformation;原理就是获取网卡的信息. //下面这段代码是我在百度贴吧找来的,经检验是正确的 string userIp = " ...

  7. Shell 命令行获取本机IP,grep的练习

    Shell 命令行获取本机IP,grep的练习 在 mac 下面输入 ifconfig 或者在 linux 下面输入 ip a 就可以得到我们的网卡信息.不过通常情况下,我们需要查看的是我们的IP地址 ...

  8. 学习Linux shell脚本中连接字符串的方法

    这篇文章主要介绍了Linux shell脚本中连接字符串的方法,如果想要在变量后面添加一个字符,可以用一下方法: 代码如下: $value1=home $value2=${value1}"= ...

  9. Linux shell脚本中shift

    Linux shell脚本中shift的用法说明 shift命令用于对参数的移动(左移),通常用于在不知道传入参数个数的情况下依次遍历每个参数然后进行相应处理(常见于Linux中各种程序的启动脚本). ...

随机推荐

  1. MYSQL锁表问题解决

    本文实例讲述了MYSQL锁表问题的解决方法.分享给大家供大家参考,具体如下: 很多时候!一不小心就锁表!这里讲解决锁表终极方法! 案例一 ? 1 mysql>show processlist; ...

  2. ubuntu安装scrapy方法

    sudo apt-get install python-dev   [默认安装python2] sudo apt-get install python3-dev   [指定安装python3最新的] ...

  3. winform 子窗体调用父窗体中的方法

    在父窗体里定义委托 public delegate void inis(string str); 在父窗体中定义要调用的方法 public void inigs(string gs) { textBo ...

  4. HDU 6063 17多校3 RXD and math(暴力打表题)

    Problem Description RXD is a good mathematician.One day he wants to calculate: ∑i=1nkμ2(i)×⌊nki−−−√⌋ ...

  5. 模拟php curl向远程服务器上传文件

    test.php <?php header('content-type:text/html;charset=utf8'); $file = dirname(__FILE__).'/1.jpg'; ...

  6. [LeetCode&Python] Problem 697. Degree of an Array

    Given a non-empty array of non-negative integers nums, the degree of this array is defined as the ma ...

  7. C语言--第三周作业评分和总结(5班)

    作业链接:https://edu.cnblogs.com/campus/hljkj/CS2017-5/homework/1073 一.评分要求 要求1 完成PTA第三周所有题(20分). 要求2 4道 ...

  8. java中实现Comparable接口实现自定义排序

    class Student implements Comparable{ String name; int gpa; @Override public int compareTo(Object arg ...

  9. (2)Django入门

    web框架:把一个请求拆成几部分,每部分做相同的事 python中常用的框架 1.Django:大而全的框架 2.flask:微框架又叫轻量级的框架 3.Tornado:高性能框架 pycharm创建 ...

  10. 《DSP using MATLAB》Problem5.16

    代码: %% ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ %% Output In ...