Bash Script to append “host and ip entries” in remote server “/etc/hosts” file

We can use below script to add host and ip entries in 
/etc/hosts file.
$ cat append.sh

#!/bin/bash

## Append hostname and IP in /etc/hosts file

hostname=$1
ip=$2

/usr/bin/sshpass -p 'password' ssh user@$1 "echo password | sudo -S  sh -c 'echo "$2  $1  $1.example.com" >> /etc/hosts'"

$ sh append.sh test-server 192.168.1.100
It will make entry in /etc/hosts file of remote server 
"192.168.1.100" as show below:
$ hostname
test-server.example.com

$ cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

192.168.1.100   test-server test-server.example.com 

Leave a Reply

Your email address will not be published. Required fields are marked *