vm_eip_change.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #!/usr/bin/python
  2. #coding=utf-8
  3. from kscore.session import get_session
  4. from kscore.exceptions import ClientError
  5. import sys
  6. #使用方法
  7. '''
  8. VM_EIP批量切换
  9. python python vm_eip_change.py vm_eip_change.csv
  10. '''
  11. #输入文件格式(csv文件)示例
  12. '''
  13. InstanceID,NewEIP
  14. 5e7f65c5-d338-4ccd-8ce9-f196be5fc7e3,192.168.1.1
  15. '''
  16. #属性解释
  17. '''
  18. InstanceID 主机或者原EIP实例
  19. NewEIP 新的EIP的IP
  20. '''
  21. ak ='ak'
  22. sk = 'sk'
  23. region = 'cn-beijing-6'
  24. def createSdkClinet(service,region):
  25. s = get_session()
  26. s.set_credentials(ak,sk)
  27. client = s.create_client(service, region, use_ssl=True)
  28. return client
  29. if __name__ == "__main__":
  30. # 是否实例是EIP实例
  31. instance_is_eip = True
  32. try:
  33. f = open(sys.argv[1])
  34. except IOError:
  35. print 'File load Error'
  36. sys.exit(0)
  37. print 'InstanceID,NewEIPIp,NewEIpId,OldEIPIp,OldEIPId'
  38. kec_client = createSdkClinet("kec",region)
  39. eip_client = createSdkClinet("eip",region)
  40. content = f.readline()
  41. content = content.replace("\n", "")
  42. while content:
  43. content = f.readline()
  44. content = content.replace("\n", "")
  45. if content != '':
  46. contents = content.split(',')
  47. vm_id = content[0]
  48. if instance_is_eip:
  49. # 查询EIP信息
  50. param_eip = {
  51. "AllocationId.1": contents[0]
  52. }
  53. eip_result = eip_client.describe_addresses(**param_eip)
  54. for item in eip_result['AddressesSet']:
  55. old_eip_ip = item['PublicIp']
  56. old_eip_id = item['AllocationId']
  57. vif_id = item['NetworkInterfaceId']
  58. # 查询主机信息
  59. param_kec = {
  60. "Filter.1.Name": 'network-interface.network-interface-id',
  61. 'Filter.1.Value.1': vif_id
  62. }
  63. kec_result = kec_client.describe_instances(**param_kec)
  64. vm_id = kec_result["InstancesSet"][0]["InstanceId"]
  65. else:
  66. # 查询主机的网卡信息
  67. param_kec = {
  68. 'InstanceId.1': contents[0]
  69. }
  70. kec_result = kec_client.describe_instances(**param_kec)
  71. vif_id = kec_result["InstancesSet"][0]["NetworkInterfaceSet"][0]["NetworkInterfaceId"]
  72. # 根据网卡信息查询原先绑定的EIP信息
  73. param_eip = {
  74. "Filter.1.Name": 'network-interface-id',
  75. 'Filter.1.Value.1': vif_id
  76. }
  77. eip_result = eip_client.describe_addresses(**param_eip)
  78. for item in eip_result['AddressesSet']:
  79. old_eip_ip = item['PublicIp']
  80. old_eip_id = item['AllocationId']
  81. # 解绑EIP
  82. if old_eip_id:
  83. param_eip_dis = {
  84. 'AllocationId': old_eip_id
  85. }
  86. print param_eip_dis
  87. eip_client.disassociate_address(**param_eip_dis)
  88. # 查询新EIP
  89. param_new_eip = {
  90. "Filter.1.Name": "public-ip",
  91. "Filter.1.Value.1" : contents[1]
  92. }
  93. new_eip_result = eip_client.describe_addresses(**param_new_eip)
  94. for item in new_eip_result['AddressesSet']:
  95. new_eip_id = item['AllocationId']
  96. new_eip_ip = item['PublicIp']
  97. # 绑定EIP
  98. if new_eip_id:
  99. param_eip_ass = {
  100. 'AllocationId': new_eip_id,
  101. 'InstanceType': 'Ipfwd',
  102. 'InstanceId': vm_id,
  103. 'NetworkInterfaceId': vif_id
  104. }
  105. print param_eip_ass
  106. eip_client.associate_address(**param_eip_ass)
  107. # 输出结果
  108. print vm_id+','+new_eip_ip+','+new_eip_id+','+old_eip_ip+','+old_eip_id