在命令行ssh远程登录服务器和scp远程传输文件都需要交互式输入密码,无法像MySQL登录数据库 mysql -uroot -p123456一样直接完成。
介绍在centos环境下,脚本依赖expect来达到这一目的,首先确认安装 expect
密码有特殊字符如$需要转义
ssh登录脚本,ip在执行脚本时动态输入:
#!/usr/bin/expect
set timeout 30
set ip [lindex $argv 0 ]
spawn ssh -l root $ip
expect "password:"
send "[email protected]\$e1111\r"
interact
scp传输脚本
#!/usr/bin/expect
cd /home/fastdfs/storage
spawn scp data.tar.gz [email protected]:/home/data/storage
set timeout 30
expect "password:"
send "[email protected]\$e1111\r"
interact
时间: 2024-08-08 03:08:43