用expect实现SCP/SSH自动输入密码登录

在命令行ssh远程登录服务器和scp远程传输文件都需要交互式输入密码,无法像mysql登录数据库 mysql -uroot -p123456一样直接完成。

其实可以用脚本依赖expect来达到这一目的。

首先安装expect:

[root@yqtrack-jumphost src]# yum -y install expect

SSH登录脚本:

#!/usr/bin/expect
set timeout 30
spawn ssh root@192.168.1.93
expect "password:"
send "123456@\$abcdef\r"
interact

SCP传送文件脚本:

#!/usr/bin/expect
set timeout 30
spawn scp /usr/local/src/zbx_redis_stats.py root@192.168.1.93:/usr/local/src/
expect "password:"
send "123456@\$abcdef\r" 
interact

注意:密码有特殊字符如“$”需要转义;
   密码以“\r”结尾。

 

[THE END]  

  

posted @ 2017-11-28 17:19  Lambeto  阅读(5676)  评论(0编辑  收藏  举报