centos ssh和scp利用expect自动填充密码

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

介绍在centos6.5环境下,脚本依赖expect来达到这一目的,首先确认安装 expect和spawn。

密码有特殊字符如$需要转义

ssh登录脚本,ip在执行脚本时动态输入:

#!/usr/bin/expect
set timeout 30
set ip [lindex $argv 0 ]
spawn ssh -l root $ip
expect "password:"
send "xxxx@\$e1111\r"
interact

scp传输脚本

#!/usr/bin/expect cd /home/fastdfs/storage spawn scp data.tar.gz root@192.168.1.111:/home/data/storage set timeout 30 expect "password:" send "xxxx@\$e1111\r" interact

相关文章

Centos下搭建性能监控Spotlight
CentOS 6.3下Strongswan搭建IPSec VPN
在CentOS6.5上安装Skype与QQ
阿里云基于centos6.5主机VPN配置
CentOS 6.3下配置multipah
CentOS安装、配置APR和tomcat-native