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. random_select

    package sorttest;   //expected and worst running time is O(n),asuming that the elements are distinct ...

  2. HDU 6034 17多校1 Balala Power!(思维 排序)

    Problem Description Talented Mr.Tang has n strings consisting of only lower case characters. He want ...

  3. 索引rebuild与rebuild online区别

    索引rebuild与rebuild online区别 1.0目的,本篇文档探讨索引rebuild 与 rebuild online的区别 2.0猜测:已有的知识 2.1对索引rebuild重建会对表申 ...

  4. Django之模板层-语法:{{ }}

    模版语法的深度查询(.) views.py def index(request): name = 'name' lis = [1,2,3,4,5,6] dic = {"name": ...

  5. 认识MySQL中的索引

    一.什么是索引 索引是一种将数据库中单列或者多列的值进行排序的结构,引用索引可以大大提高索引的速度. 二.索引的优缺点 优点:整体上提高查询的速度,提高系统的整体性能. 缺点:创建索引和维护索引都需要 ...

  6. Linux系统下curl命令上传文件,文件名包含逗号无法上传

    使用curl命令,将备份好的图片全部重新导入到seaweedfs,图片全部以存储在seaweedfs中的fid命令, fid中间有一个逗号,使用curl命令时报错: curl: (26) couldn ...

  7. [LeetCode&Python] Problem 13. Roman to Integer

    Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ...

  8. python性能分析——insert()

    我们在list中插入数据时,经常使用这两个函数: append():在列表的末尾增加一个数据 insert():在某个特定位置前加一个数据 Python内的list实现是通过数组实现的,而不是链表的形 ...

  9. priority_queue的基本用法

    #include<bits/stdc++.h> using namespace std; int main() { ]; ;i<=;i++) a[i]=i; sort(a+,a++, ...

  10. spring+springMVC,声明式事务失效,原因以及解决办法

    http://blog.csdn.net/z69183787/article/details/37819627#comments 一.声明式事务配置: <bean id="transa ...