OS/macOS

[OS/macOS] remote 서버 접근 ip 저장 script (vmgmt)

bbugge 2021. 6. 18. 12:52

 

# vmgmt
# 원하는 위치에 script를 놓고 사용한다.
SCRIPT_DIR=/usr/local/scripts

COMMAND=$1
HEAD="###########################################\n################# My VM ###################\n###########################################";
TAIL="###########################################";
TXT_FILE="$SCRIPT_DIR/.remote_vmaddr.txt"
SED=/usr/bin/sed
SSH=/usr/bin/ssh

if [ -z $COMMAND ];
then
echo 'ERROR : Please input your command'
else
if [ "$COMMAND" = "help" ];
then
echo 'vmgmt option [param]'
echo 'option : [ init / ls / mk / rm / help ]'
elif [ "$COMMAND" = "init" ];
then
cat /dev/null > $TXT_FILE
elif [ "$COMMAND" =  "ls" ];
then
if [ -z "$2" ]
then
echo "${HEAD}"
cnt=1;
cat ${TXT_FILE} | while read LINE
do
echo "${cnt}\t${LINE}";
cnt=$((cnt+1));
done
echo "${TAIL}"
#else
#IPADDR=`cat $SCRIPT_DIR/remote_vmaddr.txt | grep $2 | awk -F ':' '{print $2}'`
#echo $IPADDR
fi

elif [ "$COMMAND" =  "mk" ];
then
if [ -z "$2" ];
then
echo 'ERROR : Please input vm information.'
else
echo $2 >> $TXT_FILE
fi

elif [ "$COMMAND" =  "rm" ];
then
if [ -z "$2" ];
then
echo 'ERROR : Please input vm number.'
else
#/opt/homebrew/opt/gnu-sed/libexec/gnubin/sed -i "/${2}/d" $TXT_FILE
TXT_CONTENT=`cat $TXT_FILE`
echo "${TXT_CONTENT}" | $SED "${2}d"  > $TXT_FILE
fi

elif [ "$COMMAND" = "con" -o "$COMMAND" = "conn" ];
then
if [ -z "$2" ];
then
echo 'ERROR : Please input vm number.'
else
$SSH `$SED -n "${2}p" $TXT_FILE | awk -F ':' '{print $2}'`
fi

else
echo 'ERROR : Please check your command'
echo 'Command : [ ls / mk / rm / con ]'
fi
fi

vmgmt init : vmgmt명령을 사용하기위해 초기화 한다.

vmgmt ls : 저장한 remote 서버의 ip들을 보여주거나 특정 remote 서버의 ip 를 보여준다.

    => ex) vmgmt ls servername 

vmgmt mk : remote 서버의 ip를 저장한다.

    => ex) vmgmt mk servername:192.168.0.1

vmgmt rm : 저장한 remote 서버의 정보를 삭제한다.

    => ex) vmgmt rm servername