zoukankan      html  css  js  c++  java
  • Linux 项目 shell 自动获取报告本机IP (1) | 通过shell 自动获取报告本机IP

    由于电脑设置静态IP经常出现链接不上网络,动态IP又非常不方便,故有了这个想法并实现

    原理:

      Linux,包含PC机器,树莓派等,通过shell 自动获取报告本机IP  | 通过 Mutt+Msmtp邮箱发送    

    此次使用树莓派3B实现步骤:

    1.安装mutt 和 Msmtp

    $ sudo apt -get install mutt   //安装mutt,其他系统使用相应包管理
    
    $ sudo apt-get install msmtp   //安装msmtp,其他系统使用相应包管理

    2.在/etc/Muttrc 下配置 mutt

    ##2019 1 25 send ip to my mail
    set sendmail="/usr/bin/msmtp"
    set use_from=yes
    set realname=" Pi's address4  "
    set from =  A 邮箱地址
    set envelope_from=yes
    set crypt_use_gpgme = no

    3.在/root/下  新建配置msmtp

    account default
    host smtp.aliyun.com                
    from your A mail @aliyun.com
    auth plain
    user your A mail @aliyun.com   //A邮箱地址
    password  密码                       
    logfile ~/.msmtp.log  // 存储日志

    4.编写shell 获取 ip 并发送

    # check network availability 
    while true
    do
      TIMEOUT=5
      SITE_TO_CHECK="www.163.com"
      RET_CODE=`curl -I -s --connect-timeout $TIMEOUT $SITE_TO_CHECK -w %{http_code} | tail -n1`
      if [ "x$RET_CODE" = "x200" ]; then
      echo "Network OK, will send mail..."
      break
      else
      echo "Network not ready, wait..."
      sleep 1s
      fi
    done
     
    # get the IP address of eth0
    ETH0_IP_ADDR=` ifconfig wlan0 | sed -n "2,2p" | awk '{print substr($2,1)}' `
    ECHO_IP=`ifconfig  `
    # send the Email
    echo " test  ifconfig wlan0:$ECHO_IP " | mutt -s "Current time:`date '+%F %T'`    Raspiberry: $ETH0_IP_ADDR " B 邮箱地址

    此时通过运行脚本便可发送  ip   到  B邮箱

    5.添加开机自启动脚本就可实现   -->> 开机启动的多种方式设置

    这次使用 更改/etc/rc.load 方式自启动,在exit0 上面添加内容

    #!/bin/sh -e
    #
    # rc.local
    #
    # This script is executed at the end of each multiuser runlevel.
    # Make sure that the script will "exit 0" on success or any other
    # value on error.
    #
    # In order to enable or disable this script just change the execution
    # bits.
    #
    # By default this script does nothing.
    
    # Print the IP address
    _IP=$(hostname -I) || true
    if [ "$_IP" ]; then
      printf "My IP address is %s
    " "$_IP"
    fi
    #在exit0 上面添加内容
    su pi -c "source /etc_sh/send_ip_mail.sh"   //shell文件地址
    exit 0
  • 相关阅读:
    七种常见的回归分析—转载
    Python模块之 __future__ 转载
    Java 快速排序
    在给定范围内产生指定个数不重复的随机数
    Java 冒泡排序
    jquery文本框效果
    jquery复选框
    struts2下的Ajax
    java线程系列---Runnable和Thread的区别
    System.getProperty("user.dir")
  • 原文地址:https://www.cnblogs.com/wxiaote/p/10861622.html
Copyright © 2011-2022 走看看