前面通过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 -le 4; $i++){
$j = (97..122) | Get-Random -Count 1 | % {[char]$_}
$hash = $hash + $j
}
for ($i = 0; $i -le 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 -eq $rgname){$rgrslt = $true;break}}
if(-not $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 -match $hasetname){
if($haset.ResourceGroupName -match $rgname){break;}
else{write-host "Please change another haset name";exit;}
}
}
if(-not $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 -eq $vnetname){$vnetrslt = $true;break}}
if(-not $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 + ""
$vmpipname02 = $vmpipname + ""
$pip01rslt = Test-AzureRmDnsAvailability -DomainNameLabel $vmpipname01 -Location $rg.location
$pip02rslt = Test-AzureRmDnsAvailability -DomainNameLabel $vmpipname01 -Location $rg.location
if(-not $pip01rslt){$vmpipname01 = $hash + $vmpipname01}
$pip01 = New-AzureRmPublicIpAddress -Name $vmpipname01 -AllocationMethod Dynamic -DomainNameLabel $vmpipname01 -ResourceGroupName $rgname -Location $rg.Location
if(-not $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 + ""
$nic02name = $vmname + ""
foreach($nic in $nics){if($nic.name -eq $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 -eq $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' -f $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:...
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
hwcisco(config-if)#no shu
hwcisco(config-if)#ip add dhcp
hwcisco(config-if)#end
hwcisco#wr
Building configuration...
[OK]
hwcisco#term mon
*Apr ::36.064: %DHCP--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

在Azure上通过Powershell创建多Interface的Cisco CSR路由器的更多相关文章

  1. 用Json Template在Azure上创建Cisco CSR路由器

    Azure的ARM模式可以通过Json的模板创建VM.本文以Cisco的CSR的image为例,介绍如何用Json的创建VM. 一.Cisco CSR的Image 首先把Cisco CSR的image ...

  2. Azure上采用Powershell从已有的VHD创建VM

    刚刚的一篇Blog采用Json Template的方式从已有的VHD创建了一台新的VM.由于Json Template封装的比较好,可以改的内容不多. 下面将介绍通过用Powershell来从已有的V ...

  3. 在Azure上实现Linux Server故障转移

    要充分利用公有云的弹性扩展和高可用, 首先要在应用系统层面支持横向扩展(scale out),这个说起来很容易,或者说对新开发的应用系统而言已经成为标配.但是对已有的.老旧的应用系统来说,这就比较困难 ...

  4. Azure PowerShell (5) 使用Azure PowerShell创建简单的Azure虚拟机和Linux虚拟机

    <Windows Azure Platform 系列文章目录> 本文介绍的是国外的Azure Global.如果是国内由世纪互联运维的Azure China,请参考这篇文档: Azure ...

  5. [New Portal]Windows Azure Virtual Machine (16) 使用Azure PowerShell创建Azure Virtual Machine

    <Windows Azure Platform 系列文章目录> 注:本章内容和之前的[New Portal]Windows Azure Virtual Machine (12) 在本地制作 ...

  6. Azure 基础:使用 powershell 创建虚拟网络

    什么是虚拟网络 虚拟网络是您的网络在 Azure 云上的表示形式.您可以完全控制虚拟网络的 IP 地址.DNS 的设置.安全策略和路由表.您还可以更进一步,把虚拟网络划分为多个子网.然后用它们连接您的 ...

  7. Azure 基础:使用 powershell 创建虚拟机

    在进行与 azure 相关的自动化过程中,创建虚拟主机是避不开的操作.由于系统本身的复杂性,很难用一两条简单的命令完成虚拟主机的创建.所以专门写一篇文章来记录使用 PowerShell 在 azure ...

  8. Azure VMSS ---- PowerShell创建自定义镜像的VMSS集群

    前面一篇文章介绍了如何用PowerShell创建标准镜像的VMSS集群.http://www.cnblogs.com/hengwei/p/7391178.html 本文将介绍,如何用PowerShel ...

  9. Azure VMSS ---- PowerShell创建标准镜像的VMSS集群

    VMSS的创建可以采用Portal.Powershell.Azure CLI或者Template. 但目前Portal创建有很多限制,本文将介绍如何用PowerShell来创建VMSS的集群. 具体的 ...

随机推荐

  1. winter 2018 02 01 关于模运算的一道题

    题目:给出一个正整数n,问是否存在x,满足条件2^x mod n=1,如果存在,求出x的最小值. 分析:1.若给出的n是1,则肯定不存在这样的x;     2.若给出的是偶数,2的次幂取余一个偶数得到 ...

  2. Redis 高级实用特性

    1.安全性 设置客户端连接后进行任何其他操作前先验证密码. 因为Redis速度相当快,所以在一台比较好的服务器下,一个外部用户可以在一秒钟进行150K次的密码尝试,这意味着需要指定一个非常强大的密码来 ...

  3. EXP-00008:遇到ORACLE错误904问题

    案例情景--在一次Oracle 数据库导出时: C:\Documents and Settings\Administrator>exp lsxy/lsxy@lsxy_db file=E:\lsx ...

  4. Linux服务器注意事项

    1.在Linux新建一个tomcat目录,执行里面的文件运行的时候 会出现权限不足的提示?解决办法:这是因为新建的文件夹,对于可执行脚本,必须先授权,进入bin目录后,执行命令  chmod 764 ...

  5. gitblit搭建git服务器

    如果你的公司使用git作为版本管理工具,那么对gitblit应该也不会陌生.gitblit是一个开源的git服务器java实现,一般情况下gitblit都是由别人已经搭建好你直接使用就行了,除非你就是 ...

  6. 如何隐藏tomcat命令窗口

    有两种方法: 一.修改tomcat中的文件参数,达到隐藏目的: 引用:TOMCAT_HOME\bin\setclasspath.bat 在文件的底部找到以下内容: set _RUNJAVA=" ...

  7. Myeclipse中Tomcat的两种部署方式

    一.在Myeclipse软件中部署 1. 在Myeclipse中,创建好工程后,在Myeclipse菜单栏中选择 Windows -> Preferences -> Myeclipse - ...

  8. [BZOJ4730][清华集训2016][UOJ266] Alice和Bob又在玩游戏

    题意:俩智障又在玩游戏.规则如下: 给定n个点,m条无向边(m<=n-1),保证无环,对于每一个联通块,编号最小的为它们的根(也就是形成了一片这样的森林),每次可以选择一个点,将其本身与其祖先全 ...

  9. 键盘高级操作技巧【TLCL】

    Ctrl-a     移动光标到行首. Ctrl-e     移动光标到行尾. Ctrl-f     光标前移一个字符:和右箭头作用一样. Ctrl-b     光标后移一个字符:和左箭头作用一样. ...

  10. PAT1028. List Sorting (25)

    id用int,避免了id的strcmp,不然用string就超时. #include <iostream> #include <vector> #include <alg ...