码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-04-30 00:02:35      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:远程执行命令   /tmp   har   exit   远程   zh-cn   传递参数   continue   /usr   

expect脚本远程登录

  yum install -y expect
  yum install -y tcl tclx tcl-devel

 

  自动远程登录

 #! /usr/bin/expect
set host "192.168.133.132"
set passwd "123456"
spawn ssh root@$host
expect {
"yes/no" { send "yes\r"; exp_continue}
"password:" { send "$passwd\r" }
}
interact

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

#!/usr/bin/expect
set user "root"
set passwd "123456"
spawn ssh $user@192.168.133.132
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"

  传递参数

  传递参数
#!/usr/bin/expect
set user [lindex $argv 0]
set host [lindex $argv 1]
set passwd "123456"
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"

chmod +x ex3.sh
[root@localhost sbin]# ./ex3.sh  root 192.168.1.3 ls

 

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

标签:远程执行命令   /tmp   har   exit   远程   zh-cn   传递参数   continue   /usr   

原文地址:https://www.cnblogs.com/xiaobo-Linux/p/8972292.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!