设为首页 收藏本站
查看: 574|回复: 0

[经验分享] 在Azure上通过Powershell创建多Interface的Cisco CSR路由器

[复制链接]

尚未签到

发表于 2017-6-30 19:09:31 | 显示全部楼层 |阅读模式
  前面通过Json的Template在Azure上创建了Cisco的CSR路由器。但那个Json的template只支持1块网卡。如果需要多网卡的Cisco CSR路由器,可以改上篇文章中提到的Json Template文件,也可以用Powershell的脚本创建。
  本文将介绍如何用Powershell创建多Interface的Cisco CSR路由器。
  一、确定Cisco CSR Image的位置
  和上篇文章相同,Cisco CSR Image的链接如下,我把这个文件public出来了,大家可以直接下载:
  https://ciscorouter.blob.core.chinacloudapi.cn/image/CSR_3_16_4aS_CCO.vhd
  二、编写Powershell脚本,创建2网卡的Cisco CSR路由器



function new-ciscocsr{
param(
#The VM resource group
[Parameter(Mandatory=$true)]
[String]$rgname,
#The VM name
[Parameter(Mandatory=$true)]
[String]$vmname,
#The High Avalibility Set name
[Parameter(Mandatory=$true)]
[String]$hasetname,
#The new VM IP name
[Parameter(Mandatory=$true)]
[String]$vmpipname,
#The Vnet Name
[Parameter(Mandatory=$true)]
[String]$vnetname,
#The Subnet1 Name
[Parameter(Mandatory=$true)]
[String]$subnetname1,
#The Subnet2 Name
[Parameter(Mandatory=$true)]
[String]$subnetname2,
#The new VM size
[Parameter(Mandatory=$true)]
[String]$vmsize,
#The new user
[Parameter(Mandatory=$true)]
[String]$newuser,
#The new password
[Parameter(Mandatory=$true)]
[String]$newpwd,
#The Image URL
[Parameter(Mandatory=$true)]
[String]$ImageURL
)
#Get a random text as the random text
$hash = $null
for ($i = 0; $i  4; $i++){
$j = (97..122) | Get-Random -Count 1 | % {[char]$_}
$hash = $hash + $j
}
for ($i = 0; $i  4; $i++){
$j = (48..57) | Get-Random -Count 1 | % {[char]$_}
$hash = $hash + $j
}
#check the Resource Group, if not exist, create
$rgs = Get-AzureRmResourceGroup -Location "China East"
$rgrslt = $false
foreach ($rg in $rgs){if($rg.ResourceGroupName  $rgname){$rgrslt = $true;break}}
if( $rgrslt) {$rg = New-AzureRmResourceGroup -Name $rgname -Location "China East"}
#check the High Avalibility Set, if not exist, create
foreach ($rgh in $rgs){
$haset = Get-AzureRmAvailabilitySet -ResourceGroupName $rgh.ResourceGroupName -Name $hasetname -ErrorAction Ignore;
if($haset.name  $hasetname){
if($haset.ResourceGroupName  $rgname){break;}
else{write-host "Please change another haset name";exit;}
}
}
if( $haset.Name) {$haset = new-AzureRmAvailabilitySet -ResourceGroupName $rgname -Name $hasetname -Location $rg.Location}
#check the Vnet, if not exist, create
$vnets = Get-AzureRmVirtualNetwork
$vnetrslt = $false
foreach ($vnet in $vnets){if($vnet.Name  $vnetname){$vnetrslt = $true;break}}
if( $vnetrslt) {
$vnet = New-AzureRmVirtualNetwork -Name $vnetname -AddressPrefix 172.16.0.0/16  -ResourceGroupName $rgname -Location $rg.Location;
$subnet1 = add-AzureRmVirtualNetworkSubnetConfig -Name $subnetname1 -AddressPrefix 172.16.1.0/24 -VirtualNetwork $vnet;
$subnet2 = add-AzureRmVirtualNetworkSubnetConfig -Name $subnetname2 -AddressPrefix 172.16.2.0/24 -VirtualNetwork $vnet;
$vnet = Set-AzureRmVirtualNetwork -VirtualNetwork $vnet
}
#check the PIP address, if not exist, create
$vmpipname01 = $vmpipname + "01"
$vmpipname02 = $vmpipname + "02"
$pip01rslt = Test-AzureRmDnsAvailability -DomainNameLabel $vmpipname01 -Location $rg.location
$pip02rslt = Test-AzureRmDnsAvailability -DomainNameLabel $vmpipname01 -Location $rg.location
if( $pip01rslt){$vmpipname01 = $hash + $vmpipname01}
$pip01 = New-AzureRmPublicIpAddress -Name $vmpipname01 -AllocationMethod Dynamic -DomainNameLabel $vmpipname01 -ResourceGroupName $rgname -Location $rg.Location
if( $pip02rslt){$vmpipname02 = $hash + $vmpipname02}
$pip02 = New-AzureRmPublicIpAddress -Name $vmpipname02 -AllocationMethod Dynamic -DomainNameLabel $vmpipname02 -ResourceGroupName $rgname -Location $rg.Location
#check the NIC, if not exist, create
$nics = Get-AzureRmNetworkInterface
$nic01rslt = $false
$nic02rslt = $false
$nic01name = $vmname + "01"
$nic02name = $vmname + "02"
foreach($nic in $nics){if($nic.name  $nic01name){$nic01rslt = $true;break}}
if($nic01rslt){$nic01name = $hash+$nic01name}else{$nic01name = $nic01name}
$nic01 = New-AzureRmNetworkInterface -Name $nic01name -ResourceGroupName $rgname -Location $rg.Location -SubnetId $vnet.Subnets[0].Id -PublicIpAddressId $pip01.Id
foreach($nic in $nics){if($nic.name  $nic02name){$nic02rslt = $true;break}}
if($nic02rslt){$nic02name = $hash+$nic02name}else{$nic02name = $nic02name}
$nic02 = New-AzureRmNetworkInterface -Name $nic02name -ResourceGroupName $rgname -Location $rg.Location -SubnetId $vnet.Subnets[1].Id -PublicIpAddressId $pip02.Id
#user login information
$pwd=ConvertTo-SecureString  $newpwd -AsPlainText -Force
$newvmcred=New-Object System.Management.Automation.PSCredential($newuser,$pwd)
#OSDiskName
$vmosname = $vmname+$hash+"osdisk"
#OSDisk storage url
$urls = $ImageURL.Split('/')
$saedpnt=$urls[2].Split('.')
$saname = $saedpnt[0]
$sa = Get-AzureRmStorageAccount -ResourceGroupName $rgname -Name $saname
$osDiskUrl = '{0}vhds/{1}-{2}.vhd'  $sa.PrimaryEndpoints.Blob.ToString(), "vm",$vmosname
#create the VM
$vm = New-AzureRmVMConfig -VMName $vmname -VMSize $vmsize -AvailabilitySetId $haset.Id
$vm = Set-AzureRmVMOperatingSystem -VM $vm -Linux -ComputerName $vmname -Credential $newvmcred
$vm = Add-AzureRmVMNetworkInterface -VM $vm -Primary -Id $nic01.Id
$vm = Add-AzureRmVMNetworkInterface -VM $vm -Id $nic02.Id
$vm = Set-AzureRmVMOSDisk -VM $vm -Name $vmosname -VhdUri $osDiskUrl -CreateOption FromImage -SourceImageUri $ImageURL -Linux
New-AzureRmVM -ResourceGroupName $rgname -Location "China East" -VM $vm
}
$rgname = "ciscorouter"
$vmname = "hwcisco01"
$hasetname = "hwcisco01"  #Please check the haset isn't avalible
$vmpipname = "hwcisco01pip"
$vnetname = "hwcisco01"
$subnetname1 = "vlan1"
$subnetname2 = "vlan2"
$vmsize = "Standard_D2"
$newpwd = "abc@12345678"
$newuser = "hengwei"
$ImageURL = "https://ciscorouter.blob.core.chinacloudapi.cn/image/CSR_3_16_4aS_CCO.vhd"
new-ciscocsr -rgname ciscorouter -vmname hwcisco -hasetname hwcisco -vmpipname hwciscopip -vnetname hwcisco -subnetname1 vlan1 -subnetname2 vlan2 -vmsize Standard_D2 -newuser hengwei -newpwd abc@12345678 -ImageURL https://ciscorouter.blob.core.chinacloudapi.cn/image/CSR_3_16_4aS_CCO.vhd -Verbose -Debug
  三、登录路由器



ssh hengwei@42.159.143.24
Connecting to 42.159.143.24:22...
Connection established.
To escape to local shell, press Ctrl+Alt+].
hwcisco#
hwcisco#conf t
Enter configuration commands, one per line. End with CNTL/Z.
hwcisco(config)#int g 2
hwcisco(config-if)#no shu
hwcisco(config-if)#ip add dhcp
hwcisco(config-if)#end
hwcisco#wr
Building configuration...
[OK]
hwcisco#term mon
*Apr 25 08:17:36.064: %DHCP-6-ADDRESS_ASSIGN: Interface GigabitEthernet2 assigned DHCP address 172.16.2.4, mask 255.255.255.0, hostname hwcisco
hwcisco#sh ip int brie
Interface IP-Address OK? Method Status Protocol
GigabitEthernet1 172.16.1.4 YES DHCP up up
GigabitEthernet2 172.16.2.4 YES DHCP up up

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-389773-1-1.html 上篇帖子: [Azure][PowerShell][ASM][01]Connect 下篇帖子: 无责任Windows Azure SDK .NET开发入门篇(一):开发前准备工作
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

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