• 1,013 Views

For loop to get the hostname

Jump to solution

I am a new learner of Linux.

can you help me to solve a problem?

I want to check the hostname using the for loop command: what will be the

command to check the hostname using the for loop command:

 

2 Solutions

Accepted Solutions
Chetan_Tiwary_
Moderator
Moderator
  • 979 Views

Hello @KRISHNA_GOPAUL !

I am not sure the purpose of this but if you are just asking about the structure/format for this , then you can use something like this :

Suppose you have a list of all servers or their IPs in servers.txt file as :

servera.<domain> or 192.X.X.1

serverb.<domain> or 192.X.X.2

serverc.<domain> or 192.X.X.3

then use for loop like this : This explains the working of "for loop" in a basic manner :

Screenshot from 2023-08-28 19-22-18.png

If you just have the server list or the list of the IP address of those servers and  want to know the hostname for all those servers remotely - then you can use ssh 'command' with the "for loop"

Chetan_Tiwary__0-1693238656780.png

This command uses a for loop to iterate over the list of servers ( or their IPs ) in the server_list.txt file. For each server, the ssh command is used to connect to the server and  the echo $(hostname -f) command will print the hostname of the remote server to the console.

The -t option with the ssh command tells the ssh command to execute the command on the remote server.

The 2>/dev/null part of the command redirects the standard error output of the ssh command to the /dev/null device. This prevents the connection to server closed message from being printed to the console.

Let me know if this works for you or you are looking for something else.

View solution in original post

Tags (3)
  • 947 Views

Dear Chetan_Tiwary_,

Thank you So much.

The solution is totally understandable and also learned in depth.

 

Best Regards

Krishna

 

View solution in original post

2 Replies
Chetan_Tiwary_
Moderator
Moderator
  • 980 Views

Hello @KRISHNA_GOPAUL !

I am not sure the purpose of this but if you are just asking about the structure/format for this , then you can use something like this :

Suppose you have a list of all servers or their IPs in servers.txt file as :

servera.<domain> or 192.X.X.1

serverb.<domain> or 192.X.X.2

serverc.<domain> or 192.X.X.3

then use for loop like this : This explains the working of "for loop" in a basic manner :

Screenshot from 2023-08-28 19-22-18.png

If you just have the server list or the list of the IP address of those servers and  want to know the hostname for all those servers remotely - then you can use ssh 'command' with the "for loop"

Chetan_Tiwary__0-1693238656780.png

This command uses a for loop to iterate over the list of servers ( or their IPs ) in the server_list.txt file. For each server, the ssh command is used to connect to the server and  the echo $(hostname -f) command will print the hostname of the remote server to the console.

The -t option with the ssh command tells the ssh command to execute the command on the remote server.

The 2>/dev/null part of the command redirects the standard error output of the ssh command to the /dev/null device. This prevents the connection to server closed message from being printed to the console.

Let me know if this works for you or you are looking for something else.

Tags (3)
  • 948 Views

Dear Chetan_Tiwary_,

Thank you So much.

The solution is totally understandable and also learned in depth.

 

Best Regards

Krishna

 

Join the discussion
You must log in to join this conversation.