〖Linux〗bash和expect执行ssh命令行sshcmd.exp

 1 #!/usr/bin/expect -f
 2 # sudo apt-get install expect
 3 # ./ssh.exp user passwd server
 4 set user [lrange $argv 0 0]
 5 set pass [lrange $argv 1 1]
 6 set server [lrange $argv 2 2]
 7 set cmds [lrange $argv 3 $argc]
 8 
 9 spawn ssh -o StrictHostKeyChecking=no $user@$server $cmds
10 match_max 100000
11 expect "*?assword:*"
12 send -- "$pass\r"
13 send -- "\r"
14 interact

使用举例:

  sshcmd.exp user passwd 192.168.1.100 ls -l /

你可能感兴趣的:(expect)