美文网首页
工作必备小脚本

工作必备小脚本

作者: Joening | 来源:发表于2023-06-05 10:33 被阅读0次

    工作中常用小脚本

    #!/usr/bin/bash 
    #author: qiaoning
    
    ######################
    # 1. 脚本$1为 镜像存放路径
    # 2. 脚本$2为 遍历镜像的系统文件 
    #######################
    
    function tag_image () {
    local script_dir=$(dirname $0)
    local old_image_url="hub-pub.jdcloud.com"
    local new_image_url="harbor-lb-service.jd-tpaas.svc.cluster.local"
    local dir=$1
    local image_file=$2
        if [[ ! -d $dir ]];then
            mkdir -p $dir
        fi
        if  [[ ! -f $script_dir/$image_file ]];then
            echo "Usage......"
            exit 
        fi
    cat $image_file |egrep -vi "^#|^$" | while read image
    do
        new_tag_image=$(echo "$image" | sed  -r "s#$old_image_url#$new_image_url#g")
        new_image=$(basename `echo $new_tag_image | tr ':' '-'`)
        
        echo "docker pull $image"
        docker pull $image || exit 
        echo "docker tag $image $new_tag_image"
        docker tag $image $new_tag_image || exit
        echo "docker save $new_tag_image > ${dir}/${new_image}.tar.gz"
        docker save $new_tag_image | gzip > ${dir}/${new_image}.tar.gz  || exit
        sleep 0.5
    done
    }
    
    tag_image $1 $2
    
    
    
    
    cat >> load.sh <<EOF
    #!/usr/bin/bash
    #author: qiaoning
    ls *.tar.gz | while read line 
    do
        image_cul="hub.tpaas.local"
        load_name=`docker load -i $line`
        load_name1=`echo $load_name | awk -F: '{print $2":"$3}'`
        load_name0=`echo $load_name1 | awk -F"/" '{print $1}'`
        load_name2=`echo $load_name1 | sed "s#$load_name0#$image_cul#g"`
        echo "docker tag $load_name1 $load_name2"
         docker tag $load_name1 $load_name2 || exit -1
        echo "docker push $load_name2 ......."
         docker push $load_name2 || exit -1
        echo "Done" 
    done
    EOF
    
    
    cat >> download.sh <<EOF
    #!/usr/bin/bash
    dir=$1
    download_file=$2
    if [[ $# -ne 2 ]];then
        echo "Usage | 1 | 2 |"
        exit -1
    fi
    if [[ ! -d $dir ]];then
        mkdir -pv $dir
    fi
    for items in `cat $download_file`
    do
        if [ ${items:0:1} == "#" ];then
           continue
        else
          echo "wget $items -P $dir"
          wget $items -P $dir || exit 
          echo "Done"
        fi
    done
    EOF
    
    
    #!/usr/bin/env bash
    
    for item in `cat /root/hosts`;do ssh $item "date" ;done
    for item in `cat /root/hosts`;do ssh $item " /usr/sbin/ntpdate  ntp.stack-hebi-2a.jdcloud.local" ;done
    
    
    
    
    #!/usr/bin/bash
    user=admin
    passwd=Harbor12345
    ls *.tgz | while read line ;do curl --fail -u "$user:$passwd" --header 'Content-Type: application/zip' --data-binary "@$line" https://hub.tpaas.local/api/chartrepo/itg/charts --insecure ;done 
    
    
    
    #!/usr/bin/bash
    # $1 主机列表文件
    # $2 目标文件
    # $3 目标分发目录
    if [[ $# -ne 3 ]];then
        echo "Usage: 参数不等于3个....."
        exit -1
    fi
    
    
    for item in `cat $1`
    do
        scp -rp $2 root@$item:$3 
    
    done
    
    
    
    #!/usr/bin/env bash
    
    if [ $# -ne 2 ];then
        echo "Usage | 1 | 2 |"
        exit -1
    fi
    
    for item in `cat $1`
    do
        ssh $item "$2" 
    
    done
    
    
    
    
    #!/usr/bin/bash
    Password="Tpaas@2022"
    rsa_file="/root/.ssh/id_rsa.pub"
    if !  command -v sshpass &> /dev/null ;then 
        echo "No such sshpass command "
        exit -1
    fi
        
    if [ ! -f $rsa_file ];then
        echo "Usage..............."
        exit -1
    fi
    #### 下发秘钥 #### 
    cat $1 | while read line
    do
        echo "Start"
        echo "------------------------ $line is distribution start------------------------------"
        sshpass -p "$Password"  ssh-copy-id  -i $rsa_file  -o StrictHostKeyChecking=no root@$line &> /dev/null
        echo "------------------------ $line is Issued successfully-----------------------------"
        echo "Done"
    done
    
    #!/usr/bin/bash
    #auhtor: qiaoning
    
    cat <<-EOF
    1. master01
    2. master02
    3. master03
    4. node01
    5. node02
    6. node03
    EOF
    
    
    select to_close in 1 2 3 4 5 6 ;
    do
    case $to_close in 
        1)
            echo ssh master01
            ;;
        2)
            echo ssh master02
            ;;
        3)
            echo ssh master03
            ;;
        4)
            echo ssh node01
            ;;
        5)
            echo ssh node02
            ;;
        6)
            echo ssh node03
            ;;
        *)
            echo "Usage: $0 [ 1 | 2 | 3 | 4 | 5 | 6]"
            exit -1
    esac
    done
    
    
    #!/usr/bin/bash 
    #author: qiaoning
    #set -xe
    ######################
    # 1. 脚本$1为 镜像存放路径
    # 2. 脚本$2为 遍历镜像的系统文件 
    #######################
    function format () {
    echo -e "\033[32m $* \033[0m"
    }
    
    function tag_image () {
    local script_dir=$(dirname $0)
    local old_image_url="hub-vpc.jdcloud.com"
    local new_image_url="harbor-lb-service.jd-tpaas.svc.cluster.local"
    local dir=$1
    local DATE=$(date +%F)
    local image_file=$2
        if [[ ! -d $dir ]];then
            mkdir -p $dir
        fi
        if  [[ ! -f $script_dir/$image_file ]];then
            echo "Usage......"
            exit 
        fi
    cat $image_file |egrep -vi "^#|^$" | while read image
    do
        new_tag_image=$(echo "$image" | sed  -r "s#$old_image_url#$new_image_url#g")
        new_image=$(basename `echo $new_tag_image | tr ':' '-'`)
        new_prodect=$( dirname $image | awk -F/ '{print $NF}')
        
           if [ ${image:0:1} == "#" ];then
         continue
          else
        format "docker pull $image"
        docker pull $image || exit 
        format "docker tag $image $new_tag_image"
        docker tag $image $new_tag_image || exit
        format "docker save $new_tag_image > ${dir}${new_prodect}_${new_image}.tar.gz"
        docker save $new_tag_image | gzip > ${dir}${new_prodect}_${new_image}.tar.gz  || exit
        sleep 0.5
          fi
    done
    }
    
    tag_image $1 $2
    
    

    相关文章

      网友评论

          本文标题:工作必备小脚本

          本文链接:https://www.haomeiwen.com/subject/bjwiedtx.html