The distribution system expects to log in remotely, execute commands, and pass parameters

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
The distribution system expects to log in remotely, execute commands, and pass parameters

Log in to aminglinux02
The distribution system expects to log in remotely, execute commands, and pass parameters

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
The distribution system expects to log in remotely, execute commands, and pass parameters

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
The distribution system expects to log in remotely, execute commands, and pass parameters
pass multiple parameters
The distribution system expects to log in remotely, execute commands, and pass parameters

Guess you like

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