It is been a while…. to figure it out due to I am not doing script and programming for the living.
Few issues I found out with powershell script to save Cisco configuration automatically. I am pretty sure below script is not the only and best way. However it works and useful for me.
1. Error message "Line has invalid autocommand" from Cisco IOS. Cisco IOS won’t work with plink.exe’s batch precess
2. Need proper amount of "exit" command in batch file to finish SSH session properly. (see example)
Also, check other way to send Cisco CLI thru SSH "PowerShell: How to use SSH.NET Library for Cisco on Windows 7"
1. Basic script example.
$User = "John"
$pw = "ipBalance"
$IP = "192.168.77.11"
# Defining the plink command : 1. the cmd.exe call, 2. the command in cmd as an argument.
# Output file named with IP
$install_cmd = "cmd.exe"
$install_args = “/c `"c:\tools\plink.exe -ssh -v -l $User -pw $pw $IP -batch < c:\tools\commands.txt >> c:\tools\$IP`.txt`""
#Execute command and wait for exit
$PlinkCMD = [System.Diagnostics.Process]::Start("$install_cmd","$install_args")
$PlinkCMD.WaitForExit()
* path for plink.exe, output file and commands.txt is c:\tools in this case.
Batch file : commands.txt
terminal length 0 <—- avoid display length limit
show run
! <— ‘enter’ effect
show arp
:
:
exit
exit <—- Important for avoid hang.
2. Script with Cisco Device IP list in cvs file.
$User = "John"
$pw = "ipBalance"
# Bring Cisco Device IP from *.csv file, Cisco-IP-list.csv.
$DeviceList = Import-Csv c:\tools\Cisco-IP-list.csv
foreach ($IP_add in $Devicelist)
{
$IP = $IP_add.IPAddress; #IP is IP address
# Defining the plink command : 1. the cmd.exe call, 2. the command in cmd as an argument
$install_cmd = "cmd.exe"
$install_args = “/c `"c:\tools\plink.exe -ssh -v -l $User -pw $pw $IP -batch < c:\tools\commands.txt >> c:\tools\$IP`.txt`""
#Execute command and wait for exit
$PlinkCMD = [System.Diagnostics.Process]::Start("$install_cmd","$install_args")
$PlinkCMD.WaitForExit()
}
Cisco-IP-list.csv
IPAddress
192.168.1.58
192.168.7.4
10.10.1.2
:
Batch file : commands.txt
terminal length 0 <—- avoid display length limit
show run
! <— ‘enter’ effect
show arp
:
:
exit
exit <—- Important for avoid hang.
3. Script with Cisco Device IP in sequence block.
$User = "John"
$pw = "ipBalance"
$IP_node = 1
while ( $IP_node -le 254 )
{
# Defining the plink command : 1. the cmd.exe call, 2. the command in cmd as an argument
$install_cmd = "cmd.exe"
$install_args = “/c `"c:\tools\plink.exe -ssh -v -l $User -pw $pw 10.10.1.$IP_node -batch < c:\tools\commands.txt >> c:\tools\10.10.1.$IP_node`.txt`""
#Execute command and wait for exit
$PlinkCMD = [System.Diagnostics.Process]::Start("$install_cmd","$install_args")
$PlinkCMD.WaitForExit()
$IP_node = $IP_node + 1
}
Batch file : commands.txt
terminal length 0 <—- avoid display length limit
show run
! <— ‘enter’ effect
show arp
:
:
exit
exit <—- Important for avoid hang.
4. Script sends Cisco commands over SSH.
Function Invoke-SSH
{
<#
Author: Robin Malik
Source: Modified from: http://www.zerrouki.com/invoke-ssh/
#>
Param($hostname,$username,$password,$commandArray,$plinkAndPath,$connectOnceToAcceptHostKey = $false)
$target = $username + ‘@’ + $hostname
$plinkoptions = "-ssh $target -pw $password"
# On first connect to a host, plink will prompt you to accept the remote host key.
# This section will login and accept the host key then logout:
if($ConnectOnceToAcceptHostKey)
{
$plinkCommand = [string]::Format(‘echo y | & "{0}" {1} exit’, $plinkAndPath, $plinkoptions )
$msg = Invoke-Expression $plinkCommand
}
# Build the SSH Command by looping through the passed value(s). Append exit in order to logout:
$commandArray += "exit"
$commandArray | % { $remoteCommand += [string]::Format(‘{0}; ‘, $_) }
# Format the command to pass to plink:
$plinkCommand = [string]::Format(‘& "{0}" {1} "{2}"’, $plinkAndPath, $plinkoptions , $remoteCommand)
# Execute the command and display the output:
$msg = Invoke-Expression $plinkCommand
Write-Output $msg
}
$plinkAndPath = "C:\tools\plink.exe"
$username = "AdminUser"
$password = "P@ssw0rd"
$hostname = "x.x.x.x"
# Commands to execute:
$Commands = @()
$Commands += "show arp"
$Commands += "show ip route"
$Commands += "end"
Invoke-SSH -username $username -hostname $hostname -password $password -plinkAndPath $plinkAndPath -commandArray $Commands -connectOnceToAcceptHostKey $true > C:\MyScripts\output.txt
Also, check other way to send Cisco CLI thru SSH "PowerShell: How to use SSH.NET Library for Cisco on Windows 7"