One shell

#!/bin/bash
#auth:codewalker 
#mail:simon.xie@codewalker.me

#do remote script ssh $host "ret=0" 2>&1

IS_admin_MODE=false
C_MODE_FUNCTION_LISTS='[rwho][rusers][rhost][connect][rsu][rpasswd][rfinger][rwrite]'
A_MODE_FUNCTION_LISTS='[rhost][ausers][afinger][rlistusers]'
PING_COUNT=1
PING_TIMEOUT=1
HOST_LIST_FILE='/tmp/host_list'
USER_LIST_FILE='/tmp/user_list'
START_TIME=$(date +%s)

function help(){
cat <> " is_set_ip_range
    if [ $is_set_ip_range = 'y' ]; then 
        echo "Please type a range like: xxx.xxx.xxx"
        read -p ">> " local_ip_format
        echo "Now, scanning..."
    else 
        #ONLY for centos. 
        local_ip_format=$(get_local_IP)
        echo "choosed N, use default:$local_ip_format"".x"
    fi
    echo 'this may take a few minutes, please wait...'
    echo 'Hosts:'
    for (( i = 2; i < 256; i++)); do 
    #find host.
        ping -c$PING_COUNT -w$PING_TIMEOUT $local_ip_format'.'$i|grep "bytes from"|awk -F: '{print $1}'|awk -F'from' '{print $2}' |tee -a $HOST_LIST_FILE
    done
    count_host
    unset local_ip_format
}

function count_host() {
    #to count how many lines in this file.
    count=$(wc -l $HOST_LIST_FILE|awk -F" " '{print $1}')
    echo "there are $count hosts."
    unset count
}

function rhost() {
    #OPTION 1
    #notes that the IP address is NOT same as mien.
    #echo $local_ip_format{1..254}|xargs -n1 -P0 ping -c1|grep "bytes from"
    #################################
    #OPTION 2
    if [ -f /tmp/host_list ]; then
        echo '[host_list] EXIST.'
        echo "Do you need rescan host?[y/n] " 
        read -p ">> " is_rescan
        if [ $is_rescan = 'y' ]; then
            rm -fv $HOST_LIST_FILE
            time scan_HOST #how much time for scan host.
        else
            cat $HOST_LIST_FILE
            count_host
        fi
    else
        echo "Do you need scan host?[y/n] "
        read -p ">> " is_scan
        if [ $is_scan = "y" ]; then
            rm -fv $HOST_LIST_FILE
            time scan_HOST
        else
            if [ -f /tmp/host_list ]; then
                cat $HOST_LIST_FILE
                count_host
            else
                echo "$HOST_LIST_FILE NO FOUND!"
            fi
        fi

    fi

}

function connect() {
    #connect to host by [ssh].
    echo 'Type a ip address to login: ' 
    read -p '>>' t_ip
    #check host
    if [ $(grep -c $t_ip /tmp/host_list) -eq 0 ];then
        echo "$t_ip not boot..."
        do_exit
    fi
    #check user
    echo 'Type a username: ' 
    read -p '>>' t_username
    if [ $(grep -c $t_username /tmp/user_list) -eq 0 ]; then
        echo "Unauthorized. $t_username CANNOT login..."
        do_exit
    fi  
    ssh  $t_username@$t_ip
    unset t_username
    unset t_ip
}

function rsu() {
    su
}

function rpasswd() {
    #set password.
    echo 'Type a username to change password for: '
    read -p '>>' t_username
    passwd $t_username
    unset t_username
}

function rfinger() {
    #get information
    echo 'aaa'
    echo 'Type a username to displays information about: '
    read -p '>>' t_username
    finger $t_username
    unset t_username
}

function rwrite() {
    #send message for other user(s) in same host.
    echo 'Type some words for other users.'
    read -p '>>' t_message
    wall $t_message
    unset t_message
}

function rlistusers() {
    if [ -f /tmp/user_list ]; then
        awk -F: '{print $2}' $USER_LIST_FILE
    else
        echo "$USER_LIST_FILE NO FOUND, do you wanna add user?[y]"
        read -p ">> " is_add_user
        if [ $is_add_user = 'y' ];then
            ausers
        else
            echo 'choosed no, exit...'
            do_exit
        fi
    fi
}

function ausers() {
    if [ -f /tmp/user_list ]; then
        echo "FOUND $USER_LIST_FILE."
        awk -F: '{print $2}' $USER_LIST_FILE
        echo "Do you rewrite it?[y]"
        read -p ">> " is_rewrite_user_list
        if [ $is_rewrite_user_list = 'y' ]; then
            rm -fv $is_rewrite_user_list
        fi
    fi
    echo "input host IP which you need to add (field-separator: >>,<< )"
    read -p ">> " t_hosts
    IFS=',' read -a t_arr_hosts <<< "${t_hosts}"
    echo "you will add [${#t_arr_hosts[@]}] hosts"
    for host in "${t_arr_hosts[@]}" "${!array[@]}"
    do
        #echo "input this remote host[$host] login username."
        #read -p ">> " r_username
        #echo "input remote host[$host] login password."
        #read -p ">> " r_password
        #echo "can you longin[$host] without password?[y] "
        #read -p ">> " without_psd
        #if [ $without_psd = 'y' ]; then
            echo "add user on $host." 
            echo "input usernames (add more than one,field-separator: >>,<< )"
            read -p ">> " t_users
            IFS=',' read -a t_arr_username <<< "${t_users}"
            echo "you will add [${#t_arr_username[@]}] users on $host"
            for username in "${t_arr_username[@]}"
            do
                echo "set password for [$username] on $host"
                read -p ">> " t_pwd
                #echo "###$t_pwd for $username on $r_username@$host:$r_password [DONE]###"
                echo "###$t_pwd for $username @$host [DONE]###"
                echo 
                echo "$host:$username:$t_pwd" >> $USER_LIST_FILE
           done
        #else
        #   echo "can't longin remote host."
        #fi
    done
    echo "add all user [DONE]"
    unset t_hosts
    unset t_arr_hosts
    unset host
    unset t_users
    unset username
}


#connect mode
function CONNECT_MODE() {
    while true;do
        echo 'select a function[*exit* to exit,*help* for list functions]:' 
        read -p ">>" F
        case $F in
            rwho)
                rwho
                ;;
            rusers)
                rusers 
                ;;
            rhost)
                rhost
                ;;
            connect)
                connect
                ;;
            rsu)
                su
                ;;
            rpasswd)
                rpasswd
                ;;
            rfinger)
                rfinger
                ;;
            rwrite)
                rwrite
                ;;
            help)
                C_HELP
                ;;
            exit)
                do_exit
                exit 1
                ;;
            *)
                echo '!!!unknow argument!!!'
                echo $C_MODE_FUNCTION_LISTS
        esac
    done
}

#admin mode
function ADMIN_MODE() {
    while true;do
        echo 'select a function[*exit* to exit,*help* for list functions]:' 
        read -p ">>" F
        case $F in
            ausers)
                ausers 
                ;;
            rhost)
                rhost
                ;;
            afinger)
                rfinger
                ;;
            rwho)
                rwho
                ;;
            rusers)
                rusers 
                ;;
            connect)
                connect
                ;;
            rsu)
                su
                ;;
            rpasswd)
                rpasswd
                ;;
            rwrite)
                rwrite
                ;;
            rlistusers)
                rlistusers
                ;;
            help)
                A_HELP
                ;;
            exit)
                do_exit
                exit 1
                ;;
            *)
                echo '!!!unknow argument!!!'
                echo $A_MODE_FUNCTION_LISTS
        esac
    done
}
####main#####
if [ $# -eq 0 ]; then
    echo 'no argument '
    echo 'Please select a mode to using.'
    echo 'c for [connect]. a for[admin]'
    read -p '>>' MODE
    case $MODE in
        c)
            echo "Using connect mode"
            IS_admin_MODE=false
            echo $C_MODE_FUNCTION_LISTS
            CONNECT_MODE
            ;;
        a)
            echo "Using admin mode"
            IS_admin_MODE=true
            echo $A_MODE_FUNCTION_LISTS
            ADMIN_MODE
            ;;
        ?)
            echo "unkonw mode"
            help
            exit 1
            ;;
    esac
    do_exit
    exit 1
fi

while getopts "ac" arg
do
    case $arg in
        c)
            echo "Using connect mode"
            IS_admin_MODE=false
            echo $C_MODE_FUNCTION_LISTS
            CONNECT_MODE
            ;;
        a)
            echo "Using admin mode"
            IS_admin_MODE=true
            echo $A_MODE_FUNCTION_LISTS
            ADMIN_MODE
            ;;
        ?)
            echo "unkonw argument"
            help
            exit 1
            ;;
     esac
done

发表评论

电子邮件地址不会被公开。 必填项已用*标注