ssh 免密码登录,脚本

ssh 免密码登录,需要使用公私钥来认证@import url(http://www.blogjava.net/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);

首先通过 ssh-keygen 生成一对公私钥,默认在 ~/.ssh/id_rsa.pub 和 ~/.ssh/id_rsa,前面的是公钥。
认证就是要把这个文件加到远程用户目录 ~/.ssh/authorized_keys 文件中,而且这个文件的权限不能被其它人访问。
下面的脚本能够自动把这个文件放到远程,方法是 ./addpk ip username password
#!//usr/bin/expect
set host [lrange $argv 0 0]
set user [lrange $argv 1 1]
set passwd [lrange $argv 2 2]
proc ssh {cmd} {
    global user host passwd
    spawn ssh $user@$host “$cmd”
    expect {
        “*conne*” {
            send “yes\n”
            expect “*password*”
            send “$passwd\n”
            expect eof
        }
        “*password*” {
            send “$passwd\n”
            expect eof
    }
    }
}
proc scp {src dest} {
    global user host passwd
    spawn scp $src $user@$host:$dest
    expect {
        “*conne*” {
            send “yes\n”
            expect “*password*”
            send “$passwd\n”
            expect eof
        }
        “*password*” {
            send “$passwd\n”
            expect eof
        }
    }
}
ssh “mkdir -p ~/.ssh”
scp “/home/user1/.ssh/id_rsa.pub” “~/.ssh/id_rsa.pub”
ssh “cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys”
ssh “uniq ~/.ssh/authorized_keys > ~/.ssh/authorized_keys.tmp”
ssh “mv ~/.ssh/authorized_keys.tmp ~/.ssh/authorized_keys”
ssh “chmod 700 ~/.ssh”
ssh “chmod 600 ~/.ssh/*”

ssh 免密码登录,脚本》上有1条评论

  1. Eliza

    Now I see what you mean. Yes, I have other needs that are met by my practice, but thy&7#821e;re of a different order. I know you and I have different views about the psychotherapy relationship, but for me, my primary need is to earn a living, while the primary need of my clients is of an emotional nature. I don’t look to my clients to gratify my emotional needs, even if I do derive a lot of satisfaction as part of the work.

    回复

发表评论

电子邮件地址不会被公开。