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:
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 :
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"
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.
Dear Chetan_Tiwary_,
Thank you So much.
The solution is totally understandable and also learned in depth.
Best Regards
Krishna
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 :
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"
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.
Dear Chetan_Tiwary_,
Thank you So much.
The solution is totally understandable and also learned in depth.
Best Regards
Krishna
Red Hat
Learning Community
A collaborative learning environment, enabling open source skill development.