openstack端口禁用安全规则

#!/bin/bash
source ~/admin-openrc

UserHelp() {
cat <<EOF
# Disable the security rules and ports of the cloud host

Usage:
  ./disablePort.sh <network> server..

Options:
  <network>   Enter the network name. For example: selfnetwork provider

Example:
  ./disablePort.sh selfnetwork test01 test02
EOF
}

if [[ $1 == 'selfnetwork' || $1 == 'provider' ]]; then
  network=$1
  shift

  for i in $*; do
    # 根据主机名,查IP地址
    # "/${i}/"'{print $5}' 两者之间不能有空格,如果过滤项是变量请使用双引号
    # '{sub("^ *","");sub(" *$","");print $2}' 显示第二项并删除前后空格
    IP_Field=`openstack server list | awk -F"|" "/$i/"'{print $5}'`
    # 判断主机是否存在,不存在则跳过本次循环。继续下一次循环
    if [[ -z $IP_Field ]];then
      echo "$i Host does not exist"
      continue
    fi

    for j in $IP_Field;do
      if [[ $j =~ "$network" ]];then
        IP=`echo $j | awk -F"[,;=]" '{sub("^ *","");sub(" *$","");print $2}'`
        # echo $IP
      fi
    done
  
    # 根据IP地址,查port id
    if [[ ! -z $IP ]];then
      Port_id=`openstack port list | awk "/'${IP}'/"'{print $2}'`
      # echo $Port_id
    fi
  
    # 禁用安全规则和端口规格
    openstack port set --no-security-group --disable-port-security ${Port_id}
  done

elif [[ $1 == '--help' || $1 == '-h' || $# == 0 ]];then
  UserHelp

else
  echo -e "Wrong option\n\n"
  UserHelp

fi
posted @ 2021-07-02 10:03  jiaxzeng  阅读(254)  评论(0)    收藏  举报