【shell编程】分发系统介绍、expect脚本远程登录、expect脚本远程执行命令和expect脚本传递参数

一、分发系统介绍

后续补上


二、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

【shell编程】分发系统介绍、expect脚本远程登录、expect脚本远程执行命令和expect脚本传递参数_第1张图片


三、expect脚本远程执行命令

进入sbin目录创建2.expect文件,将以下内容复制到2.expect里面:



自动远程登录后,执行命令并退出:


#!/usr/bin/expect
set user "root"
set passwd "123456a"
spawn ssh [email protected]


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"

【shell编程】分发系统介绍、expect脚本远程登录、expect脚本远程执行命令和expect脚本传递参数_第2张图片


赋予权限:

chmod a+x 2.expect 



执行./2.expect命令进入了172.16.17.70机器并执行了两条命令后退出:

【shell编程】分发系统介绍、expect脚本远程登录、expect脚本远程执行命令和expect脚本传递参数_第3张图片


可以检查一下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"


【shell编程】分发系统介绍、expect脚本远程登录、expect脚本远程执行命令和expect脚本传递参数_第4张图片


执行命令./3.expect root 172.16.17.70 ls ,登录上70的机器并执行ls命令:

【shell编程】分发系统介绍、expect脚本远程登录、expect脚本远程执行命令和expect脚本传递参数_第5张图片


(多个参数命令: ./3.expect root 172.16.17.70 "ls;w;vmstat 1")

【shell编程】分发系统介绍、expect脚本远程登录、expect脚本远程执行命令和expect脚本传递参数_第6张图片







你可能感兴趣的:(【shell编程】分发系统介绍、expect脚本远程登录、expect脚本远程执行命令和expect脚本传递参数)