blog

当前位置:首页>文章中心>blog
用expect在脚本中自动输入密码
blog 2024-02-05 1 阅读

用expect在脚本中自动输入密码

小泥巴 小泥吧科技 2024-02-05
今天遇到一个小问题,我在用scp同步证书文件 到别一台服务器时,要输入私钥的密码,gpt4帮我写了以下脚本 ,注意要用expect执行并不是sh. 加入cron时要这样写  0 3 * * * expect /root/auto/cp-cert.sh > /dev/null
#!/usr/bin/expect -f

# 设置本地目录路径
set source_dir "/www/server/panel/vhost/cert/QQ.com"

# 设置目标主机和目标路径
set target_user "root"
set target_host "QQ.com"
set target_path "/etc/nginx/cert"

# 设置要输入的密码
set password "123456"

# 设置超时时间为无限
set timeout -1

# 执行 SCP 命令
spawn scp -r $source_dir $target_user@$target_host:$target_path

# 等待输入密码提示
expect "Enter passphrase for key"

# 发送密码
send -- "$password
"

# 等待 SCP 命令执行完成
expect eof
 
相关标签:

发表评论:

评论记录:

未查询到任何数据!
QQ咨询
回电话
回拨通话

我们稍后回拨您的电话

座机请加区号

微信联系
微信扫一扫
微信二维码

扫码加微信