秘籍----免交互几种方法
1.echo方法
#!/bin/bash
cd /usr/local/src/elasticsearch-head/
echo "
回车
" | npm run start &
sleep 5
netstat -lnupt | grep 9100
netstat -lnupt | grep 9200
//或者
#!/bin/bash
cd /usr/local/src/elasticsearch-head/
npm run start &
sleep 5
echo "
"
netstat -lnupt | grep 9100
netstat -lnupt | grep 9200
//老师的方法
echo
"n
##创建磁盘
p
w" | fdisk /dev/sda
2.expect方法
yum install -y expect
mkdir /abc
/usr/bin/expect<<-EOF
spawn mount.cifs //192.168.1.150/redhat7 /abc
expect "*redhat7*" {send "123456\r"}
expect eof
EOF
#!/bin/bash
######部署elasticsearch软件####
yum install -y expect
mkdir /abcd
echo
"123456
" | mount.cifs //192.168.1.150/redhat7 /abcd
对于需求1 要求的自动登陆ftp,并作系列操作,则可以用这种方法进行自动交互。代码如下:
#!/bin/bash
ftp -i -n 192.168.167.187 << EOF
user hzc 123456
pwd
cd test
pwd
close
bye
EOF
测试可以发现,如上代码使用帐号名hzc,密码123456成功登陆了ftp服务器,并进入目录,打印出了pwd。
五、自动交互方法二
需求2中要求采用非交互的方式改变登录用户密码,尝试用方法1,无法实现。
这时候联想到交互信息的另一个自动输入方法,管道,通过echo + sleep + | 可以实现这个需求。
#!/bin/bash
(echo "curpassword"
sleep 1
echo "newpassword"
sleep 1
echo "newpassword")|passwd
测试通过,运行这个脚本,直接把当前用户的curpassword改成newpassword。
六、自动交互方法三
需求3中要求自动登录root账号,尝试方法1和方法2,都出现错误提示standard in must be a tty。
这时候尝试寻找外部帮助,一个shell工具expect可以实现这个功能,其实expect就是一个专门用来实现自动交互功能的工具,expect的语法可以参考相关资料,代码如下:
#!/usr/bin/expect
spawn su root
expect "password: "
send "123456\r"
expect eof
exit
测试通过,运行这个脚本,直接从当前用户登录到root用户。
七、方法总结
方法一(重定向)简单直观,也经常有实际应用,但是在自动交互领域功能有限。
方法二(管道)也很简单直观,有时甚至不用sleep配合就能展现强大的自动交互实力,但是在某些时候也束手无策。
方法三(expect)在功能上是最为强大的,expect本来就是为实现自动交互功能而生,但是缺点是需要安装expect包,在嵌入式等环境下难以安装。
三个方法各有优劣,应用的好,都可以完成Linux Shell自动交互。
echo "123
456
789" > my.cof
cat > /etc/my.conf <<EOF
123
456
789
EOF
1. 修改shell
#!/usr/bin/expect
set USER [lindex $argv 0]
set SHELL [lindex $argv 1]
set timeout 3
spawn chsh $USER
expect "*]:*" { send "$SHELL\r" }
expect eof
# ./chsh.sh user1 /bin/tcsh
2. 修改密码
#!/usr/bin/expect
set USER [lindex $argv 0]
set PASS "1q2w#E\$R"
set timeout 3
spawn passwd $USER
expect "*Password:*" { send "$PASS\r" }
expect "*Password:*" { send "$PASS\r" }
expect eof
# ./pass.sh user1
或把用户和密码都作为参数
#!/usr/bin/expect
set USER [lindex $argv 0]
set PASS [lindex $argv 1]
set timeout 3
spawn passwd $USER
expect "*Password:*" { send "$PASS\r" }
expect "*Password:*" { send "$PASS\r" }
expect eof
# ./pass.sh ttt 1q2w#E$R
# ./pass.sh ttt "1q2w#E\$R"
总结:expect 必须要匹配最后一个输出字符。
远程交互ssh 登录:
1. 设置变量,执行多命令。
#!/usr/bin/expect
set IP "10.85.138.42"
set timeout 3
spawn ssh ${IP}
expect "*yes/no*" { send "yes\r" }
expect "*assword*" { send "root\r"}
expect "*#*" { send "ls\r" }
expect "*#*" { send "touch /tanjiyong/newfile\r" }
expect eof
#./exp.sh
2. 增加参数。
#!/usr/bin/expect
set IP [lindex $argv 0]
set USER [lindex $argv 1]
set timeout 3
spawn ssh $USER@${IP}
expect "*yes/no*" { send "yes\r" }
expect "*assword*" { send "root\r"}
expect "*#*" { send "ls\r" }
expect "*#*" { send "touch /tanjiyong/newfile\r" }
expect eof
#./exp.sh 10.85.138.42 root
3. ssh 登录,执行时间超过timeout时间,设定timeout为-1(无限制)。
#!/usr/bin/expect
set IP [lindex $argv 0]
set USER [lindex $argv 1]
set IP2 [lindex $argv 2]
set timeout 3
spawn ssh $USER@${IP}
expect "*yes/no*" { send "yes\r" }
expect "*assword*" { send "root\r"}
expect "*#*" { send "ls\r" }
expect "*#*" { send "ping $IP2\r" }
set timeout -1
expect "*#*" { send "exit 1\r" }
expect eof
4. ssh 登录,使用循环,在30台机器执行相同命令。
#!/usr/bin/expect
set USER root
set PASS root
for {set i 1} {$i<=30} {incr i} {
spawn ssh -l $USER 125.1.1.$i
expect "*yes/no*" { send "yes\r" }
expect "*assword*" { send "$PASS\r"}
expect "*#*" { send "find / -name hao.txt\r" }
expect eof
}
#./exp.sh
本地远程交互执行:
1. spawn 执行scp
#!/usr/bin/expect
set PASS root
set timeout 3
spawn scp /etc/passwd root@10.85.138.42:/tanjiyong
expect "*yes/no*" { send "yes\r" }
expect "*Password:*" { send "$PASS\r" }
set timeout -1
expect "*#*" { send "exit 1\r" }
# ./scp.sh
使用-- send
#!/usr/bin/expect --
set PASS root
set USER root
set IP 10.85.138.42
set env(SHELL) /bin/bash
set timeout 1
spawn $env(SHELL) #spawn /bin/bash
expect "*#*" { send "/usr/bin/scp /etc/passwd $USER@$IP:/tanjiyong\r" }
expect "*yes/no*" { send "yes\r" }
expect "*Password:*" { send "$PASS\r" }
set timeout -1 #复制的时间较长,设置为timeout无限制
expect "*#*" { send "exit 1\r" }
#!/usr/bin/expect
set PASS root
set USER root
set IP 10.85.138.42
set env(SHELL) /bin/bash
set timeout 1
spawn $env(SHELL)
expect "*#*" { send -- "/usr/bin/scp /etc/passwd $USER@$IP:/tanjiyong\r" }
expect "*yes/no*" { send "yes\r" }
expect "*Password:*" { send "$PASS\r" }
set timeout -1 #复制的时间较长,设置为timeout无限制
expect "*#*" { send "exit 1\r" }
#!/usr/bin/expect
set timeout 3
set env(SHELL) /bin/bash
spawn \$env(SHELL)
expect -exact "# "
send -- "scp $TAR_NAME ${USERNAME}@${DESTIP}{DESTDIR}\r"
expect {
"*yes/no*" { send "yes\r";exp_continue }
"assword: " { send "hw2009\r" }
}
set timeout -1 #复制的时间较长,设置为timeout无限制
expect "# "
send "exit\r"
expect eof
2. expect 搜索块
#!/usr/bin/expect
set PASS root
set timeout 3
spawn scp /etc/passwd root@10.85.138.42:/tanjiyong
expect {
"*yes/no*" { send "yes\r";exp_continue }
"assword: " { send "$PASS\r" }
}
set timeout -1
expect "*#*" { send "exit 1\r" }
# ./scp.sh
3. 判断
#!/usr/bin/expect
set PASS [lindex $argv 0]
set USER root
set IP 10.85.138.42
set env(SHELL) /bin/bash
set timeout 1
spawn $env(SHELL)
expect "*#*" { send "/usr/bin/scp /etc/passwd $USER@$IP:/tanjiyong\r" }
expect {
"*yes/no*" { send "yes\r" }
"*Password:*" { send "$PASS\r" }
}
expect "*Password:*" { send_user "\nPasswd error!\n";exit 1 }
set timeout -1
expect "# " { send "exit 1\r" }
expect eof
# ./scp.sh
4. rsync 备份使用。
#!/usr/bin/expect --
spawn ssh backup@10.85.138.212
expect {
"(yes/no)?" {
send "yes\r"
}
"assword" {
send "123456\r"
}
}
send "rsync -avz rsync@10.85.138.212:/home/html /opt\r"
expect "total size"
expect {
"rsync error" {
exit 1
}
}
expect "# "
send "exit\r"
interact
#expect eof
脚本中使用:
#!/bin/bash
echo "Start..."
cat << EOF > /expectfile
#!/usr/bin/expect
set PASS root
set USER root
set IP 10.85.138.42
set env(SHELL) /bin/bash
set timeout 1
spawn \$env(SHELL) #必须加上\,不然会被置换为空。
expect "*#*" { send "/usr/bin/scp /etc/passwd \$USER@\$IP:/tanjiyong\r" }
expect "*yes/no*" { send "yes\r" }
expect "*Password:*" { send "\$PASS\r" }
set timeout -1
expect "# " { send "exit 1\r" }
expect eof
EOF
expect -f /expectfile
echo "$?"
echo "finished backup.."
附:修改SSH Client为非ask模式
vi /etc/ssh/ssh_config
# StrictHostKeyChecking ask
StrictHostKeyChecking no
参考:自动登录
#!/usr/bin/expect
if {$argc!=3} { # 疑问:参数个数($#)
send_user "Usage: $argv0 {Array IP} {Password} {CMD}\n\n"
exit
}
set IP [lindex $argv 0]
set Password [lindex $argv 1]
set CMD [lindex $argv 2]
spawn ssh admin@$IP
expect {
"assword:" {
exec sleep 1
send "${Password}\r"
}
"*continue connecting*" {
exec sleep 1
send "yes\r"
expect "*Password:" {
exec sleep 1
send "${Password}\r"
}
}
}
expect {
"*Password*" { send_user "\n1assword error!\n"
exit
}
"*already*" { send_user "\n2:Repeated login!\n"
exit
}
"OceanStor: admin>" { send "${CMD}\r" }
}
expect "*>"
send "exit\r"
expect "*closed*"
exit
脚本2
#!/usr/bin/expect
set ipaddress [lindex $argv 0]
set passwd [lindex $argv 1]
spawn ssh -p 22 root@$ipaddress
expect {
"want" {send -- "yes\r"; exp_continue}
"password:" {send -- "$passwd"}
"No route" { exit }
}
set timeout 5
send "\n"
expect "*justin*"
send "pwd\r"
expect "*OK*"
send "exit\r"
expect eof
相关:
exit 1/exit #停止执行,退出脚本
expect eof #脚本结束
expect -exact "# " #精确匹配
expect "# "
expect "*#*" #模糊匹配
exp_continue #在同一个expect块里,做多次匹配。
send #发送命令。
send_user #打印终端信息。用法与send一致。
#获取ip地址
b=ifconfig ens33 |tr -s ' ' : |cut -d: -f3 |sed -n '2p'
Listen '$b':80 "注意要加分号,不加分号报错"
#"整行替换"
sed -i '1c123' test.txt 把第一行整行替换