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

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

 expect -c "
  spawn scp [email protected]:/root/1.log /root
  expect {
    \"*assword\" {set timeout 300; send \"password\r\";}
    \"yes/no\" {send \"yes\r\"; exp_continue;}
  }
  expect eof"

2、ssh

#!/bin/bash
expect -c "
spawn ssh [email protected] \"ls;\"
expect {
    \"*assword\" {set timeout 300; send \"password\r\";}
    \"yes/no\" {send \"yes\r\"; exp_continue;}
      }
expect eof"

你可能感兴趣的:(shell)