#!/usr/bin/expect

set remote_user "用户名" set remote_host "远程服务器IP" set remote_password "远程服务器密码" set remote_path "/远程服务器目录/"

set local_path "/本地目录/"

远程登录

spawn ssh $remote_user@$remote_host

期望出现密码输入提示

expect { "password:" { send "$remote_password\r" exp_continue } "yes/no" { send "yes\r" exp_continue } "Last login" { # 进入远程服务器目录 send "cd $remote_path\r" expect "$remote_path*" # 查找包含backconf的文件或文件夹 set result [exec find . -name "backconf"] # 依次传输 foreach file $result { send "scp $file $local_path\r" expect { "password" { send "$remote_password\r" exp_continue } "100%" { # 传输成功,继续传输下一个 send_user "传输成功:$file\n" exp_continue } } } # 传输完成,退出 send "exit\r" }

编写一个shell脚本本机A使用expect远程登录远端服务器B然后传输远端服务器B某个目录下的包含backconf文件或文件夹到本机Assh客户端未设置免密登录。