天天看點

遠端登入linux主機更換IP位址【非互動】

根據開發組同僚反應,通過VCenter對RHEL6.0以下版本的系統進行克隆的時候,無法使用進階選項進行IP的指定操作,進而通過CloudStack對新的執行個體指定IP也是不能實作的,而且,不能夠使用DHCP伺服器解決該問題,否則會導緻新虛拟機IP位址跟指定IP的不一緻,也可能導緻整個系統中IP位址配置設定的混亂。

給RHEL6.0以下系統制作的模闆均指定一個固定的保留IP位址,比如192.168.3.0/24網段中,保留192.168.3.240~192.168.3.250給此類模闆系統。

不管是通過VCenter克隆虛拟機還是CloudStack建立執行個體,隻要是通過該固定IP位址啟動的所有虛拟機,均會成功以原有IP位址啟動,并正常跟網段内其他IP位址通訊,需要保證同一時間不允許同一IP位址的模闆同時啟動。

下文以模闆固定IP為:10.196.18.250的系統,在控制節點,如cloudstackmanagement節點上面/etc/hosts中進行解析:

10.196.18.250        vm250

1.啟動vm250 ,通過檢測機制判斷其啟動成功,程式中去對10.196.18.250進行簡單的連接配接測試,ping 通即可。

2.在啟動執行個體的過程中,同時将IP位址資訊寫入本地目錄中,按照如下格式儲存:

假設檔案名為:ifcfg-eth0.001,内容為如下

DEVICE=eth0

ONBOOT=on

BOOTPROTO=static

IPADDR=10.196.28.208

NETMASK=255.255.255.0

GATEWAY=10.196.28.254

3.調用腳本檔案:ChangeIP.sh ,該腳本檔案完成兩項任務:

【1】将ifcfg-eth0.Id 拷貝到vmId指定目錄中,重命名為ifcfg-eth0

【2】通過cloudstack management 登陸vmId,重新開機網絡服務,使得新的網絡配置檔案生效。

使用方法:./ChangeIP.sh  id_of_ifcfg-eth0  id_of_vm

如:        ./ChangeIP.sh  001 250  ,将ifcfg-eth0.001 檔案拷貝紙vm250系統中,并重新啟動網絡服務,使得新執行個體的最終IP位址為10.196.28.208

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

<code>#!/bin/bash</code>

<code>#description: change ip  on instances</code>

<code>#Usage: ./ChangeIP.sh fileId vmId</code>

<code># flush the caches of remote hosts</code>

<code>&gt;~/.</code><code>ssh</code><code>/known_hosts</code>

<code>#define the network configfile location</code>

<code>dist=</code><code>/etc/sysconfig/network-scripts/ifcfg-eth0</code>

<code># define a function which copy the new ifcfg-eth0 file</code>

<code># from cloudstack management or from other host to new</code>

<code># instance boot from vm_fixip  without interactive</code>

<code>function</code> <code>scp_file(){</code>

<code>expect -c "</code>

<code>set</code> <code>timeout -1</code>

<code>spawn -noecho </code><code>scp</code> <code>$1 $2</code>

<code>expect </code><code>"yes/no"</code>

<code>send \"</code><code>yes</code><code>\r\"</code>

<code>expect </code><code>"password:"</code>

<code>send \"password\r\"</code>

<code>expect eof</code>

<code>"</code>

<code>}</code>

<code>scp_file   ifcfg-eth0.$1        root@vm$2:$dist</code>

<code># this  function named res_new means restart network</code>

<code># on new instance  loading from new network config file</code>

<code># without interactive</code>

<code>function</code> <code>res_net(){</code>

<code>spawn -noecho </code><code>ssh</code>  <code>$1 $2</code>

<code>res_net  root@vm$2 </code><code>"service network restart &gt; /dev/null 2&gt;&amp;1 &amp;"</code>

<code>sleep</code> <code>2</code>

<code>exit</code> <code>0</code>

<code></code>

本文轉自 暗黑魔君 51CTO部落格,原文連結:http://blog.51cto.com/clovemfong/1272062,如需轉載請自行聯系原作者

繼續閱讀