Injecting ip address into command prompt

StevieD asked:

I have the following convoluted method for injecting the ip address on an ec2 instance into the command line. It works but has some obvious problems and I’m looking for a good way to solve them:

TOKEN=`curl -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600"`
IP=`curl -H "X-aws-ec2-metadata-token: \$TOKEN" -v http://169.254.169.254/latest/meta-data/local-ipv4`

if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\[email protected]$IP-\H\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else
    PS1='$IP${debian_chroot:+($debian_chroot)}\[email protected]$IP-\H:\w\$ '
fi

My method has a couple of problems I want to address:

  1. It generates a bunch of unwanted output when loading or logging into bash:
Last login: Thu Dec 17 16:57:37 2020 from 22.123.17.122
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    56  100    56    0     0  11200      0 --:--:-- --:--:-- --:--:-- 11200
* Expire in 0 ms for 6 (transfer 0x560383dabf90)
*   Trying 169.254.169.254...
* TCP_NODELAY set
* Expire in 200 ms for 4 (transfer 0x560383dabf90)
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0* Connected to 169.254.169.254 (169.254.169.254) port 80 (#0)
> GET /latest/meta-data/local-ipv4 HTTP/1.1
> Host: 169.254.169.254
> User-Agent: curl/7.64.0
> Accept: */*
> X-aws-ec2-metadata-token: AQAAAEjtIasdfadfasdf5ksdjkjkkasdfe947xQ==
>
* HTTP 1.0, assume close after body
< HTTP/1.0 200 OK
< Accept-Ranges: bytes
< Content-Length: 12
< Content-Type: text/plain
< Date: Thu, 17 Dec 2020 22:52:25 GMT
< Last-Modified: Tue, 15 Dec 2020 14:11:17 GMT
< X-Aws-Ec2-Metadata-Token-Ttl-Seconds: 21600
< Connection: close
< Server: EC2ws
<
{ [12 bytes data]
100    12  100    12    0     0   2400      0 --:--:-- --:--:-- --:--:--  2400
* Closing connection 0
  1. The other problem is this runs every time I log into bash or reload bash. I’d like to figure out a way to bypass this if the IP address is already known.

My answer:


You specifically asked for verbose output from curl in the second command line, which accounts for most of the output. You should remove -v. Then, to suppress the progress meter, use -s (in both commands). This should leave you with no output, unless there is an error.

It would be better if you ran this at startup and wrote the resulting IP address to a file, and then simply read the contents of that file in your bash profile. (This assumes that the IP address doesn’t change while the instance is running, which AFAIK doesn’t happen.)

Consider a local startup script like this:

#!/usr/bin/env bash
TOKEN=`curl -s -X PUT -H "X-aws-ec2-metadata-token-ttl-seconds: 21600" http://169.254.169.254/latest/api/token`
curl -s -H "X-aws-ec2-metadata-token: \$TOKEN" -o /tmp/local-ipv4 http://169.254.169.254/latest/meta-data/local-ipv4

Now you can read it in your bash profile:

IP=$(cat /tmp/local-ipv4)

View the full question and any other answers on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.