验证中...
语言: Shell
最后更新于 2017-07-27 18:32
one_key.sh
原始数据 复制代码
#!/bin/bash
comm1="ssh-keygen -t rsa"
comm2="scp /root/.ssh/id_rsa.pub $1@$3:/root/"
comm3="ssh $1@$3"
pass=$2
#define expect function
function exp()
{
expect -c "
spawn $comm1;
expect {
\"*id_rsa):\" {send \"\r\";exp_continue}
}
expect {
\"Overwrite (y/n)?\" {send \"y\r\";exp_continue}
}
expect {
\"Enter passphrase (empty for no passphrase):\" {send \"\r\";exp_continue}
}
expect {
\"Enter same passphrase again:\" {send \"\r\";exp_continue}
}
"
}
function exp_scp()
{
expect -c "
spawn $comm2;
expect {
\"root@*'s password:\" {send \"$pass\r\";exp_continue}
}
"
}
function exp_ssh()
{
expect -c "
spawn $comm3;
expect {
\"root@*'s password:\" {send \"$pass\r\";exp_continue}
\"*#\" {send \"cat /root/id_rsa.pub \>> /root/.ssh/authorized_keys\r\ exit\r\";exp_continue}
}
"
}
#cpuio=`iostat -c |awk 'NR==4 {print$6}'`
#check the rsa file is created or not
if [ $# -lt 2 ];then
echo "Please use the script like the following rule"
echo "usage one_key.sh username password "
exit 1
fi
if [ -f "/root/.ssh/id_rsa" ];
then
echo "The file is exsit,couinue"
else
exp
fi
echo "Now copy the rsa file to the server"
exp_scp >> /dev/null 2>&1
sleep 3 >> /dev/null 2>&1
echo "Now cat the rsa file to the authorized_keys"
exp_ssh >> /dev/null && exit 0

评论列表( 1 )

1227378_cqmn
TIMI 2017-02-27 15:11

表示看不懂,有点注释就好了

你可以在登录后,发表评论

5th_float_left_gitee 5th_float_left_close