Distribution system: shell online script expect realizes remote file transfer, command execution, system online and other functions
expect script remote login
vim 1.expect
#! /usr/bin/expect
set host "192.168.91.129"
set passwd "1q2w3e"
spawn ssh root @$host
expect {
"yes/no" { send "yes\r"; exp_continue}
"assword:" { send "$passwd\r" }
}
interact *Stay on the remote machine, if you want to log in to the remote machine and exit Come, use expect or eof, if you don't add it, exit immediately
Log in to aminglinux02
expect script to execute command remotely
vim 2.expect
#!/usr/bin/expect
set user "root"
set passwd "1q2w3e"
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"
[root@aminglinux-001 ~]# chmod a+x 2.expect
expect passes parameters
#!/usr/bin/expect
set user [lindex $argv 0]
set host [lindex $argv 1]
set passwd "1q2w3e"
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"
[root@aminglinux-001 ~]# chmod a+x 3. expect
to pass only one parameter and
pass multiple parameters