全球主机交流论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

CeraNetworks网络延迟测速工具IP归属甄别会员请立即修改密码
查看: 914|回复: 6

[经验] 通过Azure Automation runbook来更新服务器的动态IP

[复制链接]
发表于 2023-11-2 18:59:09 | 显示全部楼层 |阅读模式
本帖最后由 Microcharon 于 2023-11-3 17:57 编辑

由于经常有更换 Azure IP 的习惯,所以用 Azure automation 的 runbook 来自动更新服务器的IP,前提是服务器IP是动态IP

测试了一下,还是可以正常工作的









大概就是先在 Azure 创建一个 automation 账户,然后在程序自动化那里新建一个 runbook,类型就用 PowerShell,版本随意

创建好一个新的 runbook 后在概观那里选择编辑,加入下面 gist 的 PowerShell (CMDLET) 代码,根据自己的资源组来修改变量,然后测试 or 执行试一下,成功输出后发行 runbook,在排程那里新建排程,根据自己喜好设置一次性 or 周期性排程

更新了一下版本,只需要手动修改一下资源组名称即可,所有资源需要同处于相同的资源组中

  1. $resource_group = "AzureVMJP" #资源组名称
复制代码


完整 powershell 代码

  1. # Connect Azure account
  2. Connect-AzAccount -Identity

  3. # replace these with your own values
  4. $resource_group = "AzureVMJP"

  5. # Get Azure Resource in same resource group
  6. $vm_name = (Get-AzResource -ResourceGroupName $resource_group -ResourceType "Microsoft.Compute/virtualMachines").Name
  7. $public_ip_name = (Get-AzResource -ResourceGroupName $resource_group -ResourceType "Microsoft.Network/publicIPAddresses")[0].Name
  8. $dns_label = (Get-AzPublicIpAddress -ResourceGroupName $resource_group -Name $public_ip_name).DnsSettings.DomainNameLabel

  9. # Stop the VM
  10. # Stop-AzVM -ResourceGroupName $resource_group -Name $vm_name -Force

  11. # Get the NIC
  12. $nic_name = (Get-AzResource -ResourceGroupName $resource_group -ResourceType "Microsoft.Network/networkInterfaces").Name
  13. $nic = Get-AzNetworkInterface -ResourceGroupName $resource_group -Name $nic_name

  14. # Get NIC ipconfig
  15. # $ip_config_name = (Get-AzNetworkInterface -ResourceGroupName $resource_group -Name $nic_name).IpConfigurations[0].Name

  16. # Disassociate the existing public IP address from the NIC
  17. $nic.IpConfigurations[0].PublicIpAddress = $null
  18. Set-AzNetworkInterface -NetworkInterface $nic

  19. # Create a new public IP address
  20. $location = (Get-AzResourceGroup -Name $resource_group).Location
  21. $public_ip = New-AzPublicIpAddress -Name $public_ip_name -ResourceGroupName $resource_group -AllocationMethod Dynamic -Location $location -DomainNameLabel $dns_label -Force

  22. # Associate the new public IP address with the NIC
  23. $nic.IpConfigurations[0].PublicIpAddress = $public_ip
  24. Set-AzNetworkInterface -NetworkInterface $nic

  25. # Start the VM
  26. # Start-AzVM -ResourceGroupName $resource_group -Name $vm_name

  27. # Output the new public IP address details
  28. Get-AzPublicIpAddress -ResourceGroupName $resource_group -Name $public_ip_name |
  29. Select-Object @{Name='Fqdn'; Expression={$_.DnsSettings.Fqdn}}, IpAddress, PublicIpAllocationMethod
复制代码


https://gist.github.com/Debcharo ... ba8e20453bc00c330f3
发表于 2023-11-4 15:14:33 | 显示全部楼层
如果是多ip模式下,不换主ip,只更换辅助ip呢
 楼主| 发表于 2023-11-4 16:12:15 | 显示全部楼层
office_google 发表于 2023-11-4 15:14
如果是多ip模式下,不换主ip,只更换辅助ip呢

辅助IP设定只有一个还是多个呢?

是需要把辅助IP全部都换一遍IP还是只选1个或多个来更换IP呢?
发表于 2023-11-4 16:18:22 来自手机 | 显示全部楼层
为什么不用服务器自带的cname
 楼主| 发表于 2023-11-4 16:29:25 | 显示全部楼层
wwbfred 发表于 2023-11-4 16:18
为什么不用服务器自带的cname

你说的是IP设定里的DNS设定吗?

  1. <servername>.<location>.cloudapp.azure.com
复制代码


https://learn.microsoft.com/zh-c ... hines/custom-domain
发表于 2023-11-6 16:09:48 | 显示全部楼层
Microcharon 发表于 2023-11-4 02:12
辅助IP设定只有一个还是多个呢?

是需要把辅助IP全部都换一遍IP还是只选1个或多个来更换IP呢? ...

多个辅助ip的情况,主ip不变,辅助ip全变
 楼主| 发表于 2023-11-8 14:58:39 | 显示全部楼层
office_google 发表于 2023-11-6 16:09
多个辅助ip的情况,主ip不变,辅助ip全变








https://gist.github.com/Debcharo ... 863dcd7c3cd5d39932e

稍微改了一下,次要 IP 就不弄 dns_label 了,通过 foreach 遍历

要求都在同一资源组中,可以通过修改变量切换


  1. $resource_group = "AzureVMJP"
复制代码


ipconfig 和公共 IP 位址的命名格式符合:ipconfig [num] 对应 "$resource_group" + "-ip" + "[num-1]"
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|全球主机交流论坛

GMT+8, 2024-9-20 00:14 , Processed in 0.061286 second(s), 8 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表