scp时使用expect免密码交互

方法1:
前提需要机器安装expect命令
#! /usr/bin/expect -f 
spawn scp -P 端口  root@x.x.x.x :/x.txt /x.txt

expect {
"(yes/no)?"
{
send  "yes\n"
expect "*password:"  { send  "12345\n" }
}
"*password:"
{
send "12345\n"
}
}
expect eof
方法2:
使用ssh 配置私钥公钥
ssh-keygen   将会生成密钥文件和私钥文件 id_rsa,id_rsa.pub或id_dsa,id_dsa.pub
将pub文件内的内容拷贝到目标主机的 ~/.ssh/authorized_keys 内即可
posted @ 2022-07-27 21:10  feitiandamo  阅读(86)  评论(0)    收藏  举报