您好,登錄后才能下訂單哦!
#! /bin/sh #遠程主機1(源主機) src_host=$1 src_user=$2 src_passwd=$3 #遠程主機2(目標主機) dst_host=$4 dst_user=$5 dst_passwd=$6 src_pub=/root/.ssh/id_rsa.pub #在遠程主機1上生成公鑰 Keygen() { expect << EOF spawn ssh $src_user@$src_host "test -f /root/.ssh/id_rsa.pub || echo CheckFalse " expect { "password:" { send "$src_passwd\n" } eof { exit } } expect CheckFalse { exit 11 } EOF if [ $? -ne 11 ];then echo -n "公鑰已經(jīng)存在,將直接拷貝……" else echo -n "公鑰不存在,開始創(chuàng)建……" expect << EOF spawn ssh $src_user@$src_host "ssh-keygen -t rsa " while {1} { expect { "password:" { send "$src_passwd\n" } "yes/no*" { send "yes\n" } "Enter file in which to save the key*" { send "\n" } "Enter passphrase*" { send "\n" } "Enter same passphrase again:" { send "\n" } "Overwrite (y/n)" { send "n\n" } eof { exit } } } EOF fi } #從遠程主機1上拷貝公鑰到遠程主機2上 Get_pub () { expect << EOF spawn scp $src_user@$src_host:$src_pub $dst_user@$dst_host:/root/.ssh/$src_host expect { "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上將內(nèi)容追加到authorized_keys Put_pub() { expect << EOF spawn ssh $dst_user@$dst_host "mkdir -p /root/.ssh;chmod 700 /root/.ssh;cat /root/.ssh/$src_host >> /root/.ssh/authorized_keys;chmod 600 /root/.ssh/authorized_keys" expect { "password:" { send "$dst_passwd\n";exp_continue } "yes/no*" { send "yes\n";exp_continue } eof{ exit } } EOF } Keygen Get_pub Put_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
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。