#!/bin/bash
#description: change ip on instances
#Usage: ./ChangeIP.sh fileId vmId
# flush the caches of remote hosts
>~/.ssh/known_hosts
#define the network configfile location
dist=/etc/sysconfig/network-scripts/ifcfg-eth0
# define a function which copy the new ifcfg-eth0 file
# from cloudstack management or from other host to new
# instance boot from vm_fixip without interactive
function scp_file(){
expect -c "
set timeout -1
spawn -noecho scp $1 $2
expect "yes/no"
send "yesr"
expect "password:"
send "passwordr"
expect eof
"
}
scp_file ifcfg-eth0.$1 root@vm$2:$dist
# this function named res_new means restart network
# on new instance loading from new network config file
# without interactive
function res_net(){
expect -c "
set timeout -1
spawn -noecho ssh $1 $2
expect "password:"
send "passwordr"
expect eof
"
}
res_net root@vm$2 "service network restart > /dev/null 2>&1 &"
sleep 2
exit 0