一、分发系统介绍
后续补上
二、expect脚本远程登录
安装:yum install -y expect
进入sbin目录创建1.expect文件,将以下内容复制到1.expect里面:
赋予1.expect权限:
chmod a+x 1.expect
自动远程登录脚本:
#! /usr/bin/expect
set host "172.16.17.70"
set passwd "123456a"
spawn ssh root@$host
expect {
"yes/no" { send "yes\r"; exp_continue}
"assword:" { send "$passwd\r" }
}
interact
三、expect脚本远程执行命令
进入sbin目录创建2.expect文件,将以下内容复制到2.expect里面:
自动远程登录后,执行命令并退出:
#!/usr/bin/expect
set user "root"
set passwd "123456a"
spawn ssh $user@172.16.17.70
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"
赋予权限:
chmod a+x 2.expect
执行./2.expect命令进入了172.16.17.70机器并执行了两条命令后退出:
可以检查一下70的机器,文件和内容都生成了:
四、expect脚本传递参数
进入sbin目录创建3.expect文件,将以下内容复制到3.expect里面:
赋予权限:
传递参数 :
#!/usr/bin/expect
set user [lindex $argv 0]
set host [lindex $argv 1]
set passwd "123456a"
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"
执行命令./3.expect root 172.16.17.70 ls ,登录上70的机器并执行ls命令:
(多个参数命令: ./3.expect root 172.16.17.70 "ls;w;vmstat 1")
本文介绍了expect脚本在Linux环境下的应用,包括如何实现远程登录、执行命令及传递参数。通过示例展示了如何创建expect脚本,如1.expect、2.expect和3.expect,用于自动登录远程主机并执行指定的命令。
1048

被折叠的 条评论
为什么被折叠?



