shell脚本小记-记录IP变化

anonymous_vhacker
anonymous_vhacker
发布于 2023-01-13 / 15 阅读
0
0

shell脚本小记-记录IP变化

记录IP变化的脚本

脚本文件:

monitorIP.sh

用法:

./monitorIP.sh <网卡名> <分钟数>

后台监测用法:

nohup ./monitorIP.sh <网卡名> <分钟数> &

关闭后台监测方法:

  1. ps -ef | grep monitorIP.sh 找到pid

  2. sudo kill <pid> 杀掉进程

脚本内容:

#!/bin/bash

if [[ -z $1 ]];then
        echo -e "Usage: $0 <interface> <num_of_minutes> \neg: $0 eth0 60"
        exit 1
fi
if [[ -z $2 ]];then
        echo -e "Usage: $0 <interface> <num_of_minutes> \neg: $0 eth0 60"
        exit 1
fi

expr $2 "+" 10 &> /dev/null
if [[ $? -eq 0 ]];then
        minutes=$2
else
        echo -e "Usage: $0 <interface> <num_of_minutes> \neg: $0 eth0 60"
        exit 1
fi

fileName=$1"_IP_"`date '+%s'`".csv"

i=0
while ((i<=minutes))
do
        echo `ifconfig $1 | grep inet | awk 'NR==1' | awk '{print $2}'`,$1,`date "+%F %T"` >> $fileName
        ((i++))
        sleep 60
done

评论