Skip to content

Instantly share code, notes, and snippets.

@tao12345666333
Created February 10, 2023 04:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tao12345666333/7199857344c9a868bf1edcea39637468 to your computer and use it in GitHub Desktop.
Save tao12345666333/7199857344c9a868bf1edcea39637468 to your computer and use it in GitHub Desktop.
It used to convert a string in the format XXXXXXXX:XXXX, where X is a hexadecimal digit, into the dotted-quad notation (e.g. A.B.C.D) and decimal port number format.
#!/bin/bash
address_string="$1"
# Split the string into IP address and port number parts
ip_hex="$(echo "$address_string" | cut -d : -f 1)"
port_hex="$(echo "$address_string" | cut -d : -f 2)"
# Convert the hexadecimal IP address to decimal
ip_dec=$(printf %d 0x$ip_hex)
# Convert the decimal IP address to dotted-quad notation
ip_quad=$(echo $((($ip_dec>>24)&255)).$((($ip_dec>>16)&255)).$((($ip_dec>>8)&255)).$((($ip_dec)&255)))
# Convert the hexadecimal port number to decimal
port_dec=$(printf %d 0x$port_hex)
# Print the result
echo "$ip_quad:$port_dec"
@tao12345666333
Copy link
Author

$ head -n 2 /proc/1/net/tcp
  sl  local_address rem_address   st tx_queue rx_queue tr tm->when retrnsmt   uid  timeout inode                                                     
   0: 00000000:2378 00000000:0000 0A 00000000:00000000 00:00000000 00000000  1001        0 23741 1 00000000842a97c4 100 0 0 10 0

$ bash convert_address.sh 00000000:2378
0.0.0.0:9080

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment