linux expect implements logging in to a remote host and executing sample command code

  • 2020-06-07 05:57:16
  • OfStack

This paper mainly introduces the linux expect implementation of login remote host and execution of commands related content, sharing for your reference and learning, the following is 1 to see the detailed introduction:

Sample code:


#!/bin/bash 
export cmd=$1
expect << 'END'
set cmd "$env(cmd)"
spawn ssh root@192.168.56.1 $cmd
expect {
 "(yes/no)?" {send "yes\r";exp_continue}
 "password:" {send "root\r";exp_continue}
 eof
}
END

explain

export defines a variable and gets $1 expect < < 'END' starts the expect script and ends END set cmd "$env(cmd)" gets the system variable spwan starts 1 process expect {} specific reply message

conclusion


Related articles: