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

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

 

posted @ 2018-04-29 23:07  zhaocundang  阅读(326)  评论(0编辑  收藏  举报