20.27分发系统介绍;20.28expect脚本远程登录;20.29expect脚本远程执行命令;20.30expect脚本传递参数

简介:

20.27 分发系统介绍

shell项目-分发系统-expect

20.28 expect脚本远程登录

1. 安装expect

[root@hao-01 ~]# yum install -y expect

自动远程登录

2. 创建配置1.expect脚本(远程登录)

[root@hao-01 ~]# vim 1.expect

添加内容(自动远程登录hao2机器并执行命令)

#! /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.expectwKiom1nB6YPw4mzaAAAlIKFFDHI340.png

spacer.gif20.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.expectwKiom1nB6ZrBbXc4AAAt7veA6gk638.png

spacer.gif

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"wKiom1nB6a_QiVDRAAKcNcaltsI331.png

spacer.gif











本文转自 主内安详 51CTO博客,原文链接:http://blog.51cto.com/zhuneianxiang/1967098,如需转载请自行联系原作者
目录
相关文章
|
4月前
|
NoSQL Shell 数据安全/隐私保护
搞定shell脚本expect自动化交互输入密码等就是这么简单
搞定shell脚本expect自动化交互输入密码等就是这么简单
153 0
|
Shell 网络安全 数据安全/隐私保护
expect - 自动交互脚本
1. expect参数 expect教程中文版 expect说明 2. 启用选项 -c :执行脚本前先执行的命令,可多次使用。 -d :debug模式,可以在运行时输出一些诊断信息,与在脚本开始处使用 exp_internal 1 相似。
1389 0
|
Shell 网络安全 开发工具
|
Shell 网络安全 数据安全/隐私保护
|
Shell 数据安全/隐私保护 应用服务中间件