Last active
September 12, 2016 10:48
-
-
Save fredhsu/4e1ea0c0fa24d4748aac to your computer and use it in GitHub Desktop.
eAPI from Powershell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$username = "admin" | |
$password = "admin" | |
$switchIp = "172.22.28.157" | |
#URL | |
$eApiUrl = "https://$switchIp/command-api" | |
# Command(s) we want to send | |
$cmds = @('show version') | |
# Create Object and convert to JSON | |
$params = @{version= 1;cmds= $cmds; format="json"} | |
$command = (New-Object PSObject | Add-Member -PassThru NoteProperty jsonrpc '2.0' | | |
Add-Member -PassThru NoteProperty method 'runCmds' | | |
Add-Member -PassThru NoteProperty params $params | | |
Add-Member -PassThru NoteProperty id '1') | ConvertTo-Json | |
# Should generate: '{"jsonrpc": "2.0", "method": "runCmds", "params":' + $params +', "id":1}' | |
# Create ASCII encoded version of the JSON string | |
$bytes = [System.Text.Encoding]::ASCII.GetBytes($command) | |
# Ignore Certificate | |
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true} | |
# Create a web request with the proper parameters and write the bytes | |
$web = [System.Net.WebRequest]::Create($eApiUrl) | |
$web.Method = "POST" | |
$web.ContentType = "application/json" | |
$web.Credentials = New-Object System.Net.NetworkCredential -ArgumentList $username, $password | |
$stream = $web.GetRequestStream() | |
$stream.Write($bytes, 0, $bytes.Length) | |
$stream.close() | |
# Get the response from the switch and convert back to an object | |
$reader = New-Object System.IO.StreamReader -ArgumentList $web.GetResponse().GetResponseStream() | |
$response = $reader.ReadToEnd() | ConvertFrom-Json | |
$reader.Close() | |
$response.result | |
Write-Host "Model is: " + $response.result.modelName |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment