Shell编程学习笔记
程序员文章站
2022-07-12 12:11:27
...
1.检测某一网段中可达主机,并通过自动回答脚本实现ssh远程连接获取连接到主机名称
程序脚本
#!/bin/bash
#################################################
# Author :Lockey
# Email :aaa@qq.com
# Version :v1.1.1
# Create_date : 2017-08-22
# Description : ip test shell script
#################################################
#function to show usage
#用户输入不符合程序参数要求时输出用法示例
function print_usage {
cat << EOF
This utility can detect host reachability!
Usage: ./testip ipstart ipend zone
eg: ./testip 0 255 172.25.254
EOF
echo -e "\n"
}
#function to test the ip reachability
function testIp {
for((i=$1;i<=$2;i++))
do
ip=$3.$i
ping -c1 -w1 $ip &>/dev/null
#ping网段内主机,根据命令执行返回值来确定下一步的动作
if [ $? -eq 0 ]
then
result=`/root/useradd/answer.exp $ip redhat hostname | grep foundation | cut -d . -f 1-3`
#执行自动应答脚本根据命令执行结果处理输出
echo $result | grep foundation &>/dev/null
if [ $? -ne 0 ]
then
echo "ssh connection refused by host $ip"
else
echo "$ip's hostname is: $result"
fi
else
echo "$ip can not be reached!"
fi
done
}
print_usage
read -p "Please input startip endip and zone: " ipstart ipend ipzone
#函数调用
testIp $ipstart $ipend $ipzone
expect自动应答脚本
#!/usr/bin/expect
set timeout 3
set num [ lindex $argv 0 ]
set passwd [ lindex $argv 1 ]
set com [ lindex $argv 2 ]
spawn ssh aaa@qq.com$num $com
#监控执行命令
#spawn scp /root/useradd/lockey-scp-loop.sh aaa@qq.com$num:/home/kiosk/Desktop
expect {
"authenticity" { send "yes\r";exp_continue }
"password" { send "$passwd\r" }
}
expect eof
#interact
2. 通过脚本导入文件数据批量添加用户、删除用户或者修改用户密码
#!/bin/bash
#################################################
# Author :Lockey
# Email :aaa@qq.com
# Version :v1.1.1
# Create_date : 2017-08-22
# Description : user management shell script
#################################################
###function to conduct user add by matched user file and passwd file
function Useradd {
if [ $userNum -ne $passwdNum ]
then
echo 'User number are not match the passwords line!'
else
count=1
for user in `cat $user_file`
do
id $user &>/dev/null
if [ $? -ne 0 ]
then
useradd $user
echo "$user successfully created!"
password=`sed -n "${count}p" $passwd_file`
count=$[ $count + 1 ]
passwd $user &>/dev/null<<end
$password
$password
end
else
if [ ! -e $modify_passwd ]
then
echo -e "User \033[31m$user\033[0m existed!"
password=`sed -n "${count}p" $passwd_file`
count=$[ $count + 1 ]
passwd $user &>/dev/null <<end
$password
$password
end
echo -e $user\'s password has been changed!"\n"
else
echo -e "User \033[31m$user\033[0m existed!"
fi
fi
done
fi
}
###function to conduct user delete by given user file
function Userdel {
for user in `cat $user_file`
do
userdel -r $user &>/dev/null
if [ $? -ne 0 ]
then
echo $user does not exist!
else
echo "$user successfully deleted!"
fi
done
}
###actions to judge if the given options are reasonable
if [ -z $1 ]
then
echo 'Please point out your action ( add|delete )!'
exit 1
fi
action=$1
if [ $action != "delete" ] && [ $action != "add" ]
then
echo 'Wrong action! add or delete should be specified!'
exit 1
elif [ -z $2 ]
then
echo 'Please input user file!'
exit 1
elif [ -z $3 ] && [ $action = 'add' ]
then
echo 'Please input passwd file!'
exit 1
elif [ ! -e $2 ]
then
echo 'User file not exist!'
exit 1
elif [ ! -e $3 ] && [ $action = 'add' ]
then
echo 'Passwd file not exist!'
exit 1
else
user_file=$2
passwd_file=$3
userNum=`wc -l $user_file | cut -d ' ' -f 1`
if [ $action = 'delete' ]
then
Userdel
elif [ $action = 'add' ]
then
passwdNum=`wc -l $passwd_file | cut -d ' ' -f 1`
modify_passwd=$4
Useradd
else
echo 'Wrong action!'
fi
fi
3. 给定秒数然后进行倒计时
#!/bin/bash
read -p "Please input seconds to start the countdown: " times
echo -e 'Time start!\n'
#限定倒计时最长时间为1天,如果不合理则默认为70s
if [ -e $times ] || [ $times -lt 0 ] || [ $times -gt 3600 ]
then
times=70
fi
m=`expr $times / 60 `
s=`expr $times % 60 `
for(( i=$m; i>=0; i-- ))
do if [ $i -lt 10 ]
then
mm="0${i}"
else
mm=$i
fi
for(( j=$s; j>=0; j-- ))
do
if [ $j -lt 10 ]
then
ss="0${j}"
else
ss=$j
fi
echo -n -----------------Time left: '***' $mm:$ss '***'-------------------
echo -ne "\r \r"
sleep 1
done
s=59
done
clear
echo 'Time over!'
4. 单行或者多行字符反转
!/bin/bash
for arg in aaa@qq.com
do
inputstr=$arg
len=${#inputstr}
for((i=1;i<=$len;i++))
do
echo -n ${inputstr:0-$i:1}
#字符从右切片,每次左移一位取一个字符
done
echo ""
done