错误:转载链接

# 1129 Host is blocked because of many connection errors; unblock with ‘mysqladmin flush-hosts’

原因:

同一个ip在短时间内产生太多(超过mysql数据库max_connection_errors的最大值)中断的数据库连接而导致的阻塞;

解决方法:

1、提高允许的max_connection_errors数量(治标不治本):

① 进入Mysql数据库查看max_connection_errors: show variables like ‘%max_connection_errors%’;

② 修改max_connection_errors的数量为1000: set global max_connect_errors = 1000;

③ 查看是否修改成功:show variables like ‘%max_connection_errors%’;

2、使用mysqladmin flush-hosts 命令清理一下hosts文件(不知道mysqladmin在哪个目录下可以使用命令查找:whereis mysqladmin);

① 在查找到的目录下使用命令修改:/usr/bin/mysqladmin flush-hosts -h192.168.1.1 -P3308 -uroot -prootpwd;

备注:

其中端口号,用户名,密码都可以根据需要来添加和修改;

配置有master/slave主从数据库的要把主库和从库都修改一遍;

第二步也可以在数据库中进行,命令如下:flush hosts;

显示数据库

show dbs

显示集合

show collections

切换数据库

use $db_name

切换集合

use $collection_name

查找


db.$collections_name.find().sort({"keyword": -1,"keyword":1}).limit();

                            sort(排序):1:正序;-1:倒序          limit:限制输出结果

#创建普通用户
db.createUser({user:"test",pwd:"123456",roles:[{role:"dbOwner",db:"iplat"}]});
#创建管理员用户
db.createUser({user:"root",pwd:"root123456",roles:[{role:"userAdminAnyDatabase",db:"admin"}]});

MongoDB默认日志为追加写入,文件大小将会越来越大

一、编写logAppend.js

use admin;  
db.runCommand({logRotate:1});

二、编写logAppend.sh

mongo 127.0.0.1:27017/admin /logAppend.js

三、权限设置
chmod 755 logAppend.*

四、编辑crontab

crontab -e  
0 3 * * * /save/mongodb/shell/logAppend.sh > /tmp/log.log 2>&1

1、将下载到的升级包上传到ESXi主机的存储根目录,关掉所有VMs并进入维护模式;

2、SSH登陆ESXi主机,执行vmware -v命令确认当前版本;

3、执行命令

esxcli software sources profile list --depot=/vmfs/volumes/datastore1/ESXi600-201610001.zip

确认能够看到profiles讯息,尤其是xxxx-standard的这个,注意,这里最少要有2个profiles才行,一个是带vmware tools的一个没有带vmware tools;

4、验证完成之后,执行如下命令开始升级:

esxcli software profile update --depot=/vmfs/volumes/datastore1/ESXi600-201610001.zip --profile=ESXi-6.0.0-20161004001-standard

5、等待升级完成,重启,执行vmware -v命令验证是否升级成功,退出维护模式。

 

很多人误以为shell脚本只能在命令行下使用。其实shell也可以调用一些GUI组件,例如菜单,警告框,进度条等等。你可以控制最终的输出,光标位置还有各种输出效果。下面我将介绍一些工具,帮助你创建强大的,互动的,用户友好的 Unix/Linux shell脚本。我在FreeBSD和Linux下测试过这些工具,不过其他UNIX系列的操作系统应该都支持的。

1. notify-send 命令

这个命令可以让你通过通知进程发送一个桌面通知给用户。这可以用来向用户发送提示,或者显示一些信息而不用打断用户工作。你需要安装如下软件包:

$ sudo apt-get install libnotify-bin

下面这个例子展示了如何从命令行向桌面发送一个简单的消息:

notify-send "rsnapshot done :)"

输出:
notify-send-rsnapshot-done

下面是一个复杂一点的例子:

....
alert=18000
live=$(lynx --dump http://money.rediff.com/ | grep 'BSE LIVE' | awk '{ print $5}' | sed 's/,//g;s/\.[0-9]*//g')
[ $notify_counter -eq 0 ] && [ $live -ge $alert ] && { notify-send -t 5000 -u low -i   "BSE Sensex touched 18k";  notify_counter=1; }
...

输出:
bse-sensex-touched-18k

这里的参数解释如下:

  •  -t 5000:指定超时的时间,毫秒
  •  -u low:设置是否紧急
  •  -i gtk-dialog-info:通知图标,你可以指定图标 -i /path/to/your-icon.png

2. tput 命令

这个命令是用来设置终端特性的:

  •   移动光标
  •   获得终端信息
  •   设置前景和背景色
  •   设置粗体模式
  •   设置反模式等等

举例:

#!/bin/bash
 
# clear the screen
tput clear
 
# Move cursor to screen location X,Y (top left is 0,0)
tput cup 3 15
 
# Set a foreground colour using ANSI escape
tput setaf 3
echo "XYX Corp LTD."
tput sgr0
 
tput cup 5 17
# Set reverse video mode
tput rev
echo "M A I N - M E N U"
tput sgr0
 
tput cup 7 15
echo "1. User Management"
 
tput cup 8 15
echo "2. Service Management"
 
tput cup 9 15
echo "3. Process Management"
 
tput cup 10 15
echo "4. Backup"
 
# Set bold mode
tput bold
tput cup 12 15
read -p "Enter your choice [1-4] " choice
 
tput clear
tput sgr0
tput rc

输出:
tput-%e5%91%bd%e4%bb%a4

3. setleds 命令

这个命令可以让你控制键盘灯,例如打开数字键盘灯:

setleds -D +num

关闭数字键盘灯:

setleds -D -num
  •   -caps: 清除大写灯
  •   +caps:打开大写灯
  •   -scroll:清除滚动锁
  •   +scroll:打开滚动锁

4. zenity 命令

这个命令可以显示GTK+的对话框,然后返回用户的输入。你可以用这个命令在脚本中显示信息,并要求用户输入信息。下面这段代码就是域名的whois查询:

#!/bin/bash
# Get domain name
_zenity="/usr/bin/zenity"
_out="/tmp/whois.output.$$"
domain=$(${_zenity} --title  "Enter domain" \
                --entry --text "Enter the domain you would like to see whois info" )
 
if [ $? -eq 0 ]
then
  # Display a progress dialog while searching whois database
  whois $domain  | tee >(${_zenity} --width=200 --height=100 \
                      --title="whois" --progress \
                        --pulsate --text="Searching domain info..." \
                                    --auto-kill --auto-close \
                                    --percentage=10) >${_out}
 
  # Display back output
  ${_zenity} --width=800 --height=600  \
         --title "Whois info for $domain" \
         --text-info --filename="${_out}"
else
  ${_zenity} --error \
         --text="No input provided"
fi

输出:

zenity-%e5%91%bd%e4%bb%a4

zenity: Linux / UNIX display Dialogs Boxes From The Shell Scripts

5. kdialog 命令

这个命令和zenity很想,只不过它是为KDE/QT应用准备的。使用方法如下:

kdialog --dontagain myscript:nofilemsg --msgbox "File: '~/.backup/config' not found."

输出
kdialog-%e5%91%bd%e4%bb%a4

你可以查看 shell scription with KDE Dialogs 来获取更多信息

6. Dialog

这个命令可以在shell脚本中显示文本组件。它使用了curses和ncurses类库。示例代码:

>#!/bin/bash
dialog --title "Delete file" \
--backtitle "Linux Shell Script Tutorial Example" \
--yesno "Are you sure you want to permanently delete \"/tmp/foo.txt\"?" 7 60
 
# Get exit status
# 0 means user hit [yes] button.
# 1 means user hit [no] button.
# 255 means user hit [Esc] key.
response=$?
case $response in
   0) echo "File deleted.";;
   1) echo "File not deleted.";;
   255) echo "[ESC] key pressed.";;
esac

 

7. logger 命令

这个命令可以让你写入系统日志例如 /var/log/messages:

logger "MySQL database backup failed."
tail -f /var/log/messages
logger -t mysqld -p daemon.error "Database Server failed"
tail -f /var/log/syslog

输出:
Apr 20 00:11:45 vivek-desktop kernel: [38600.515354] CPU0: Temperature/speed normal
Apr 20 00:12:20 vivek-desktop mysqld: Database Server failed

8. setterm 命令

这个命令可以设置中断的属性。下面的例子是强制屏幕全黑15分钟,并且60分钟后把显示器设为待机状态:

setterm -blank 15 -powersave powerdown -powerdown 60

下面这段命令可以在中断显示加下划线的文字:

setterm -underline on;
echo "Add Your Important Message Here"
setterm -underline off

或者你可以关闭光标:

setterm -cursor off

9. smbclient:

向 MS-Windows 系统发送消息
smbclient可以和 SMB/CIFS服务器通信。它可以向MS-Windows系统的指定用户发送消息:

smbclient -M WinXPPro <

或者

echo "${Message}" | smbclient -M salesguy2

10. Bash Socket 编程

你可以在bash中开启一个socket链接,并且传输数据。Bash有两个特殊的设备文件:

  •   /dev/tcp/host/port – 如果hostname,和port是合法的话,bash会尝试开启一个TCP连接。
  •   /dev/udp/host/port – 如果hostname和port是合法的话,bash会开启一个UDP连接。

你可以利用这个技术来测试一台主机的端口是否是开启的,而不需要使用nmap或者port扫描器:

# find out if TCP port 25 open or not
(echo >/dev/tcp/localhost/25) &>/dev/null && echo "TCP port 25 open" || echo "TCP port 25 close"

你可以 使用循环来查找开着的端口

echo "Scanning TCP ports..."
for p in {1..1023}
do
  (echo >/dev/tcp/localhost/$p) >/dev/null 2>&1 && echo "$p open"
done

输出:
Scanning TCP ports…
22 open
53 open
80 open
139 open
445 open
631 open

下面的这个例子让你的脚本扮演HTTP客户端:

#!/bin/bash
exec 3<> /dev/tcp/${1:-www.cyberciti.biz}/80
 
printf "GET / HTTP/1.0\r\n" >&3
printf "Accept: text/html, text/plain\r\n" >&3
printf "Accept-Language: en\r\n" >&3
printf "User-Agent: nixCraft_BashScript v.%s\r\n" "${BASH_VERSION}"   >&3
printf "\r\n" >&3
 
while read LINE <&3
do
   # do something on $LINE
   # or send $LINE to grep or awk for grabbing data
   # or simply display back data with echo command
   echo $LINE
done

关于GUITools和Cronjob

如果你使用cronjob来调用你的脚本的话,你要通过“ export DISPLAY=[user’s machine]:0 ”命令来设置本地的 display/input 服务。例如调用 /home/vivek/scripts/monitor.stock.sh脚本,它使用了 zenity 工具:

@hourly DISPLAY=:0.0 /home/vivek/scripts/monitor.stock.sh

所有的命令你都可以通过“man”来查询详细的使用方式。

VIA http://www.oschina.net/question/28_39527

1. pv 命令

有时候我们在电影屏幕上看到一些字幕一个个匀速显示出来,像有人在边敲键盘,边显示一样。Linux上的pv命令可以实现这种效果。
默认情况下,Linux是没有pv命令的,需要自行安装。
首先安装命令:
现在运行如下命令:
效果如下:
pv-command

2.toilet命令

toilet命令能够将输出的文本添加边框。

可以通过yum或者apt-get安装toilet命令。
运行如下命令:

效果如下:toilet-command

3.rig命令

rig能够随机产生一条人名、地名等记录。

可以通过yum或者apt-get安装rig命令。
运行如下命令:

效果如下:rig-command

4.aview命令

想象一下在屏幕上用ASCII码格式显示一张图片是什么效果??真的是酷炫到爆。Linux上aview命令可以达到这个效果。
可以通过yum或者apt-get安装aview命令。
运行如下命令:

效果如下:elephant

 

5.xeyes命令

xeyes命令是一个图形显示程序。运行这个程序,你会看到一双萌萌的眼睛会一直盯着你。

可以通过yum或者apt-get安装xeyes命令。

运行如下命令:

运行结果如下:

xeyes

6.cowsay命令

cowsay命令可以在命令行终端显示一些有趣的动物形状。

可以通过yum或者apt-get安装cowsay命令。

可以通过如下命令查看cowsay命令都能显示哪些形状:

现在执行如下命令:

效果如下:

cowsay

touch -d "2016-11-07 09:18:30" fillname

必选参数对长短选项同时适用。
  -a			只更改访问时间
  -c, --no-create	不创建任何文件
  -d, --date=字符串	使用指定字符串表示时间而非当前时间
  -f			(忽略)
  -h, --no-dereference		会影响符号链接本身,而非符号链接所指示的目的地
				(当系统支持更改符号链接的所有者时,此选项才有用)
  -m			只更改修改时间
  -r, --reference=FILE   use this file's times instead of current time
  -t STAMP               use [[CC]YY]MMDDhhmm[.ss] instead of current time
      --time=WORD        change the specified time:
                           WORD is access, atime, or use: equivalent to -a
                           WORD is modify or mtime: equivalent to -m
      --help		显示此帮助信息并退出
      --version		显示版本信息并退出

转载:http://blog.csdn.net/longxibendi/article/details/6412822

#!/bin/bash 
#author longxibendi 
#blog http://blog.csdn.net/longxibendi 
#function get the web servers information of companies' 
if [ -e ./web_server.log ] ;then 
 rm -f ./web_server.log 
fi 
if [ -e ./ab.log ] ; then 
 rm -f ./ab.log 
fi 
for company_name in taobao jd dangdang vancl mbaobao alibaba baidu yahoo sina sohu renren changyou wanmei 126 163 139 
do 
 { 
 ab -n 1 -c 1 http://www.${company_name}.com/index.html | tee ./ab_${company_name}.log 2>&1 
 if [ $? -eq 0 ] ; then 
 printf "%15s, %20s\n" "${company_name}.com", " ` cat ./ab_${company_name}.log | grep "Server Software" ` " >> ./web_server.log 
 fi 
 sleep 2 
 }& 
done 
wait 
echo "It is OK "

平常少不了进行新系统的安装,每次安装完新系统后都需要一个一个手动设置,异常繁琐,于是写了个脚本,每次安装完后直接运行就可以了

#CentOS安装iptables
centos_iptables_install ()
{
	read -p "是否安装iptables?[Y/n] " iptables_bool_path
	case $iptables_bool_path in
	y|Y|yes|YES)
		echo -e "现在开始安装iptables\n"
		yum install iptables-services -y
		return 1
		;;
	n|N|no|NO)
		echo -e "不安装iptables\n"
		return 0
		;;
	*)
		echo -e "输入有误,请重新输入\n"
		centos_iptables_install
		;;
	esac
}

#Ubuntu安装iptables
ubuntu_iptables_install ()
{
	read -p "是否安装iptables?[Y/n] " iptables_bool_path
	case $iptables_bool_path in
	y|Y|yes|YES)
		echo -e "现在开始安装iptables\n"
		#iptables脚本
		touch /etc/iptables.rules
		#设置开机启动
		touch /etc/network/interfaces.d/interfaces.tail
		echo "pre-up iptables-restore < /etc/iptables.rules" >> /etc/network/interfaces.d/interfaces.tail
		echo "iptables配置文件路径:/etc/iptables.rules"
		return 1
		;;
	n|N|no|NO)
		echo -e "不安装iptables\n"
		return 0
		;;
	*)
		echo -e "输入有误,请重新输入\n"
		ubuntu_iptables_install
		;;
	esac
	
}

#CentOS安装open-vm-tools
centos_vm_tools_install ()
{
	read -p "当前系统是否为虚拟机?[Y/n] " vm_bool
	case $vm_bool in
	y|Y|yes|YES)
		echo -e "现在开始安装open-vm-tools\n"
		yum install open-vm-tools* -y
		;;
	n|N|no|NO)
		echo -e "不安装open-vm-tools\n"
		;;
	*)
		echo -e "输入有误,请重新输入\n"
		centos_vm_tools_install
		;;
	esac
}

#Ubuntu安装open-vm-tools
ubuntu_vm_tools_install ()
{
	read -p "当前系统是否为虚拟机?[Y/n] " vm_bool
	case $vm_bool in
	y|Y|yes|YES)
		echo -e "现在开始安装open-vm-tools\n"
		apt install open-vm-tools* -y
		;;
	n|N|no|NO)
		echo -e "不安装open-vm-tools\n"
		;;
	*)
		echo -e "输入有误,请重新输入\n"
		ubuntu_vm_tools_install
		;;
	esac
}

#CentOS设置openssh-server
centos_ssh_server ()
{
	echo "禁止DNS反向解析"
	sed -i 's/#UseDNS yes/UseDNS no/' /etc/ssh/sshd_config
	systemctl start sshd
}

#Ubuntu安装openssh-server
ubuntu_ssh_server_install ()
{
	echo "开始安装openssh-server"
	apt install openssh-server -y
	read -p "是否开启ssh根用户登陆?[Y/n] " ssh_root_bool
	case $ssh_root_bool in
	y|Y|yes|YES)
		sed -i 's/PermitRootLogin prohibit-password/#PermitRootLogin prohibit-password/' /etc/ssh/sshd_config
		sed -i '/#PermitRootLogin prohibit-password/a\PermitRootLogin yes' /etc/ssh/sshd_config
		systemctl start ssh
		systemctl enable ssh
		;;
	n|N|no|NO)
		sed -i 's/PermitRootLogin prohibit-password/#PermitRootLogin prohibit-password/' /etc/ssh/sshd_config
		sed -i '/#PermitRootLogin prohibit-password/a\PermitRootLogin no' /etc/ssh/sshd_config
		systemctl start ssh
		systemctl enable ssh
		;;
	*)
		echo -e "输入有误,请重新输入\n"
		ubuntu_ssh_server_install
		;;
	esac
}

#关闭SELINUX
selinux ()
{
	read -p "是否关闭SELinux?[Y/n] " selinux_bool
	case $selinux_bool in
	y|Y|yes|YES)
		echo -e "正在关闭SELinux"
		sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/sysconfig/selinux
		;;
	n|N|no|NO)
		echo -e "不关闭SELinux\n"
		;;
	*)
		echo -e "输入有误,请重新输入\n"
		selinux
		;;
	esac
}

#关闭ufw
ufw_status ()
{
	read -p "是否关闭ufw?[Y/n] " ufw_bool
	case $ufw_bool in
	y|Y|yes|YES)
		echo -e "正在关闭ufw"
		ufw disable
		;;
	n|N|no|NO)
		echo -e "不关闭ufw\n"
		;;
	*)
		echo -e "输入有误,请重新输入\n"
		ufw_status
		;;
	esac
}

#关闭firewalld
firewalld_status ()
{
	read -p "是否关闭firewalld?[Y/n] " ufw_bool
	case $ufw_bool in
	y|Y|yes|YES)
		echo -e "正在关闭firewalld"
		systemctl stop firewalld.service
		systemctl disable firewalld.service
		;;
	n|N|no|NO)
		echo -e "不关闭firewalld\n"
		;;
	*)
		echo -e "输入有误,请重新输入\n"
		firewalld_status
		;;
	esac
}

#系统选择
system_choice ()
{
    if [ -z $# ]
    then
        echo "==============================================="
        echo "               请选择系统发行版                "
        echo "                1.Ubuntu 16.04                 "
        echo "                2.CentOS 7                     "
        echo "==============================================="

        read -p "请选择[1/2] " system_release_num
            case $system_release_num in
            1)
                system_release="ubuntu"
                echo -e "系统为Ubuntu,现在开始执行"
                ;;
            2)
                system_release="centos"
                echo -e "系统为CentOS,现在开始执行"
                ;;
            *)
                echo -e "输入有误,请重新输入\n"
                system_choice
                ;;
            esac
    elif [[ $# -eq 1 && $1 -eq 1 ]]
    then
        system_release="ubuntu"
    elif [[ $# -eq 1 && $1 -eq 2 ]]
    then
        system_release="centos"
    fi
}

#检测root身份
if [[ "$(whoami)" != "root" ]]
then
    echo -e "\033[41;37m 请使用root用户运行 \033[0m"
    exit 1
fi

#获取系统发行版名称
if grep -Eqi "CentOS" /etc/issue || grep -Eq "CentOS" /etc/*-release; then
    release='CentOS'
elif grep -Eqi "Ubuntu" /etc/issue || grep -Eq "Ubuntu" /etc/*-release; then
    release='Ubuntu'
else
    release='unknow'
    echo -e "\033[41;37m 您的系统发行版不是CentOS或Ubuntu,若要继续安装,请选择以哪个系统方式安装 \033[0m"
    system_choice
fi

if [[ -s /etc/redhat-release ]];then
    version=`grep -oE  "[0-9.]+" /etc/redhat-release | awk -F . '{ print $1 }'`
else
    version=`grep -oE  "[0-9.]+" /etc/issue| awk -F . '{ print $1 }'`
fi

if [[ $release == "Ubuntu" &&  $version -ge 16 ]]
then
    system_choice 1
elif  [[ $release == "CentOS" && $version -ge 7 ]]
then
    system_choice 2
fi

case $system_release in
ubuntu)
	kill -9 `ps -ef | grep /var/lib/dpkg/ | grep -v grep | awk '{print $2}'`

	echo -e "系统发行版为ubuntu,现在执行安装\n"

	ubuntu_ssh_server_install

	ubuntu_vm_tools_install

	ufw_status

	ubuntu_iptables_install
	if [[ $? -eq 1 ]]
	then
		echo -e "\033[41;37m 已安装iptables \033[0m"
	else
		echo -e "\033[41;37m 未进行安装iptables \033[0m"
	fi

	apt install lrzsz vim wget -y

	#个性化.bashrc
	echo > /root/.bashrc
	cat >> /root/.bashrc << 'EOF'
# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples

# If not running interactively, don't do anything
case $- in
    *i*) ;;
      *) return;;
esac

# don't put duplicate lines or lines starting with space in the history.
# See bash(1) for more options
HISTCONTROL=ignoreboth

# append to the history file, don't overwrite it
shopt -s histappend

# for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
HISTSIZE=1000
HISTFILESIZE=2000

# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize

# If set, the pattern "**" used in a pathname expansion context will
# match all files and zero or more directories and subdirectories.
#shopt -s globstar

# make less more friendly for non-text input files, see lesspipe(1)
[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"

# set variable identifying the chroot you work in (used in the prompt below)
if [ -z "${debian_chroot:-}" ] && [ -r /etc/debian_chroot ]; then
    debian_chroot=$(cat /etc/debian_chroot)
fi

# set a fancy prompt (non-color, unless we know we "want" color)
case "$TERM" in
    xterm-color|*-256color) color_prompt=yes;;
esac

# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt
force_color_prompt=yes

if [ -n "$force_color_prompt" ]; then
    if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
	# We have color support; assume it's compliant with Ecma-48
	# (ISO/IEC-6429). (Lack of such support is extremely rare, and such
	# a case would tend to support setf rather than setaf.)
	color_prompt=yes
    else
	color_prompt=
    fi
fi

if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi
unset color_prompt force_color_prompt

# If this is an xterm set the title to user@host:dir
case "$TERM" in
xterm*|rxvt*)
    PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
    ;;
*)
    ;;
esac

# enable color support of ls and also add handy aliases
if [ -x /usr/bin/dircolors ]; then
    test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
    alias ls='ls --color=auto'
    #alias dir='dir --color=auto'
    #alias vdir='vdir --color=auto'

    alias grep='grep --color=auto'
    alias fgrep='fgrep --color=auto'
    alias egrep='egrep --color=auto'
fi

# colored GCC warnings and errors
export GCC_COLORS='error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01'

# some more ls aliases
alias ll='ls -alhF'
alias la='ls -A'
alias l='ls -CF'
alias nano='nano -$'

# Add an "alert" alias for long running commands.  Use like so:
#   sleep 10; alert
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'

# Alias definitions.
# You may want to put all your additions into a separate file like
# ~/.bash_aliases, instead of adding them here directly.
# See /usr/share/doc/bash-doc/examples in the bash-doc package.

if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
fi

# enable programmable completion features (you don't need to enable
# this, if it's already enabled in /etc/bash.bashrc and /etc/profile
# sources /etc/bash.bashrc).
if ! shopt -oq posix; then
  if [ -f /usr/share/bash-completion/bash_completion ]; then
    . /usr/share/bash-completion/bash_completion
  elif [ -f /etc/bash_completion ]; then
    . /etc/bash_completion
  fi
fi


#set time style
export TIME_STYLE='+%Y-%m-%d %H:%M:%S'

#history格式化 用户-IP-时间-命令
USER_IP=`who -u am i 2>/dev/null| awk '{print $NF}'|sed -e 's/[()]//g'`
if [ -z $USER_IP  ]
then
  USER_IP="NO_client_IP"
fi
export HISTTIMEFORMAT="$USER from $USER_IP %F %T "

man() {
env \
LESS_TERMCAP_mb=$(printf "\e[1;31m") \
LESS_TERMCAP_md=$(printf "\e[1;31m") \
LESS_TERMCAP_me=$(printf "\e[0m") \
LESS_TERMCAP_se=$(printf "\e[0m") \
LESS_TERMCAP_so=$(printf "\e[1;44;33m") \
LESS_TERMCAP_ue=$(printf "\e[0m") \
LESS_TERMCAP_us=$(printf "\e[1;32m") \
man "$@"
}
EOF
	
	#root用户添加.profile
	cp /etc/skel/.profile /root/
	#开启root用户桌面登录
	sudo echo -e "[SeatDefaults]\n#autologin-user=root\nuser-session=ubuntu\ngreeter-show-manual-login=true" > /usr/share/lightdm/lightdm.conf.d/50-ubuntu.conf
	;;
centos)
	kill -9 `ps -ef | grep yum | grep -v grep | awk '{print $2}'`

	#配置源
	echo -e "系统发行版为centos,现在执行安装\n"
	echo "开始配置yum源"
	echo "正在配置163源"
	mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
	wget  -O /etc/yum.repos.d/CentOS7-Base-163.repo http://mirrors.163.com/.help/CentOS7-Base-163.repo
	echo "正在配置aliyun源"
	wget -O /etc/yum.repos.d/CentOS-Base-Aliyun.repo http://mirrors.aliyun.com/repo/Centos-7.repo
	wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

	selinux

	centos_ssh_server

	centos_vm_tools_install

	firewalld_status

	centos_iptables_install
	if [[ $? -eq 1 ]]
	then
		echo -e "\033[41;37m 已安装iptables \033[0m"
	else
		echo -e "\033[41;37m 未进行安装iptables \033[0m"
	fi

	yum install lrzsz nano vim -y

	#个性化.bashrc
	echo > /root/.bashrc
	cat >> /root/.bashrc << 'EOF'
# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples

# If not running interactively, don't do anything
case $- in
    *i*) ;;
      *) return;;
esac

# don't put duplicate lines or lines starting with space in the history.
# See bash(1) for more options
HISTCONTROL=ignoreboth

# append to the history file, don't overwrite it
shopt -s histappend

# for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
HISTSIZE=1000
HISTFILESIZE=2000

# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize

# If set, the pattern "**" used in a pathname expansion context will
# match all files and zero or more directories and subdirectories.
#shopt -s globstar

# make less more friendly for non-text input files, see lesspipe(1)
[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"

# set variable identifying the chroot you work in (used in the prompt below)
if [ -z "${debian_chroot:-}" ] && [ -r /etc/debian_chroot ]; then
    debian_chroot=$(cat /etc/debian_chroot)
fi

# set a fancy prompt (non-color, unless we know we "want" color)
case "$TERM" in
    xterm-color|*-256color) color_prompt=yes;;
esac

# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt
force_color_prompt=yes

if [ -n "$force_color_prompt" ]; then
    if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
	# We have color support; assume it's compliant with Ecma-48
	# (ISO/IEC-6429). (Lack of such support is extremely rare, and such
	# a case would tend to support setf rather than setaf.)
	color_prompt=yes
    else
	color_prompt=
    fi
fi

if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi
unset color_prompt force_color_prompt

# If this is an xterm set the title to user@host:dir
case "$TERM" in
xterm*|rxvt*)
    PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
    ;;
*)
    ;;
esac

# enable color support of ls and also add handy aliases
if [ -x /usr/bin/dircolors ]; then
    test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
    alias ls='ls --color=auto'
    #alias dir='dir --color=auto'
    #alias vdir='vdir --color=auto'

    alias grep='grep --color=auto'
    alias fgrep='fgrep --color=auto'
    alias egrep='egrep --color=auto'
fi

# colored GCC warnings and errors
export GCC_COLORS='error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01'

# some more ls aliases
alias ll='ls -alhF'
alias la='ls -A'
alias l='ls -CF'
alias nano='nano -$'

# Add an "alert" alias for long running commands.  Use like so:
#   sleep 10; alert
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'

# Alias definitions.
# You may want to put all your additions into a separate file like
# ~/.bash_aliases, instead of adding them here directly.
# See /usr/share/doc/bash-doc/examples in the bash-doc package.

if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
fi

# enable programmable completion features (you don't need to enable
# this, if it's already enabled in /etc/bash.bashrc and /etc/profile
# sources /etc/bash.bashrc).
if ! shopt -oq posix; then
  if [ -f /usr/share/bash-completion/bash_completion ]; then
    . /usr/share/bash-completion/bash_completion
  elif [ -f /etc/bash_completion ]; then
    . /etc/bash_completion
  fi
fi


#set time style
export TIME_STYLE='+%Y-%m-%d %H:%M:%S'

#history格式化 用户-IP-时间-命令
USER_IP=`who -u am i 2>/dev/null| awk '{print $NF}'|sed -e 's/[()]//g'`
if [ -z $USER_IP  ]
then
  USER_IP="NO_client_IP"
fi
export HISTTIMEFORMAT="$USER from $USER_IP %F %T "

man() {
env \
LESS_TERMCAP_mb=$(printf "\e[1;31m") \
LESS_TERMCAP_md=$(printf "\e[1;31m") \
LESS_TERMCAP_me=$(printf "\e[0m") \
LESS_TERMCAP_se=$(printf "\e[0m") \
LESS_TERMCAP_so=$(printf "\e[1;44;33m") \
LESS_TERMCAP_ue=$(printf "\e[0m") \
LESS_TERMCAP_us=$(printf "\e[1;32m") \
man "$@"
}
EOF

	;;
*)
	echo -e "传入参数有误"
	;;
esac