Linux cloud computing architecture-shell entry to proficient (give up)[3]

Linux cloud computing architecture-shell entry to proficient (give up)[3]

1. Expect to achieve no interactive login

# 安装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 regular expressions

Basic regular expression

Special characters description
^ Match beginning
$ End of match
* Match the previous character 0 or more times, match * with \*
+ Match the previous character one or more times, match * with \+
? Match the previous character 0 or 1 times, match * with \?
\ Escapes
. Match all single characters except newline \n
() Match the start position and end position of parenthesis expression, match (use \(, match) use \)
[] Match the start and end positions of the square bracket expression, match [with \[, match] with \]
{} Match the start position and end position of the brace expression, match {with \{, match} with \}
| Or, match|use\
\n Match newline
\r Match carriage return
\t Match tab
# 去除/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 check script and view script execution process

3.1 bash checks the script for syntax errors

bash -v script_name.sh Check if there is a syntax error in 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 view the detailed execution process of the script

bash -x script_name.sh View the detailed execution process of 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 ))

Guess you like

Origin blog.csdn.net/weixin_36522099/article/details/107752449