写这个脚本的背景:因为管理的机器越来越多,每次登录机器都可以显示相应机器的信息防止误操作。同时个人工作的环境有多个跳板机, 也容易操作错误, 所以每台机器上加个登录显示。
创建登录显示脚本
修改
vim /etc/profile
在最后一行加入
/usr/local/bin/login.sh
将脚本login.sh 放到/usr/local/bin
chmod 755 /usr/local/bin/login.sh
脚本内容如下(只在CentOS 6和CentOS 7试通过)
==================================
#!/bin/bash
#Server OS info
OS_version=”
if grep -q ‘release 6’ /etc/RedHat-release ; then
OS_version=’CentOS6′
else
OS_version=’CentOS7′
fi
echo -e “#################################Server INFO#################################################”
hostname=`hostname`
echo -e “HostName = $hostname”
osinfo=`cat /etc/redhat-release`
echo -e “OS info = $osinfo”
kernelinfo=`uname -r`
echo -e “LinuxKernel = $kernelinfo”
kernelmac=`uname -m`
echo -e “HW Machine = $kernelmac”
CPU=`cat /proc/cpuinfo | grep “model name” | head -n 1 | awk -F “:” “{print $2}”`
echo -e “CPU info = $CPU”
if [ ${OS_version} == ‘CentOS6’ ];then
network=`ifconfig -a | awk ‘BEGIN {FS=”\n”; RS=””} {print $1,$2}’ | grep -v ‘lo’ | awk ‘{print “\t\t”$1,$7}’`
else
network=`ifconfig -a | awk ‘BEGIN {FS=”\n”; RS=””} {print $1,$2}’ | grep -v ‘lo’ | awk ‘{print “\t\t”$1,$6}’`
fi
echo -e “NetWork info = \n$network”
externalip=$(timeout 3 curl -s ipecho.net/plain;echo)
[ $? -ne 0 -o ‘X’ == “$externalip”X ] && externalip=’No outside network or ACL drop’
echo -e “External IP = $externalip”
echo -e “———————————————————————————————“
Username=`whoami`
echo -e “UserName = $Username”
echo -e “UserHomeDir = $HOME”
echo -e “———————————————————————————————“
重新登录机器,显示如下
当切换到其他用户时,相应的显示当前用户的工作目录。
本文永久更新链接地址:http://www.linuxidc.com/Linux/2017-03/142265.htm