时间:2016-02-26 18:56 来源: 我爱IT技术网 作者:佚名
欢迎您访问我爱IT技术网,今天小编为你分享的电脑教程是服务器系列之:【Linux在批量服务器管理中实用的PS1命令提示符格式实现方法】,下面是详细的分享!
Linux在批量服务器管理中实用的PS1命令提示符格式实现方法
#!/bin/sh
#########################################################################
# Update PS1 like [root@192.168.1.113 /data]# #
#########################################################################
#先判断网卡是否存在,我这边eth1是内网网卡
ifconfig eth1 >/dev/null 2>&1
if [[ $? !=0 ]]
then
echo 'interface eth1 not exsit!';
exit 1
fi
#Centos/Redhat 7 ifconfig显示的结果不是 inet addr: 而是 inet 直接加IP,所以这里需要判断下:
function Get_eth1IP()
{
if [[ $1 -eq 7 ]]
then
#for centos 7
eth1_IP=$(ifconfig eth1 |awk '/inet / {print $2}'|awk '{print $1}')
else
eth1_IP=$(ifconfig eth1 |awk -F":" '/inet addr:/ {print $2}'|awk '{print $1}')
fi
}
test -f /etc/redhat-release && grep 7 /etc/redhat-release >/dev/null 2>&1 && Get_eth1IP 7
test -f /etc/centos-release && grep 7 /etc/redhat-release >/dev/null 2>&1 && Get_eth1IP 7 || Get_eth1IP
echo $eth1_IP | grep -E "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}" >/dev/null 2>&1
if [[ $? !=0 ]]
then
echo 'eth1_IP is empty!'
exit 1
fi
function Export()
{
echo "export PS1='\[\e[32m\][\u@${eth1_IP}:\[\e[m\]\[\e[33m\]\w\[\e[m\]\[\e[32m\]]\[\e[m\]\\$ '">>${1} && \
echo -e "\033[32m Update \033[0m \033[33m${1}\033[33m \033[32mSuccess! Please relogin your system for refresh... \033[0m"
}
function home_env()
{
if [[ ! -z $1 ]]
then
home=$1
else
home=/root
fi
#有的用户可能会在家目录下自定义一些配置,即 .proflie这个隐藏文件,所以也需要更新
test -f $home/.profile && (
sed -i '/export PS1=/d' $home/.profile
Export $home/.profile
)
}
#获取当前用户id,如果是root组的则可以操作/etc/profile
userid=$(id | awk '{print $1}' | sed -e 's/=/ /' -e 's/(/ /' -e 's/)/ /'|awk '{print $2}')
if [[ $userid=0 ]]
then
#for all
sed -i '/export PS1=/d' /etc/profile
Export /etc/profile
#for root
home_env
#如果其他用户需要修改,只要开启一下三行,并将other修改成用户名
#id other >/dev/null 2>&1 && (
# home_env ~other
#)
else
#for userself
home_env ~
fi
以上就是关于Linux在批量服务器管理中实用的PS1命令提示符格式实现方法的服务器维护教程分享,更多电脑教程请移步到>>电脑教程频道。
- 评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
-
