2021-05-12 14:32:11
Linux Shell 登入顯示歡迎資訊或機器資訊
寫這個指令碼的背景:因為管理的機器越來越多,每次登入機器都可以顯示相應機器的資訊防止誤操作。同時個人工作的環境有多個跳板機, 也容易操作錯誤, 所以每台機器上加個登入顯示。
建立登入顯示指令碼
修改
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 "tt"$1,$7}'`
else
network=`ifconfig -a | awk 'BEGIN {FS="n"; RS=""} {print $1,$2}' | grep -v 'lo' | awk '{print "tt"$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
相關文章