Distribution System Introduction Expect Script Remote Login Expect Script Remote Execute Command Expect Script Pass Parameters

expect script remote login

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

 

  Automatic remote login

 #! /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

After automatic remote login, execute the command and exit

#!/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"

  pass parameters

  pass parameters
#!/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

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325069627&siteId=291194637