使用expect实现scp ssh自动输入密码登录

expect是一种自动交互语言,能实现在shell脚本中为scp和ssh等自动输入密码自动登录。
下面给出scp和ssh的使用示例:
1、scp

  1. expect -c "
  2.   spawn scp [email protected]:/root/1.log /root
  3.   expect {
  4.     "*assword" {set timeout 300; send "passwordr";}
  5.     "yes/no" {send "yesr"; exp_continue;}
  6.   }
  7.   expect eof"

2、ssh

  1. #!/bin/bash
  2. expect -c "
  3. spawn ssh [email protected] "ls;"
  4. expect {
  5.     "*assword" {set timeout 300; send "passwordr";}
  6.     "yes/no" {send "yesr"; exp_continue;}
  7.       }
  8. expect eof
  9.             "