Linux scp/ssh命令免密码登陆

**解决的问题:**从服务器A无密码访问服务器Bweb

ssh秘钥

服务器A
服务器B
在A上执行命令 ssh-keygen -t rsa,而后一路回车,最后会在 /root/.ssh/目录下生成 id_rsa 和 id_rsa.pub 文件,将id_rsa.pub 拷贝到服务器B的 /root目录下:
scp ~/.ssh/id_rsa.pub B:/root/
而后执行 经过 cat 命令 把id_rsa.pub追写到服务器B的.ssh目录下authorized_keys 文件中,命令以下:
cat id_rsa.pub >> .ssh/authorized_keys服务器

这时候服务器A就能够无密访问服务器B,反过来,B访问A也同理。ssh

若是执行scp命令的时候,遇到known_hosts错误,去.ssh目录下的known_hosts文件里删除掉你要访问机器的记录。svg

expect

先建立scp_expect.sh文件:spa

#!/usr/bin/expect
set timeout 200
set host [lindex $argv 0]
set username [lindex $argv 1] 
set password [lindex $argv 2] 
set src_file [lindex $argv 3]
set dest_file [lindex $argv 4]
spawn scp -r $username@$host:$src_file $dest_file
expect {
    "(yes/no)?"
    {
        send "yes\n"
        expect "*assword:" { send "$password\n"}
    }
    "*assword:"
    {
        send "$password\n"
    }
}
expect "100%"
expect eof

在脚本中调用scp_expect文件:code

scp_expect.sh ${host} root ${password} ${src_file} ${dest_file}
这样就能够在脚本中直接调用scp命令,而不用被输入密码打断。ssh命令同理。xml