Linux云计算架构-shell入门到精通(放弃)[3]

Linux云计算架构-shell入门到精通(放弃)[3]

1. expect 实现无交互登录

# 安装expect
[root@client ~]# yum install expect -y
[root@client ~]# which expect
/usr/bin/expect

# 定义脚本执行的shell,即expect可执行文件的链接路径
#!/usr/bin/expect

# 设置超时时间
set timeout 30
set timeout -1

# 设置变量
set name "abong"
set passwd "123456"

# spawn命令,功能是给ssh进程加个壳,用来传递交互指令。【交互式输入的首条命令】

# expect命令,判断输出结果是否包含字符串,没有则立即返回。有则等待timeout时间后再返回。【交互式输入】
# send命令,执行交互动作。
# exp_continue命令,继续执行交互动作。

# interact命令,执行完交互动作后,把控制权交给控制台,若无该命令,交互完直接退出。

# $argv:expect脚本可以接受bash传递过来的参数,可以使用[lindex $argv n]获得,n从0开始。
# 交互式输入账号密码使用ssh登录远程主机
# 默认timeout为10s
[root@client ~]# vim ssh.exp 
#!/usr/bin/expect
set ipaddr "192.168.10.10"
set name "root"
set passwd "123456"
set timeout 30
spawn ssh $name@$ipaddr
expect {
    
    
"yes/no" {
    
    send "yes\r";exp_continue}
"password" {
    
    send "$passwd\r"}
}
expect {
    
    "#"}
send "touch /root/a.txt\r"
send "ls /etc > /root/a.txt\r"
send "exit\r"
expect eof
[root@client ~]# chmod +x ssh.exp
[root@client ~]# expect ssh.exp
spawn ssh [email protected]
[email protected]'s password: 
Last login: Sun Aug  2 20:32:30 2020 from 192.168.10.43
[root@master-lvs ~]# touch /root/a.txt
[root@master-lvs ~]# ls /etc > /root/a.txt
[root@master-lvs ~]# exit
登出
Connection to 192.168.10.10 closed.
# 免密使用ssh登录多台远程主机进行操作
[root@client ~]# vim ipaddr.txt
192.168.10.10 123456
192.168.10.20 123456
[root@client ~]# vim ssh.exp
#!/usr/bin/expect
set ipaddr [lindex $argv 0]
set name "root"
set passwd [lindex $argv 1]
set timeout 30
spawn ssh $name@$ipaddr
expect {
    
    
"yes/no" {
    
    send "yes\r";exp_continue}
"password" {
    
    send "$passwd\r"}
}
expect {
    
    "#"}
send "touch /root/a.txt\r"
send "ls /etc > /root/a.txt\r"
send "exit\r"
expect eof
[root@client ~]# chmod +x ssh.exp
[root@client ~]# vim login.sh
#!/bin/bash
for ip in `awk '{print $1}' /root/ipaddr.txt`
do
  pass=`grep $ip /root/ipaddr.txt | awk '{print $2}'`
  expect /root/ssh.exp $ip $pass
done
[root@client ~]# chmod +x login.sh
# 执行登录脚本
[root@client ~]# bash login.sh
spawn ssh [email protected]
[email protected]'s password: 
Last login: Sun Aug  2 21:08:42 2020 from 192.168.10.43
[root@master-lvs ~]# touch /root/a.txt
[root@master-lvs ~]# ls /etc > /root/a.txt
[root@master-lvs ~]# exit
登出
Connection to 192.168.10.10 closed.
spawn ssh [email protected]
[email protected]'s password: 
Last login: Sun Aug  2 21:09:23 2020 from 192.168.10.43
[root@backup_lvs ~]# touch /root/a.txt
[root@backup_lvs ~]# ls /etc > /root/a.txt
[root@backup_lvs ~]# exit
登出
Connection to 192.168.10.20 closed.

2. shell正则表达式

基础正则表达式

特殊字符 描述
^ 匹配开头
$ 匹配结尾
* 匹配前一个字符0次或多次,匹配*用\*
+ 匹配前一个字符1次或多次,匹配*用\+
? 匹配前一个字符0次或1次,匹配*用\?
\ 转义符
. 匹配除了换行符\n外的所有单个字符
() 匹配圆括号表达式的开始位置和结束位置,匹配(用\(,匹配)用\)
[] 匹配方括号表达式的开始位置和结束位置,匹配[用\[,匹配]用\]
{} 匹配大括号表达式的开始位置和结束位置,匹配{用\{,匹配}用\}
| 或 ,匹配|用\
\n 匹配换行符
\r 匹配回车符
\t 匹配制表符
# 去除/etc/ssh/sshd_config文件中的空行和#号开头的行
# 使用基础正则表达式
[root@client ~]# cat /etc/ssh/ssh_config | grep -v "^$" | grep -v "^#"
[root@client ~]# cat /etc/ssh/ssh_config | grep -v "^$\|^#"
# 使用扩展正则表达式
[root@client ~]# cat /etc/ssh/ssh_config | grep -E -v "^$|^#"
[root@client ~]# cat /etc/ssh/ssh_config | egrep -v "^$|^#"

# 匹配.字符,除换行符\n外的所有单个字符
[root@client ~]# grep .ot /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
setroubleshoot:x:991:985::/var/lib/setroubleshoot:/sbin/nologin

3. bash检查脚本和查看脚本执行过程

3.1 bash检查脚本是否存在语法错误

bash -v script_name.sh 查看bash是否存在语法错误

[root@client ~]# cat ping.sh 
#!/bin/bash
for ((i=40;i<45;i++))
do
  ping -c 3 192.168.10.$i &> /dev/null
  if [ $? -ne 0 ] ; then
    echo "192.168.10.$i is shutdown"
  else
    echo "192.168.10.$i is startup"
  fi
done
[root@client ~]# bash -v ping.sh
#!/bin/bash
for ((i=40;i<45;i++))
do
  ping -c 3 192.168.10.$i &> /dev/null
  if [ $? -ne 0 ] ; then
    echo "192.168.10.$i is shutdown"
  else
    echo "192.168.10.$i is startup"
  fi
done
192.168.10.40 is shutdown
192.168.10.41 is shutdown
192.168.10.42 is shutdown
192.168.10.43 is startup
192.168.10.44 is shutdown

3.2 bash查看脚本详细执行过程

bash -x script_name.sh 查看bash详细的执行过程

[root@client ~]# cat ping.sh 
#!/bin/bash
for ((i=40;i<45;i++))
do
  ping -c 3 192.168.10.$i &> /dev/null
  if [ $? -ne 0 ] ; then
    echo "192.168.10.$i is shutdown"
  else
    echo "192.168.10.$i is startup"
  fi
done
[root@client ~]# bash -x ping.sh
+ (( i=40 ))
+ (( i<45 ))
+ ping -c 3 192.168.10.40
+ '[' 1 -ne 0 ']'
+ echo '192.168.10.40 is shutdown'
192.168.10.40 is shutdown
+ (( i++ ))
+ (( i<45 ))
+ ping -c 3 192.168.10.41
+ '[' 1 -ne 0 ']'
+ echo '192.168.10.41 is shutdown'
192.168.10.41 is shutdown
+ (( i++ ))
+ (( i<45 ))
+ ping -c 3 192.168.10.42
+ '[' 1 -ne 0 ']'
+ echo '192.168.10.42 is shutdown'
192.168.10.42 is shutdown
+ (( i++ ))
+ (( i<45 ))
+ ping -c 3 192.168.10.43
+ '[' 0 -ne 0 ']'
+ echo '192.168.10.43 is startup'
192.168.10.43 is startup
+ (( i++ ))
+ (( i<45 ))
+ ping -c 3 192.168.10.44
+ '[' 1 -ne 0 ']'
+ echo '192.168.10.44 is shutdown'
192.168.10.44 is shutdown
+ (( i++ ))
+ (( i<45 ))

猜你喜欢

转载自blog.csdn.net/weixin_36522099/article/details/107752449