Shell判断输入是否有效IP和字母

简介: Shell判断输入是否有效IP和字母

1、需求目的

   最近在研究ansible自动化工具,在主机表(/etc/ansible/hosts)添加管理节点信息时,几台到十几台还可以手动添加,到了百十台工作量就大了。于是想用脚本自动添加,减少工作量,刚开始想到for循环自动添加,但添加的IP、用户、密码,都不相同,实现起来比较困难,也没太多时间去研究,结果就用了手动交互输入。

用户信息表如下格式:

# cat /etc/ansible/hosts

192.168.1.100   ansible_ssh_user=user  ansible_ssh_pass=123

2、实现思路

   2.1  先输入IP,并判断输入的是否有效IP地址和输入的IP是否已经存在

   2.2  判断输入用户名是否为字母

   2.3  密码就不多说,如上述都满足条件,则通过变量引用将节点信息追加到主机表中,并打印输入内容

3、测试

wKioL1TNiiOCwn4QAALd2jSUE0s070.jpg

4、脚本内容如下

#====================================================
# Author: lizhenliang - EMail:zhenliang369@163.com
# Last modified: 2015-02-1
# Filename: input_hostinfo.sh
# Description: 
# blog:lizhenliang.blog.51cto.com
#====================================================
User_File=/etc/ansible/hosts
Check_IP()
{
while true
do
    read -p "Please input IP address: " ip
    IP_Count=`cat user_info |grep "\<$ip\>" |wc -l`     #统计用户信息文件中是否包含输入的IP,如果已存在则重新输入
    if [ -z $ip ];then      #输入不能为空
        echo "Enter not null." 
    elif [ $ip == exit -o $ip == quit -o $ip == q ];then        #如果输入exit、quit、q就退出输入模式
        exit 1;
    elif [  $IP_Count -eq 1 ];then
        echo "IP $ip is already! Please enter the IP agagin." 
    elif [[ $ip =~ ^[1-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[1-9]{1,3}$ ]];then     #输入的不是数字或不是IP格式,则重新输入
        #^$:从开始到结束是数字才满足条件,=~:一个操作符,表示左边是否满足右边(作为一个模式)正则表达式
        a=`echo $ip |cut -d. -f1`
        b=`echo $ip |cut -d. -f2`
        c=`echo $ip |cut -d. -f3`
        d=`echo $ip |cut -d. -f4`
        if [ $a -le 255 -a $b -le 255 -a $c -le 255 -a $d -le 255 ];then
        #当满足输入条件时,截取IP四段数字进行整数比较,判断四段数字是否小于或等于255,同时满足条件,跳出所有循环继续,如果其中一个不满足,则重新输入
            break;
        else
            echo "IP format error,Please enter the IP again."
        fi
    else
        echo "IP format error,Please enter the IP again."
    fi
done
}
########################################################
Check_User()
{
while true
do
    read -p "Please input username: " username
    if [ -z $username ];then        #输入不能为空
        echo "Enter not null."
    elif [[ ! $username =~ ^[a-z]+$ ]];then      #输入的不是字母,则重新输入
        echo "Enter the username must is letter."
    else
        break;      #当满足输入条件时,跳出所有循环
    fi
done
}
########################################################
Password()
{
    read -p "Please input pass: " Password
}
########################################################
Check_IP;
Check_User;
Password;
echo "$ip   ansible_ssh_user=${username}    ansible_ssh_pass=${Password}" >> $User_File
#如果上面输入都满足条件,将输入的信息追加到用户信息文件中,并打印追加内容
echo "------------------------"
tail -n 1 $User_File


相关文章
|
9月前
|
Shell 分布式数据库
shell脚本中if判断‘-a‘ - ‘-z‘含义
shell脚本中if判断‘-a‘ - ‘-z‘含义
|
2月前
|
Shell Linux 开发工具
$加数字在Shell中的含义
$加数字在Shell中的含义
60 0
$加数字在Shell中的含义
|
12月前
|
Shell Perl
输入数字运行相应命令
输入数字运行相应命令
58 1
|
11月前
|
Shell PHP
浅谈无数字字母shell
浅谈无数字字母shell
94 0
|
11月前
|
Shell
Shell 输入/输出的重定向含义(>、>>、2>、2>>、&>、&>>、1>&2、2>&1)
Shell 输入/输出的重定向含义(>、>>、2>、2>>、&>、&>>、1>&2、2>&1)
46 0
|
弹性计算 Shell Linux
3天玩转shell--5.变量截取字符串
本文将通过shell代码示例,简单通俗的讲解shell。通过执行代码和运行结果反向掌握shell编程方法。准备一台低配的阿里云ECS Linux环境,跟着教程走起,本文比较适合shell小白。
102 0
|
Shell
SHELL判断文件是否包含某个字串
SHELL判断文件是否包含某个字串
161 0
|
Shell
SHELL中变量字串中包含$时怎么办?
SHELL中变量字串中包含$时怎么办?
103 0
|
Web App开发 Shell
72、shell正则表达式判断ip地址
1、表达式书写:[2] regex_ip="(2[0-4][0-9]|25[0-5]|1[0-9][0-9]|[1-9]?[0-9])(\.(2[0-4][0-9]|25[0-5]|1[0-9][0-9]|[1-9]?[0-9])){3}" 意思是 (0-255)(点号  0-255)3次 echo "123.
1443 0