全自动化建立SSH信任
#!/bin/sh#远程主机1(源主机)src_host=$1src_user=$2src_passwd=$3#远程主机2(目标主机)dst_host=$4dst_user=$5dst_passwd=$6src_pub=/root/.ssh/id_rsa.pub#在远程主机1上生成公钥Keygen(){expect<<EOFspawnssh$src_user@$src_host"test-f/root/.ssh/id_rsa.pub||echoCheckFalse"expect{"password:"{send"$src_passwd\n"}eof{exit}}expectCheckFalse{exit11}EOFif[$?-ne11];thenecho-n"公钥已经存在,将直接拷贝……"elseecho-n"公钥不存在,开始创建……"expect<<EOFspawnssh$src_user@$src_host"ssh-keygen-trsa"while{1}{expect{"password:"{send"$src_passwd\n"}"yes/no*"{send"yes\n"}"Enterfileinwhichtosavethekey*"{send"\n"}"Enterpassphrase*"{send"\n"}"Entersamepassphraseagain:"{send"\n"}"Overwrite(y/n)"{send"n\n"}eof{exit}}}EOFfi}#从远程主机1上拷贝公钥到远程主机2上Get_pub(){expect<<EOFspawnscp$src_user@$src_host:$src_pub$dst_user@$dst_host:/root/.ssh/$src_hostexpect{"password:"{send"$src_passwd\n";exp_continue}"password:"{send"$dst_passwd\n";exp_continue}"yes/no*"{send"yes\n";exp_continue}eof{exit}}EOF}#在远程主机2上将内容追加到authorized_keysPut_pub(){expect<<EOFspawnssh$dst_user@$dst_host"mkdir-p/root/.ssh;chmod700/root/.ssh;cat/root/.ssh/$src_host>>/root/.ssh/authorized_keys;chmod600/root/.ssh/authorized_keys"expect{"password:"{send"$dst_passwd\n";exp_continue}"yes/no*"{send"yes\n";exp_continue}eof{exit}}EOF}KeygenGet_pubPut_pub
使用格式:
./ssh-turst.sh 192.168.1.1 root 123456 192.168.1.2 root 123456
当然也可以把这些主机IP,用户名,密码写入到一个config.ini文件里
192.168.1.1 root 123456 192.168.1.2 root 123456
192.168.1.1 root 123456 192.168.1.3 root 123456
然后使用xargs命令
xargs -n6 ./ssh-trush.ssh < config.file
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。