From 38f66523019938693da6456f56c29f1c9f233ac3 Mon Sep 17 00:00:00 2001 From: TsMask <340112800@qq.com> Date: Mon, 21 Apr 2025 16:09:21 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0OMC=E6=9C=8D=E5=8A=A1?= =?UTF-8?q?=E6=8E=A7=E5=88=B6=E8=84=9A=E6=9C=AC=E5=B9=B6=E7=A7=BB=E9=99=A4?= =?UTF-8?q?=E8=BF=87=E6=97=B6=E7=9A=84=E8=84=9A=E6=9C=AC=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- linux/usr/local/bin/omcd | 44 +++++ linux/usr/local/etc/omc/script/cpnetools.sh | 38 ---- linux/usr/local/etc/omc/script/cpsshkey.sh | 36 ---- linux/usr/local/etc/omc/script/importdb.sh | 161 ----------------- linux/usr/local/etc/omc/script/init_ne.sh | 177 +++++++++++++++++++ linux/usr/local/etc/omc/script/link_ne.sh | 100 ----------- linux/usr/local/etc/omc/script/nehosts | 2 - linux/usr/local/etc/omc/script/rmexpfiles.sh | 16 -- linux/usr/local/etc/omc/script/setomc.sh | 112 ------------ linux/usr/local/etc/omc/script/ssh_keys.sh | 41 +++++ linux/usr/local/etc/omc/script/sshcpid.sh | 23 --- 11 files changed, 262 insertions(+), 488 deletions(-) create mode 100644 linux/usr/local/bin/omcd delete mode 100644 linux/usr/local/etc/omc/script/cpnetools.sh delete mode 100644 linux/usr/local/etc/omc/script/cpsshkey.sh delete mode 100644 linux/usr/local/etc/omc/script/importdb.sh create mode 100644 linux/usr/local/etc/omc/script/init_ne.sh delete mode 100644 linux/usr/local/etc/omc/script/link_ne.sh delete mode 100644 linux/usr/local/etc/omc/script/nehosts delete mode 100644 linux/usr/local/etc/omc/script/rmexpfiles.sh delete mode 100644 linux/usr/local/etc/omc/script/setomc.sh create mode 100644 linux/usr/local/etc/omc/script/ssh_keys.sh delete mode 100644 linux/usr/local/etc/omc/script/sshcpid.sh diff --git a/linux/usr/local/bin/omcd b/linux/usr/local/bin/omcd new file mode 100644 index 0000000..90d9408 --- /dev/null +++ b/linux/usr/local/bin/omcd @@ -0,0 +1,44 @@ +#!/bin/bash + +ServiceList="omc sshsvc" + +case "$1" in + start) + for v in $ServiceList;do + systemctl start $v + if [ $? = 0 ]; then + echo "starting $v process done" + fi + sleep 1 + done + ;; + status) + for v in $ServiceList;do + systemctl status $v + done + ;; + stop) + for v in $ServiceList;do + systemctl stop $procName + if [ $? = 0 ]; then + echo "Stopping $v process done" + fi + done + ;; + restart) + $0 stop + sleep 1 + $0 start + ;; + version) + local BinDir=/usr/local/bin + for v in $ServiceList;do + $BinDir/$v --version + done + ;; + *) + echo "OMC Service Control" + echo "Usage: $0 start|status|stop|restart|version" + echo "" + ;; +esac diff --git a/linux/usr/local/etc/omc/script/cpnetools.sh b/linux/usr/local/etc/omc/script/cpnetools.sh deleted file mode 100644 index ce2249e..0000000 --- a/linux/usr/local/etc/omc/script/cpnetools.sh +++ /dev/null @@ -1,38 +0,0 @@ -#!/bin/bash - -tooldir=/usr/local/bin -toollist="zip unzip" - -# distribute to hosts in file nehosts -while read line - do - if [[ "$line" =~ ^[^[:space:]]*# || -z "$line" ]]; then - continue - fi - user=`echo $line | cut -d " " -f 2` - ip=`echo $line | cut -d " " -f 1` - passwd=`echo $line | cut -d " " -f 3` - - for toolname in $toollist;do - expect < "$output_file" + + +# ======================= generate sql +# ne_info +# generate_ne_info 1 OMC 001 127.0.0.1 33030 1,2 +generate_ne_info() { + local row_id=$1 + local ne_type=$2 + local ne_id=$3 + local ip=$4 + local port=$5 + local ne_host=$6 + local ne_name="${ne_type}_${ne_id}" + local ne_rmuid="4400HX${ne_type}${ne_id}" + + echo "INSERT INTO ne_info VALUES (${row_id}, '${ne_type}', '${ne_id}', '${ne_rmuid}', '${ne_name}', '${ip}', ${port}, 'PNF', '-', '-', '-', '-', '${ne_host}', 1, '', '', 0, 'script', $(date +%s%3N));" +} + +# ne_host +# generate_ne_host 1 OMC 001 127.0.0.1 +generate_ne_host() { + local row_id=$1 + local ne_type=$2 + local ne_id=$3 + local ip=$4 + local ne_name="${ne_type}_${ne_id}" + local start_id=$row_id + local ssh_type="2" + local ssh_password="" + if [ -n "$DEFAULT_SSH_PORT" ]; then + ssh_type="0" + ssh_password="$DEFAULT_SSH_PWD" + fi + + # SSH连接 + echo "INSERT INTO ne_host VALUES (${start_id}, 'ssh', '1', '${ne_name}_${DEFAULT_SSH_PORT}', '${ip}', ${DEFAULT_SSH_PORT}, '${DEFAULT_SSH_USER}', '${ssh_type}', '${ssh_password}', '', '', '', '', 'script', $(date +%s%3N), 'script', $(date +%s%3N));" + ((start_id++)) + + # Telnet连接 + echo "INSERT INTO ne_host VALUES (${start_id}, 'telnet', '1', '${ne_name}_${DEFAULT_TELNET_PORT}', '${ip}', ${DEFAULT_TELNET_PORT}, 'admin', '0', 'okzArqkFYlnVVdQYkNiqmiW+CGQrxg1AFmroR1bw1Kg=', '', '', '', '', 'script', $(date +%s%3N), 'script', $(date +%s%3N));" + ((start_id++)) + + # 特殊网元类型处理 + case "$ne_type" in + "UDM") + echo "INSERT INTO ne_host VALUES (${start_id}, 'redis', '1', '${ne_name}_${DEFAULT_REDIS_PORT}', '${ip}', ${DEFAULT_REDIS_PORT}, 'udmdb', '0', 'jW965gWZK0mw6EtYfihMV9QqHjqa03vJ8VlVii9non8=', '', '', '0', '', 'script', $(date +%s%3N), 'script', $(date +%s%3N));" + ;; + "UPF") + echo "INSERT INTO ne_host VALUES (${start_id}, 'telnet', '1', '${ne_name}_${DEFAULT_TELNET_PORT2}', '${ip}', ${DEFAULT_TELNET_PORT2}, 'admin', '0', '', '', '', '', '', 'script', $(date +%s%3N), 'script', $(date +%s%3N));" + ;; + esac +} +# generate_ne_host_ids OMC 1 +generate_ne_host_ids() { + local ne_type=$1 + local start_id=$2 + local ids="${start_id},$((start_id+1))" + + case "$ne_type" in + "UDM") + ids="${ids},$((start_id+2))" + ;; + "UPF") + ids="${ids},$((start_id+2))" + ;; + esac + echo "$ids" +} + +# ne_version +# generate_ne_version 1 OMC 001 +generate_ne_version() { + local row_id=$1 + local ne_type=$2 + local ne_id=$3 + + echo "INSERT INTO ne_version VALUES (${row_id}, '${ne_type}', '${ne_id}', '-', '-', '-', '', '', '', '', '', '', '0', 'script', $(date +%s%3N), 'script', $(date +%s%3N));" +} + +# ne_license +# generate_ne_license 1 OMC 001 +generate_ne_license() { + local row_id=$1 + local ne_type=$2 + local ne_id=$3 + + echo "INSERT INTO ne_license VALUES (${row_id}, '${ne_type}', '${ne_id}', '', '', '', '', '1', '', 'script', $(date +%s%3N), 'script', $(date +%s%3N));" +} + + +# ======================= + + +# Execute output sql to file +execute_output() { + local row_id=1 + local host_id=1 + + # empty the table + echo "DELETE FROM ne_host;" >> "$output_file" + echo "DELETE FROM ne_info;" >> "$output_file" + echo "DELETE FROM ne_version;" >> "$output_file" + echo "DELETE FROM ne_license;" >> "$output_file" + echo "" >> "$output_file" + + for v in "${NE_LIST[@]}"; do + IFS=',' read -r ne_type ne_ip ne_port <<< "$v" + echo "===> ${ne_type} ${ne_ip} ${ne_port}" + + generate_ne_host "$host_id" "$ne_type" "$DEFAULT_NE_ID" "$ne_ip" >> "$output_file" + local host_ids=$(generate_ne_host_ids $ne_type $host_id) + generate_ne_info "$row_id" "$ne_type" "$DEFAULT_NE_ID" "$ne_ip" "$ne_port" "$host_ids" >> "$output_file" + generate_ne_version "$row_id" "$ne_type" "$DEFAULT_NE_ID" >> "$output_file" + generate_ne_license "$row_id" "$ne_type" "$DEFAULT_NE_ID" >> "$output_file" + + echo "" >> "$output_file" + + ((row_id++)) + case "$ne_type" in + "UDM"|"UPF") + host_id=$((host_id+3)) + ;; + *) + host_id=$((host_id+2)) + ;; + esac + done +} +execute_output + +# Execute import sql to omc +execute_import() { + local OMCBinFile=/usr/local/bin/omc + local OMCEtcDir=/usr/local/etc/omc + local confFile=$OMCEtcDir/omc.yaml + source $OMCEtcDir/omc.conf + $OMCBinFile -c $confFile --sqlPath $output_file --sqlSource $MODE +} +execute_import + +# Remove the output file +rm -rf $output_file diff --git a/linux/usr/local/etc/omc/script/link_ne.sh b/linux/usr/local/etc/omc/script/link_ne.sh deleted file mode 100644 index fb8e848..0000000 --- a/linux/usr/local/etc/omc/script/link_ne.sh +++ /dev/null @@ -1,100 +0,0 @@ -#!/bin/bash -set -x - -OMCBinFile=/usr/local/bin/omc -OMCEtcDir=/usr/local/etc/omc -confFile=$OMCEtcDir/omc.yaml -VariableFile=$OMCEtcDir/omc.conf -# Read the value of the variable from file -source $VariableFile - -# ========================= ne_host -ne_host_id=0 -# 调用函数生成并输出SQL语句 -# ne_host "$ne_type" "$ne_ip" "$ne_port" "$ssh_ip" "$ssh_port" -ne_host() { - local ne_type=$1 - local ne_ip=$2 - local ne_port=$3 - - local ssh_ip=$4 - local ssh_port=$5 - local ssh_user="omcuser" - local ssh_type="2" # 0passwd 1sshkey 2notpwd - local ssh_passwd="" - - local telnet_port=4100 - local telnet_user="admin" - local telnet_password='NUBonCin4GZgl7o12YjeClE8ToQmYp9KWdhMjSNxc2M=' - - local timestamp=$(date +%s%3N) - - # 根据ne_type生成对应的ne_name - case $ne_type in - "UDM") - let ne_host_id++ - echo "INSERT INTO "ne_host" VALUES ($ne_host_id, 'ssh', '1', '$ne_type_S_$timestamp', '$ssh_ip', $ssh_port, '$ssh_user', '$ssh_type', '$ssh_passwd', '', '', '', '', 'system', $timestamp, '', 0);" >> "$output_file" - local host_ids="${ne_host_id}" - let ne_host_id++ - echo "INSERT INTO "ne_host" VALUES ($ne_host_id, 'telnet', '1', '$ne_type_T_$timestamp', '$ne_ip', $telnet_port, '$telnet_user', '0', '$telnet_password', '', '', '', '', 'system', $timestamp, '', 0);" >> "$output_file" - local host_ids="${host_ids},${ne_host_id}" - let ne_host_id++ - echo "INSERT INTO "ne_host" VALUES ($ne_host_id, 'redis', '1', '$ne_type_R_$timestamp', '$ne_ip', 6379, 'udmdb', '0', 'nO3fEhtuKuBkQE5ozsUhNfzn02vhnyxYTEiPn2CIlr4=', '', '', '0', '', 'system', $timestamp, '', 0);" >> "$output_file" - local host_ids="${host_ids},${ne_host_id}" - echo "${host_ids}" - ;; - "UPF") - let ne_host_id++ - echo "INSERT INTO "ne_host" VALUES ($ne_host_id, 'ssh', '1', '$ne_type_S_$timestamp', '$ssh_ip', $ssh_port, '$ssh_user', '$ssh_type', '$ssh_passwd', '', '', '', '', 'system', $timestamp, '', 0);" >> "$output_file" - local host_ids="${ne_host_id}" - ne_host_id=$((ne_host_id + 1)) - echo "INSERT INTO "ne_host" VALUES ($ne_host_id, 'telnet', '1', '$ne_type_T_$timestamp', '$ne_ip', $telnet_port, '$telnet_user', '0', '$telnet_password', '', '', '', '', 'system', $timestamp, '', 0);" >> "$output_file" - local host_ids="${host_ids},${ne_host_id}" - let ne_host_id++ - echo "INSERT INTO "ne_host" VALUES ($ne_host_id, 'telnet', '1', '$ne_type_TT_$timestamp', '$ne_ip', 5002, 'admin', '0', '', '', '', '0', '', 'system', $timestamp, '', 0);" >> "$output_file" - local host_ids="${host_ids},${ne_host_id}" - echo "${host_ids}" - ;; - *) - let ne_host_id++ - echo "INSERT INTO "ne_host" VALUES ($ne_host_id, 'ssh', '1', '${ne_type}_S_${timestamp}', '$ssh_ip', $ssh_port, '$ssh_user', '$ssh_type', '$ssh_passwd', '', '', '', '', 'system', $timestamp, '', 0);" >> "$output_file" - local host_ids="${ne_host_id}" - let ne_host_id++ - echo "INSERT INTO "ne_host" VALUES ($ne_host_id, 'telnet', '1', '${ne_type}_S_${timestamp}', '$ne_ip', $telnet_port, '$telnet_user', '0', '$telnet_password', '', '', '', '', 'system', $timestamp, '', 0);" >> "$output_file" - local host_ids="${host_ids},${ne_host_id}" - echo "${host_ids}" - ;; - esac -} - -# ========================= -# 输出的文件路径 -output_file="output.sql" -# 清空文件内容(如果文件已存在) -> "$output_file" - -# 定义ne_types数组 -# ne_type ne_ip ne_port ssh_ip ssh_port -ne_types=( - "OMC,172.16.5.110,33030,127.0.0.1,22" - "IMS,172.16.5.110,33030,127.0.0.1,22" - "AMF,172.16.5.110,33030,127.0.0.1,22" -) - -# 循环处理ne_types数组 -for v in "${ne_types[@]}"; do - # 解析每一项 - IFS=',' read -r ne_type ne_ip ne_port ssh_ip ssh_port <<< "$v" - - host_ids=$(ne_host "$ne_type" "$ne_ip" "$ne_port" "$ssh_ip" "$ssh_port") - ne_host_id=$(echo "$host_ids" | awk -F',' '{print $NF}') # 提取最后一个值并赋值给ne_host_id - echo "===> ${host_ids}" - -done - -echo "SQL语句已写入 $output_file" - -# ========================= -#$OMCBinFile -c $confFile --sqlPath $OMCEtcDir/database/$MODE/$T_PARAM --sqlSource $MODE - -# bash link_ne.sh diff --git a/linux/usr/local/etc/omc/script/nehosts b/linux/usr/local/etc/omc/script/nehosts deleted file mode 100644 index 96f1903..0000000 --- a/linux/usr/local/etc/omc/script/nehosts +++ /dev/null @@ -1,2 +0,0 @@ -# host user password -# Example: 172.16.5.100 omcuser password diff --git a/linux/usr/local/etc/omc/script/rmexpfiles.sh b/linux/usr/local/etc/omc/script/rmexpfiles.sh deleted file mode 100644 index 881f44f..0000000 --- a/linux/usr/local/etc/omc/script/rmexpfiles.sh +++ /dev/null @@ -1,16 +0,0 @@ -#!/bin/bash - -# rm expired file with filename like *20231028111213.zip" - -filepath=$1 -duration=$2 - -find $filepath -maxdepth 1 -type f -name "*[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]*" -printf "%f\n" | while read filename; do - datestr=$(echo "$filename" | grep -oE '[0-9]{8}') - filedate=$(date -d "$datestr" +%s) - sevendaysago=$(date -d "$duration days ago" +%s) - if [ "$filedate" -lt "$sevendaysago" ]; then - rm -f "$filepath/$filename" - echo "rm file: $filename" - fi -done \ No newline at end of file diff --git a/linux/usr/local/etc/omc/script/setomc.sh b/linux/usr/local/etc/omc/script/setomc.sh deleted file mode 100644 index 619d320..0000000 --- a/linux/usr/local/etc/omc/script/setomc.sh +++ /dev/null @@ -1,112 +0,0 @@ -#!/bin/bash - -# Read the value of the variable from file -source /usr/local/omc/etc/omc.conf - -C_ARG_LOWER="omc" -C_ARG_UPPER="OMC" -M_ARG_LOWER="*" - -check_args() { - while getopts "c:m:" option; do - case $option in - c) - C_ARG_LOWER=$(echo $OPTARG | tr '[:upper:]' '[:lower:]') - C_ARG_UPPER=$(echo $OPTARG | tr '[:lower:]' '[:upper:]') - # Modifying Script Variables - if [ "${C_ARG_LOWER}" == "ba" ]; then - C_ARG_UPPER="BA" - sed -i 's/VENDORS=.*/VENDORS=BA/' /usr/local/omc/etc/omc.conf - elif [ "${C_ARG_LOWER}" == "omc" ]; then - C_ARG_UPPER="OMC" - sed -i 's/VENDORS=.*/VENDORS=OMC/' /usr/local/omc/etc/omc.conf - elif [ "${C_ARG_LOWER}" == "agt" ]; then - C_ARG_UPPER="AGT" - sed -i 's/VENDORS=.*/VENDORS=AGT/' /usr/local/omc/etc/omc.conf - fi - ;; - m) - M_ARG=$(echo $OPTARG | tr '[:upper:]' '[:lower:]') - if [ "${VENDORS}" == "BA" ]; then - C_ARG_LOWER="ba" - C_ARG_UPPER="BA" - fi - ;; - \?) - echo "Invalid option: -$OPTARG" >&2 - ;; - esac - done -} - -USER="root" -PASSWORD="1000omc@kp!" -PORT="33066" -DBNAME="omc_db" -OMCRootDir=/usr/local/omc -OMCBinDir=${OMCRootDir}/bin -UpgradeSQLDir=${OMCRootDir}/etc/db/upgrade -Upgvue3SQLDir=${OMCRootDir}/etc/db/upgvue3 -InstallSQLDir=${OMCRootDir}/etc/db/install -OMCStaticDir=${OMCRootDir}/static - -check_args "$@" - -case "${M_ARG}" in - install) - ${OMCBinDir}/importdb.sh ${M_ARG} - - if [ "${C_ARG_LOWER}" != "" ]; then - CustomizedDir=${OMCStaticDir}/${C_ARG_LOWER}.d - if [ ! -d "${CustomizedDir}" ]; then - echo "Not found ${C_ARG_UPPER} customized directory, nothing to be done" - exit 1 - fi - echo -n "Setting ${C_ARG_UPPER} customized OMC ..." - for SQL in ${CustomizedDir}/db/*.sql; do - mysql -u${USER} -p${PASSWORD} -P ${PORT} --protocol tcp -D ${DBNAME} < ${SQL}; - done - cp -rf ${CustomizedDir}/logo/* ${OMCStaticDir}/logo - cp -rf ${CustomizedDir}/doc/* ${OMCStaticDir}/helpDoc - if [ "${C_ARG_LOWER}" == "ba" ]; then - rm -rf ${OMCStaticDir}/logo/zh_* - rm -rf ${OMCStaticDir}/helpDoc/zh_* - fi - #perl -0777 -i -pe 's/omcuser/bluearcus/g' ${OMCRootDir}/etc/default/restconf.yaml - #perl -0777 -i -pe 's/omcuser/bluearcus/g' ${OMCBinDir}/nehosts - if [ $? = 0 ]; then - echo "done" - fi - fi - ;; - upgrade | upgvue3) - ${OMCBinDir}/importdb.sh ${M_ARG} - ;; - skip) - if [ "${C_ARG_LOWER}" != "" ]; then - CustomizedDir=${OMCStaticDir}/${C_ARG_LOWER}.d - if [ ! -d "${CustomizedDir}" ]; then - echo "Not found ${C_ARG_UPPER} customized directory, nothing to be done" - exit 1 - fi - echo -n "Setting ${C_ARG_UPPER} customized OMC ..." - for SQL in ${CustomizedDir}/db/*.sql; do - mysql -u${USER} -p${PASSWORD} -P ${PORT} --protocol tcp -D ${DBNAME} < ${SQL}; - done - cp -rf ${CustomizedDir}/logo/* ${OMCStaticDir}/logo - cp -rf ${CustomizedDir}/doc/* ${OMCStaticDir}/helpDoc - if [ "${C_ARG_LOWER}" == "ba" ]; then - rm -rf ${OMCStaticDir}/logo/zh_* - rm -rf ${OMCStaticDir}/helpDoc/zh_* - fi - if [ $? = 0 ]; then - echo "done" - fi - fi - ;; - *) - ${OMCBinDir}/importdb.sh - ;; -esac - -exit 0 \ No newline at end of file diff --git a/linux/usr/local/etc/omc/script/ssh_keys.sh b/linux/usr/local/etc/omc/script/ssh_keys.sh new file mode 100644 index 0000000..2b2405b --- /dev/null +++ b/linux/usr/local/etc/omc/script/ssh_keys.sh @@ -0,0 +1,41 @@ +#!/bin/bash + +# Defining a list of SSH +# ssh_user,ssh_ip +# ssh_user,ssh_ip,ssh_port +SSH_LIST=( + "manager,172.16.5.100" + "manager,172.16.5.110" + "manager,172.16.5.120" + "manager,172.16.5.130" + "manager,172.16.5.140" + "manager,172.16.5.150" + "manager,172.16.5.160" + "manager,172.16.5.190" +) +DEFAULT_SSH_PORT=22 + +# Create .ssh directory if it doesn't exist +mkdir -p ~/.ssh +chmod 700 ~/.ssh +# Check if the SSH key already exists +if [ ! -f ~/.ssh/id_rsa ]; then + # Generate SSH key + ssh-keygen -t rsa -P "" -f ~/.ssh/id_rsa + chmod 600 ~/.ssh/id_rsa + chmod 644 ~/.ssh/id_rsa.pub +fi + +for v in "${SSH_LIST[@]}"; do + IFS=',' read -r ssh_user ssh_ip ssh_port <<< "$v" + if [ -z "$ssh_port" ]; then + ssh_port=$DEFAULT_SSH_PORT + fi + echo "===> ${ssh_user} ${ssh_ip} ${ssh_port}" + + # Copy public key content + cat ~/.ssh/id_rsa.pub | ssh -p ${ssh_port} ${ssh_user}@${ssh_ip} "\ + mkdir -p ~/.ssh && chmod 700 ~/.ssh && \ + touch ~/.ssh/authorized_keys && chmod 775 ~/.ssh/authorized_keys && \ + cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys" +done diff --git a/linux/usr/local/etc/omc/script/sshcpid.sh b/linux/usr/local/etc/omc/script/sshcpid.sh deleted file mode 100644 index 830ce91..0000000 --- a/linux/usr/local/etc/omc/script/sshcpid.sh +++ /dev/null @@ -1,23 +0,0 @@ -#!/bin/bash - -# 判断id_rsa密钥文件是否存在 -if [ ! -f ~/.ssh/id_rsa ];then - ssh-keygen -t rsa -P "" -f ~/.ssh/id_rsa -else - echo "id_rsa has created ..." -fi - -ip=$1 -user=$2 -passwd=$3 - -#分发到$ip主机. -expect <