Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

340
Views
Actualización de direcciones IP existentes de un grupo de seguridad en AWS mediante aws cli

Tengo un script de shell que agrega mi IP pública al grupo de seguridad ec2 especificado. Revisé algunos documentos de AWS y no puedo encontrar qué Apis usar para actualizar la dirección IP existente en lugar de simplemente agregar una.

He pasado por lo siguiente:

  1. actualización-seguridad-grupo-regla-descripciones-ingreso
  2. autorizar-seguridad-grupo-ingreso

¿Hay una API que se pueda usar para simplemente actualizar la dirección IP existente en el grupo de seguridad?

Estoy usando el siguiente script bash para agregar nuevas entradas al grupo de seguridad.

 #!/bin/bash curl https://checkip.amazonaws.com > ip.txt awk '{ print $0 "/32" }' < ip.txt > ipnew.txt export stuff=$(cat ipnew.txt) aws ec2 authorize-security-group-ingress --group-name XXXXX --protocol tcp --port 22 --cidr $stuff --profile xxxxx
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Este script encontrará cualquier grupo de seguridad etiquetado con la clave ssh-from-my-ip y un valor que no distingue entre mayúsculas y minúsculas de true o yes . Luego revocará el antiguo acceso de entrada desde el puerto 22 (si lo hay) y autorizará su nueva IP CIDR. Requiere aws cli y jq.

 #! /bin/bash # This script makes it easier to maintain security groups that allow SSH access # from a computer with a dynamic IP, such as a computer on a home network or ISP. # # Using the script will allow you to SSH to an EC2 without having to allow # access to the whole world (0.0.0.0/0). If you run this script whenever your IP # changes then the security groups in your account specified by your AWS profile # will be updated. # # The script will find any security groups for your current profile that are # tagged with a Tag with a Key of "ssh-from-my-ip" and a case insensitive value # of "true" or "yes". # # For each security group found it will revoke any existing tcp ingress on # port 22 and authorize ingress on port 22 for your current IP. # # Dependencies - AWS CLI and jq # need my current ip MY_IP=$(curl --silent https://checkip.amazonaws.com) echo "Your IP is ${MY_IP}" # need security group id(s) and existing CIDR for the SG pairs=$(aws ec2 describe-security-groups | aws ec2 describe-security-groups | jq -c '.SecurityGroups[]? | select( (.Tags[]? | select(.Key == "ssh-from-my-ip") | .Value | test("true|yes"; "i"))) | if .IpPermissions | length == 0 then {sg: .GroupId, cidr: null } else {sg: .GroupId, cidr: .IpPermissions[].IpRanges[].CidrIp} end') for p in $pairs do SG=$(echo "$p" | jq -r '.sg') OLD_CIDR=$(echo "$p" | jq -r '.cidr') echo "Updating security group ${SG}" if [[ $OLD_CIDR != 'null' ]] then echo "Revoking ingress permission for ${OLD_CIDR} in security group ${SG}" # remove the existing ingress permission aws ec2 revoke-security-group-ingress \ --group-id "${SG}" \ --protocol tcp \ --port 22 \ --cidr "${OLD_CIDR}" fi # authorize my new IP CIDR NEW_CIDR="${MY_IP}"/32 echo "Authorizing ingress permission for ${NEW_CIDR} in security group ${SG}" aws ec2 authorize-security-group-ingress --group-id "${SG}" --ip-permissions '[{"IpProtocol": "tcp", "FromPort": 22, "ToPort": 22, "IpRanges": [{"CidrIp": "'"${NEW_CIDR}"'", "Description": "Rule0"}]}]' done
over 4 years ago · Santiago Trujillo Report

0

No hay ningún comando para 'actualizar' una regla. Deberá agregar y eliminar reglas.

Aquí hay un script similar que uso:

 IP=`curl -s http://whatismyip.akamai.com/` aws ec2 authorize-security-group-ingress --group-name XXX --protocol tcp --port 22 --cidr $IP/32 --output text

Sin embargo, esto finalmente agrega demasiadas reglas, por lo que necesito eliminar las reglas existentes. Puede automatizar esa eliminación antes de agregar una regla.

over 4 years ago · Santiago Trujillo Report

0

He sido capaz de hackear mi camino para hacer que esto funcione. Como sugirió John, creé otro grupo de seguridad, agregué los puertos que requieren acceso y lo actualicé a través del script de shell. La actualización funciona eliminando todas las reglas mencionadas en el grupo de seguridad y volviéndolas a agregar con la IP requerida

El código fuente ha sido publicado en Github.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!