Tag: Shell Determines whether the input is valid IP shell determines whether the input is a letter
1. Purpose of demand
Recently in the study of ansible automation tools, in the host table (/etc/ansible/hosts) Add Management node information, a few to more than 10 units can also be added manually, to the Baishi station workload is large. So want to use a script to automatically add, reduce the workload, just beginning to think of the For loop automatically add, but add the IP, user, password, are not the same, the implementation is more difficult, there is not much time to study, the result is the use of manual interaction input.
The User Information table is in the following format:
# cat/etc/ansible/hosts
192.168.1.100 Ansible_ssh_user=user ansible_ssh_pass=123
2. Realization of Ideas
2.1 Enter the IP first, and determine whether the input is valid IP address and the input IP is already present
2.2 Determine if the input user name is a letter
2.3 The password is not much said, if the above conditions are met, the node information is appended to the host table by a variable reference, and the input content is printed
3. Testing
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/59/40/wKioL1TNiiOCwn4QAALd2jSUE0s070.jpg "title=" QQ picture 20150201100145.png "alt=" Wkiol1tniiocwn4qaald2jsue0s070.jpg "/>
4. The contents of the script are as follows
#====================================================
# Author:lizhenliang-email:[email Protected]
# 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, re-enter if it already exists
If [-Z $ip];then #输入不能为空
echo "Enter not NULL."
elif [$IP = = Exit-o $ip = = Quit-o $ip = = q];then #如果输入exit, quit, Q exit input mode
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格式, re-enter
#^$: A number satisfies a condition from start to finish, =~: An operator that indicates whether the left side satisfies the right (as a pattern) regular expression
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
#当满足输入条件时, intercept the IP four segment number for an integer comparison, to determine whether the four-segment number is less than or equal to 255, while satisfying the conditions, jump out of all loops continue, if one is not satisfied, re-enter
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 #输入的不是字母, re-enter
echo "Enter The username must is letter."
Else
Break #当满足输入条件时, jump out of all loops
Fi
Done
}
########################################################
Password ()
{
Read-p "Please input pass:" Password
}
########################################################
CHECK_IP;
Check_user;
Password;
echo "$ip Ansible_ssh_user=${username} Ansible_ssh_user=${password}" >> $User _file
#如果上面输入都满足条件, append the entered information to the user information file, and print the appended content
echo "------------------------"
Tail-n 1 $User _file
This article is from the "Penguin" blog, please be sure to keep this source http://lizhenliang.blog.51cto.com/7876557/1610406
Shell determines whether the input is valid IP and letter