LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Some practice question - working with servers (https://www.linuxquestions.org/questions/programming-9/some-practice-question-working-with-servers-4175703534/)

airline33 11-13-2021 05:44 AM

Some practice question - working with servers
 
Hello everyone,
I would like to have some help with some questions I encountered lately:

1. Write a command that shows how many hops you have between your machine and google.com.

2. Using this API: http://api.open-notify.org/
a - Write a command that shows how many people are in space in any moment.
b - Write a command that prints the current longitude of the international space station.



3. Using this: https://raw.githubusercontent.com/Bi...r/pokedex.json
a - write a command that prints the number of Pokemons available in Pokedex.

b - Write a command that prints all the height of the Pokemons from the lowest to the tallest.

c - Who is the tallest Pokemon?



4. Using this: https://ipapi.co/<ip>/json
a - write a command that prints the country where the server of which one of the following addresses is located: amazon.com, amazon.co.uk, amazon.de, amazon.co.jp

b - Are those servers are located in their country?





Thanks

astrogeek 11-13-2021 11:43 AM

Welcome to LQ!

Per the LQ Rules, please do not post homework assignments verbatim. We're happy to assist if you have specific questions or have hit a stumbling point, however. Let us know what you've already tried and what references you have used (including class notes, books, and Google searches) and we'll do our best to help. Also, keep in mind that your instructor might also be an LQ member.

Please review the Site FAQ for guidance in asking well formed questions. Especially visit the link from that page, How to Ask Questions the Smart Way for discussion of things to consider when asking others for help.

shruggy 11-13-2021 02:03 PM

Cross-posts @linux.org and @SO (closed).

Nice APIs they have out there. I didn't count the lines of traceroute output as this would be too easy, but did all the other exercises with curl and jq. See below (I printed only the three shortest and the tallest Pokemon):
Code:

#!/bin/sh
echo = 2 =
api=http://api.open-notify.org
echo == 2a ==
echo 'Q: How many people are in space now?'
echo  "A: $(curl -s $api/astros.json|jq .number)"
echo == 2b ==
echo 'Q: Current longitude of the ISS?'
echo "A: $(curl -s $api/iss-now.json|jq -r .iss_position.longitude)"

echo = 3 =
api=https://raw.githubusercontent.com/Biuni/PokemonGO-Pokedex/master/pokedex.json
echo == 3a ==
echo 'Q: Number of Pokemons available in Pokedex?'
echo "A: $(curl -s $api|jq '.pokemon|length')"

echo == 3b ==
echo 'Q: Heights of all Pokemons from the lowest to the tallest'
curl -s $api|
  jq -r '.pokemon|sort_by(.height)[]|"\(.name) is \(.height) tall"'|
  sed -n '1,3p;$p;3a...'

echo == 3c ==
echo 'Q: Who is the tallest Pokemon?'
curl -s $api|
  jq -r '.pokemon|max_by(.height)|"A: \(.name) is the tallest"'

echo = 4 =
api=https://ipapi.co
echo == 4a ==
echo 'Q: What country the following addresses are located in?'
for cc in com co.uk de co.jp
do
  for ip in $(host -tA amazon.$cc|awk '$0=$NF')
  do
    printf '%-13s %17s is in %s\n' \
      "amazon.$cc" "($ip)" "$(curl -s $api/$ip/json|jq -r .country_name)"
  done
done

echo == 4b ==
echo 'Q: Are those servers located in their country?'
for c in US:com GB:co.uk DE:de JP:co.jp
do
  cc=${c#*:}
  for ip in $(host -tA amazon.$cc|awk '$0=$NF')
  do
    printf '%-13s %17s is %s\n' \
      "amazon.$cc" "($ip)" \
      "$(curl -s $api/$ip/json|
            jq -r 'if .country=="'"${c%:*}"'" then "at home" else "abroad" end')"
  done
done


dugan 11-13-2021 02:10 PM

Quote:

Originally Posted by airline33 (Post 6300936)
1. Write a command that shows how many hops you have between your machine and google.com.

Code:

traceroute www.google.com | wc -l

shruggy 11-13-2021 02:14 PM

@dugan. Off by one. The first line is the header, at least in my version of traceroute.


All times are GMT -5. The time now is 08:39 AM.