1

I have controller node (CN) and server nodes (SN). From CN, I need to config the hostname of each and every SN. inv file contains list of SNs and corresponding hostname.

In below script, I am not sure how to pass hostname variable. Any best way to achieve same?

#!/bin/bash

IFS='\n'

for data in `awk 'NR>1 {print}' inv`
do
        ip=`awk '{print $1}' <<< $data`
        hostname=`awk '{print $2}' <<< $data`
        ssh root@$ip "hostnamectl set-hostname $hostname"
done

inv file,

IPs                     Hostname
172.31.98.11            Server1
172.31.106.177          Server2
172.31.97.105           Server3
4
  • What exactly do you mean? hostnamectl on the server should already be called with the correct value (the variable is expanded before the string is sent the server. Does the variable contain the correct value? Commented Apr 25, 2021 at 7:35
  • What is a "bash server"? Commented Apr 25, 2021 at 7:40
  • @stickybit other bash server. I changed title now. Commented Apr 25, 2021 at 7:57
  • 1
    @dahiya_boy I still don't understand. Why (and where) do you need the variable on the other server? You only execute a single command and it has the variable's value already expanded. Commented Apr 25, 2021 at 8:10

1 Answer 1

1

There is no need to use awk here since you are just printing all rows after 1st record. A tail does that job.

You may use this bash script:

while read -r ip hn; do
   ssh root@"$ip" "hostnamectl set-hostname $hn"
done < <(tail -n +2 inv)
Sign up to request clarification or add additional context in comments.

2 Comments

can you pls explain done < <(tail -n +2 inv) I unable to understand why < < and space b/w them.
< <(...) is called process substitution. Read more about it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.