Configure hostname for multiple ESXi hosts

I build a bunch of new ESXi hosts and this is how I configure hostname for each host.

First, I have the list of the hosts’ FQDN in a file

$ cat list
esxi01.example.com
esxi02.example.com
esxi03.example.com
esxi04.example.com
esxi05.example.com
esxi06.example.com

I use cat and awk to generate an ssh command for each host. This assumes you have ssh keys configured.

$ cat list | awk '{print "ssh root@"$1  " esxcli system hostname set -f " $1}' > sethostname.sh
$ cat sethostname.sh
ssh root@esxi01.example.com esxcli system hostname set -f esxi01.example.com
ssh root@esxi02.example.com esxcli system hostname set -f esxi02.example.com
ssh root@esxi03.example.com esxcli system hostname set -f esxi03.example.com
ssh root@esxi04.example.com esxcli system hostname set -f esxi04.example.com
ssh root@esxi05.example.com esxcli system hostname set -f esxi05.example.com
$ chmod +x sethostname.sh
$ ./sethostname.sh

If you don’t have keys configured, you can use sshpass to provide the password to the ssh command. Remember to clear your history.

$ cat list | awk '{print "sshpass 'password' shh root@"$1  " esxcli system hostname set -f " $1}' > sethostname.sh
$ cat sethostname.sh
sshpass password shh root@esxi01.example.com esxcli system hostname set -f esxi01.example.com
sshpass password shh root@esxi02.example.com esxcli system hostname set -f esxi02.example.com
sshpass password shh root@esxi03.example.com esxcli system hostname set -f esxi03.example.com
sshpass password shh root@esxi04.example.com esxcli system hostname set -f esxi04.example.com
sshpass password shh root@esxi05.example.com esxcli system hostname set -f esxi05.example.com
sshpass password shh root@esxi06.example.com esxcli system hostname set -f esxi06.example.com
$ chmod +x sethostname.sh
$ ./sethostname.sh
Advertisements

One thought on “Configure hostname for multiple ESXi hosts

  1. Pingback: Add second VMKernel interface to multiple ESXi hosts – rtfmp

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s