Skip to content

Commit

Permalink
network debug learning
Browse files Browse the repository at this point in the history
  • Loading branch information
mookkiah committed May 28, 2024
1 parent 2ac3a1d commit 39b2e78
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 2 deletions.
110 changes: 110 additions & 0 deletions _posts/2023-09-20-aws-elbv2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
---
layout: post
title: "AWS Load Balancer"
date: 2023-09-20 03:42:00 -0400
modified_date: 2024-05-28 15:22:00 -0400
categories: aws elb
---

# AWS ELB

As the name Elastic Load Balancer says it balances the load by routing traffic across multiple targets.
Good place to configure and debug how the flow of traffic goes with in AWS.

## Notes

## Useful AWS commands

```sh
aws elbv2 describe-load-balancers --query 'LoadBalancers[].LoadBalancerName'
aws elbv2 describe-load-balancers --query 'LoadBalancers[].[LoadBalancerName,LoadBalancerArn]'


lb_arn=$(aws elbv2 describe-load-balancers --names "$1" --query 'LoadBalancers[].[LoadBalancerArn]' --output text)

listener_arn=$(aws elbv2 describe-listeners --load-balancer-arn "$lb_arn" --query 'Listeners[].{ListenerArn:ListenerArn,Protocol:Protocol,Port:Port}' | jq -r ".[] | select(.Port=="$2").ListenerArn")

aws elbv2 describe-rules --listener-arn "$listnerarn" --query 'Rules[].{Priority:Priority,Host:Conditions[0].Values[0]}' | jq

```

## Useful scripts

Python script which prints all the hostnames used in the load balancer rounting rule.
Change the log level to DEBUG to back trace.

```python
import boto3
from pprint import pprint
import json
import logging
import sys


logger = logging.getLogger('print-aws')
logger.setLevel(logging.INFO)
sh = logging.StreamHandler(sys.stdout)
formatter = logging.Formatter('[%(asctime)s] %(levelname)s [%(filename)s.%(funcName)s:%(lineno)d] %(message)s', datefmt='%a, %d %b %Y %H:%M:%S')
sh.setFormatter(formatter)
logger.addHandler(sh)

elbv2 = boto3.client("elbv2")


def printEndPointUrls():
response= elbv2.describe_load_balancers()
loadbalancers = response["LoadBalancers"][2:]
logger.debug(response)
for lb in loadbalancers:
lbArn=lb["LoadBalancerArn"]
printLBListeners(lbArn)


def printLBListeners(lbArn):
logger.debug(lbArn)
response = elbv2.describe_listeners(LoadBalancerArn=lbArn)
listeners = response["Listeners"]
logger.debug(listeners)
for listener in listeners:
if listener["Protocol"] == "HTTPS" and listener["Port"] == 443:
if not listener["DefaultActions"]:
logger.debug(listener["DefaultActions"][0]["RedirectConfig"]["Host"])
listenerArn = listener["ListenerArn"]
printListenerRules(listenerArn)


def printListenerRules(listenerArn):
logger.debug("printing Listener Rules...")
try:
response = elbv2.describe_rules(ListenerArn=listenerArn)
logger.debug(response)
if response["Rules"]:
for rule in response["Rules"]:
logger.debug(rule)
printRule(rule)
except Exception as e:
logger.error(e)
return



def printRule(rule):
try:
if rule["Conditions"]:
for condition in rule["Conditions"]:
if condition["Field"] == "host-header":
print(*condition["Values"], sep = "\n")
except Exception as e:
logger.error("Error at printing rule \n %s", json.dumps(rule, indent=2))
logger.error(e)
return


if __name__ == "__main__":
print(printEndPointUrls())
#print(printListenerRules("arn:aws:elasticloadbalancing:......"))


```

## Resources
31 changes: 29 additions & 2 deletions _posts/2023-11-29-networking-commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
layout: post
title: "Networking Commands"
date: 2023-11-29 03:30:00 -0500
modified_date: 2023-11-29 03:30:00 -0500
modified_date: 2024-05-28 03:27:00 -0500
categories: network
---

Expand Down Expand Up @@ -46,7 +46,28 @@ Address: 12.32.0.1#53
Non-authoritative answer:
*** Can't find cluster0.random.mongodb.net: No answer
```

To debug add `-debug`

```
$ nslookup -debug google.com
Server: 100.64.0.1
Address: 100.64.0.1#53
------------
QUESTIONS:
google.com, type = A, class = IN
ANSWERS:
-> google.com
internet address = 142.250.217.238
ttl = 54
AUTHORITY RECORDS:
ADDITIONAL RECORDS:
------------
Non-authoritative answer:
Name: google.com
Address: 142.250.217.238
```

### dig
Expand Down Expand Up @@ -142,8 +163,14 @@ $ traceroute google.com

### Debugging tools

In linux (Ubuntu), to get some basic commands, install net-tools.
In linux (Ubuntu), to get some basic commands (ex: dig, nslookup), install them using following commands.

```
$ sudo apt install net-tools
```

or in AWS cloudshell...

```
$ sudo yum install -y bind-utils
```

0 comments on commit 39b2e78

Please sign in to comment.