• 本帖最后由 摸鱼小熊猫 于 2018-3-2 01:30 编辑 *

先上脚本源码:

  • # usage: xiaoji number?
  • # for example: xiaoji 30 # --> create 30 containers
  • # script status: pass
  • # platform: virmach vps with 768M memory
  • # need docker env and make sure kernel version >3.10
  • # default OS debian
  • - - if [ $# != 1 ];then
  • echo -ne "need 1 parameters!!!\nusage: $0 container_numbers\n"
  • exit ;
  • fi
  • - apt-get update -y || yum update -y
  • apt-get install -y ansible sshpass || yum install -y ansible sshpass
  • rm /root/xiaoji /etc/ansible/host
  • docker rm -f docker ps -aq
  • numbers=$(($1+1))
  • - for i in seq 2 $numbers
  • do
  • echo -ne "172.17.0.$i\n" >> /etc/ansible/host
  • done
  • - #expressions=("这里替换成自己的镜像" "镜像2" "镜像3") #生成的小鸡,系统会从这里面随机选择一个
  • expressions=("rastasheep/ubuntu-sshd") #如果只写一个类似这样,那所有的小鸡都是ubuntu系统,建议用自己定制的docker镜像
  • for i in seq 2 $numbers
  • do
  • #docker run -d -m 1024M --cpuset-cpus=0-2 --cpu-shares=10 --name ssh_$i -p $((1000+$i)):22 $2 #限制小鸡cpu和内存
  • image_name=${expressions[$RANDOM % ${#expressions[@]} ]}
  • echo echo $image_name | cut -d \/ -f 2 | cut -d \- -f 2 >> os
  • docker run -itd -m 512M --restart=always --name ssh_$i -p $((1000+$i)):22 $image_name
  • done
  • - rm -f /root/.ssh/known_hosts
  • - #ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa #如果还没有生成密钥对,取消前面的注释,否则后续必定失败
  • sed -i '/StrictHostKeyChecking/s/^#//; /StrictHostKeyChecking/s/ask/no/' /etc/ssh/ssh_config
  • - while read ip
  • do
  • sshpass -p root ssh-copy-id $ip
  • done /etc/ansible/hosts
  • cat /etc/ansible/host >> /etc/ansible/hosts
  • - while read container_ip
  • do
  • random_passwd=echo $RANDOM | md5sum | cut -c 1-18
  • ansible $container_ip -m shell -a "echo root:$random_passwd | chpasswd"
  • container_last_ip=echo $container_ip | cut -d \. -f 4
  • host_ip=hostname -I | cut -d " " -f 1
  • echo -ne "ssh_$container_last_ip\t$container_host_ip\t$((1000+$container_last_ip))\tssh -p $((1000+$container_last_ip)) $host_ip\t$random_passwd\n" >> tmp
  • done /root/xiaoji && rm tmp os复制代码

因为要记录生成的随机密码,所以单线程生成小鸡比较慢,要是不记录密码生成小鸡的速度可以提高很多
要是大佬们有更好的方式,欢迎pm我
效果图:

59487

59488

图片内容:

  • ssh_2 1002 ssh -p 1002 107.174.221.14 17339ff82b1dec1788 debian
  • ssh_3 1003 ssh -p 1003 107.174.221.14 a002bd37a510e33805 debian
  • ssh_4 1004 ssh -p 1004 107.174.221.14 3c5b43257fdce6942b centos
  • ssh_5 1005 ssh -p 1005 107.174.221.14 9b6ab5e6833f57f95b debian
  • ssh_6 1006 ssh -p 1006 107.174.221.14 438799f9ff489ac0a0 centos
  • ssh_7 1007 ssh -p 1007 107.174.221.14 6e904934bcfeaf647e centos
  • ssh_8 1008 ssh -p 1008 107.174.221.14 1e36f9732d20448175 centos
  • ssh_9 1009 ssh -p 1009 107.174.221.14 30050b34defe2bbedc debian
  • ssh_10 1010 ssh -p 1010 107.174.221.14 61f30dd07c86168ec0 debian
  • ssh_11 1011 ssh -p 1011 107.174.221.14 a781b4a21419abfdf5 centos
  • ssh_12 1012 ssh -p 1012 107.174.221.14 1c9c7908b3f2068959 debian
  • ssh_13 1013 ssh -p 1013 107.174.221.14 a9220d70fbdb754e57 debian
  • ssh_14 1014 ssh -p 1014 107.174.221.14 5b0d816b3607dbeb42 debian
  • ssh_15 1015 ssh -p 1015 107.174.221.14 c9b3b21479862fee90 centos
  • ssh_16 1016 ssh -p 1016 107.174.221.14 dc61cbe19b2f277c7b centos
  • ssh_17 1017 ssh -p 1017 107.174.221.14 35a37d1213cd4aa960 centos
  • ssh_18 1018 ssh -p 1018 107.174.221.14 59ec818a0749559a7f centos
  • ssh_19 1019 ssh -p 1019 107.174.221.14 f42cf99bdddff43a00 debian
  • ssh_20 1020 ssh -p 1020 107.174.221.14 e4519e55181d1b3d61 debian
  • ssh_21 1021 ssh -p 1021 107.174.221.14 d6a043822b7916c1b7 debian
  • ssh_22 1022 ssh -p 1022 107.174.221.14 dcece22318d4b14dce debian
  • ssh_23 1023 ssh -p 1023 107.174.221.14 2635b422c8b660e6a1 debian
  • ssh_24 1024 ssh -p 1024 107.174.221.14 93342c7ee7a7f79a37 debian
  • ssh_25 1025 ssh -p 1025 107.174.221.14 6fe8c72538d8ffbf36 centos
  • ssh_26 1026 ssh -p 1026 107.174.221.14 80c1fd5ca734cf5efe centos
  • ssh_27 1027 ssh -p 1027 107.174.221.14 a9db208e9cfeac0d60 centos
  • ssh_28 1028 ssh -p 1028 107.174.221.14 9d87fa85a46bf4e170 centos
  • ssh_29 1029 ssh -p 1029 107.174.221.14 2418d6f567e75cbd15 debian
  • ssh_30 1030 ssh -p 1030 107.174.221.14 203463b81ef856ae54 centos
  • ssh_31 1031 ssh -p 1031 107.174.221.14 4c2bdf2903710c0c29 centos复制代码

在virmach vps 做的实验,内存700多M,大佬们轻点蹂躏