20.27 分发系统介绍shell
shell项目-分发系统-expectvim
20.28 expect脚本远程登陆ssh
1. 安装expect :ide
[root@hao-01 ~]# yum install -y expectspa
自动远程登陆blog
2. 建立配置1.expect脚本(远程登陆) :ip
[root@hao-01 ~]# vim 1.expectit
添加内容(自动远程登陆hao2机器,并执行命令):class
#! /usr/bin/expect登录
set host "192.168.211.129"
set passwd "admin"
spawn ssh root@$host
expect {
"yes/no" { send "yes\r"; exp_continue}
"password:" { send "$passwd\r" }
}
interact
3. 增长1.expect脚本x权限 :
[root@hao-01 ~]# chmod a+x 1.expect
4. 执行1.expect脚本(远程登陆) :
[root@hao-01 ~]# ./1.expect
20.29 expect脚本远程执行命令
自动远程登陆后,执行命令并退出
1. 远程hao2机器,建立/tmp/12.txt文件,追加剧定向1212到/tmp/12.txt文件 :
[root@hao-01 ~]# vim 2.expect
添加内容:
#!/usr/bin/expect
set user "root"
set passwd "admin"
spawn ssh $user@192.168.211.129
expect {
"yes/no" { send "yes\r"; exp_continue}
"password:" { send "$passwd\r" }
}
expect "]*"
send "touch /tmp/12.txt\r"
expect "]*"
send "echo 1212 > /tmp/12.txt\r"
expect "]*"
send "exit\r"
2. 增长2.expect脚本x权限 :
[root@hao-01 ~]# chmod a+x 2.expect
3. 执行2.expect脚本 :
[root@hao-01 ~]# ./2.expect
20.30 expect脚本传递参数
传递参数
1.
[root@hao-01 ~]# vim 3.expect
添加内容:
#!/usr/bin/expect
set user [lindex $argv 0]
set host [lindex $argv 1]
set passwd "admin"
set cm [lindex $argv 2]
spawn ssh $user@$host
expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect "]*"
send "$cm\r"
expect "]*"
send "exit\r"
2. 增长3.expect脚本x权限 :
[root@hao-01 ~]# chmod a+x 3.expect
3. 执行3.expect脚本 :
远程登陆到指定用户名 主机ip 执行的多个命令(ls;w)
[root@hao-01 ~]# ./3.expect root 192.168.211.129 "ls;w"