#!/bin/bash
acount_begin=3


function scpbash(){
    acount_text=($(cat ./list.txt))
    server_total_count=$(echo ${#acount_text[*]})
    ((server_total_count=$server_total_count-$acount_begin-1))

    if [[ $1>0 ]]
    then
        # 如果第一个参数有值，则使用它
        ((acount_begin=$acount_begin+$1))
    fi

    echo "=============== Host total:$server_total_count ================="
    menu=""
    for ((n=1;n<=$server_total_count;n++))
    do

        ((i=n+$acount_begin))
        line=($(echo ${acount_text[$i]//,/ }))

        REMOTE_HOSTNAME=${line[0]}
        REMOTE_USER=${line[1]}
        REMOTE_HOST=${line[3]}
        REMOTE_PASS=${line[2]}
        remark=${line[3]}

        if [ -z "$REMOTE_HOST" ]; then
            # 如果第一个参数有值，则使用它
            echo "exit"
            exit 0
        fi

        #ssh-keyscan $REMOTE_HOST >> ~/.ssh/known_hosts

        # 远程执行命令
        echo "$REMOTE_HOST"
        sshpass -p "$REMOTE_PASS" ssh $REMOTE_USER@$REMOTE_HOST "sudo cloud-init clean --logs"
        #sshpass -p "$REMOTE_PASS" ssh $REMOTE_USER@$REMOTE_HOST ps 

    done
}

function vps_list(){
    acount_text=($(cat ./list.txt))
    server_total_count=$(echo ${#acount_text[*]})
    ((server_total_count=$server_total_count-$acount_begin-1))

    if [[ $1>0 ]]
    then
        # 如果第一个参数有值，则使用它
        ((acount_begin=$acount_begin+$1))
    fi

    echo "=============== Host total:$server_total_count ================="
    menu=""
    for ((n=1;n<=$server_total_count;n++))
    do

        ((i=n+$acount_begin))
        line=($(echo ${acount_text[$i]//,/ }))

        REMOTE_USER=${line[0]}
        REMOTE_HOST=${line[2]}
        REMOTE_PASS=${line[1]}
        remark=${line[3]}

        if [ -z "$REMOTE_HOST" ]; then
            # 如果第一个参数有值，则使用它
            echo "exit"
            exit 0
        fi

        # 远程执行命令
        echo "$REMOTE_HOST"
        sshpass -p "$REMOTE_PASS" ssh $REMOTE_USER@$REMOTE_HOST virsh list --all

    done
}

function reset_password(){
    acount_text=($(cat ./list.txt))
    server_total_count=$(echo ${#acount_text[*]})
    ((server_total_count=$server_total_count-$acount_begin-1))

    if [[ $1>0 ]]
    then
        # 如果第一个参数有值，则使用它
        ((acount_begin=$acount_begin+$1))
    fi

    echo "========================================================"
    menu=""
    for ((n=1;n<=$server_total_count;n++))
    do

        ((i=n+$acount_begin))
        line=($(echo ${acount_text[$i]//,/ }))

        REMOTE_HOSTNAME=${line[0]}
        REMOTE_USER=${line[1]}
        REMOTE_HOST=${line[3]}
        REMOTE_PASS=${line[2]}
        remark=${line[3]}

        if [ -z "$REMOTE_HOST" ]; then
            # 如果第一个参数有值，则使用它
            echo "exit"
            exit 0
        fi
        
        echo "主机名：$REMOTE_HOSTNAME"
        echo "主机：$REMOTE_HOST"

        # 使用 ssh-keyscan 获取远程主机的公钥，并将其添加到 known_hosts 文件中
        #ssh-keyscan $REMOTE_HOST >> ~/.ssh/known_hosts

        # 设置新密码
        new_password=$2
        echo "新密码：$new_password"
        # 远程执行命令
        echo "重置密码root..."
        # 使用 echo 和管道重定向输入到 passwd 命令
        sshpass -p "$REMOTE_PASS" ssh $REMOTE_USER@$REMOTE_HOST "echo -e '$new_password\n$new_password' | passwd root"

        echo "重置密码ubuntu..."
        sshpass -p "$new_password" ssh $REMOTE_USER@$REMOTE_HOST "echo -e '$new_password\n$new_password' | passwd ubuntu"

        echo "重置密码完成！"
        echo "---------------------------"

    done
}

function agent_reboot(){
    acount_text=($(cat ./list.txt))
    server_total_count=$(echo ${#acount_text[*]})
    ((server_total_count=$server_total_count-$acount_begin-1))

    if [[ $1>0 ]]
    then
        # 如果第一个参数有值，则使用它
        ((acount_begin=$acount_begin+$1))
    fi

    echo "=============== Host total:$server_total_count ================="
    menu=""
    for ((n=1;n<=$server_total_count;n++))
    do

        ((i=n+$acount_begin))
        line=($(echo ${acount_text[$i]//,/ }))

        REMOTE_USER=${line[0]}
        REMOTE_HOST=${line[2]}
        REMOTE_PASS=${line[1]}
        remark=${line[3]}

        if [ -z "$REMOTE_HOST" ]; then
            # 如果第一个参数有值，则使用它
            echo "exit"
            exit 0
        fi

        # 远程执行命令
        echo "杀死同名进程..."
        sshpass -p "$REMOTE_PASS" ssh $REMOTE_USER@$REMOTE_HOST pkill $(basename $BINARY_FILE)

        echo "启动新的程序..."
        sshpass -p "$REMOTE_PASS" ssh $REMOTE_USER@$REMOTE_HOST $BINARY_FILE --daemon
    done
}

function send(){

    acount_text=($(cat ./remote_list.txt))
    server_total_count=$(echo ${#acount_text[*]})
    ((server_total_count=$server_total_count-$acount_begin-1))

    if [[ $1>0 ]]
    then
        # 如果第一个参数有值，则使用它
        ((acount_begin=$acount_begin+$1))
    fi

    read -p "Pls input files name:" files_name
    read -p "Pls input remote path:" remote_path
    
    echo "=============== Host total:$server_total_count ================="
    menu=""
    for ((n=1;n<=$server_total_count;n++))
    do

        ((i=n+$acount_begin))
        line=($(echo ${acount_text[$i]//,/ }))

        REMOTE_USER=${line[1]}
        REMOTE_HOST=${line[3]}
        REMOTE_PASS=${line[2]}
        

        if [ -z "$REMOTE_HOST" ]; then
            # 如果第一个参数有值，则使用它
            echo "exit"
            exit 0
        fi

        # 要传输的二进制可执行文件
        BINARY_FILE=$files_name

        # 传输二进制文件到远程服务器
        echo "传输文件到远程服务器... $REMOTE_HOST"
        sshpass -p "$REMOTE_PASS" scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null $BINARY_FILE $REMOTE_USER@$REMOTE_HOST:$remote_path

    done


}

send