05-15-2017 12:36 AM - edited 03-11-2019 12:43 AM
Hi Folks,
I am using ACS 5.4 and I would like to add device by using perl script. However, it did not work. Has anyone can help me?
#!/usr/bin/env perl -w
use strict;
use warnings;
use MIME::Base64;
use REST::Client;
use XML::Twig;
use XML::LibXML;
# Set the request parameters
my $host = 'https://xx.xx.xx.xx';
# Eg. User name="admin", Password="admin" for this code sample.
my $user = 'xxx;
my $pwd = 'xxx';
my $tag = <<END_XML;
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:devices xmlns:ns2="networkdevice.rest.mgmt.acs.nm.cisco.com">
<Device>
<description>Test Adding device</description>
<name>test device</name>
<groupInfo>
<groupName>All Device Types</groupName>
<groupType>Device Type</groupType>
</groupInfo>
<groupInfo>
<groupName>All Locations</groupName>
<groupType>Location</groupType>
</groupInfo>
<subnets>
<ipAddress>1.1.1.1</ipAddress>
<netMask>32</netMask>
</subnets>
<tacacsConnection>
<legacyTACACS>true</legacyTACACS>
<sharedSecret>helloAll</sharedSecret>
<singleConnect>false</singleConnect>
</tacacsConnection>
</Device>
</ns2:devices>
END_XML
my $request = XML::LibXML->load_xml(string=>$tag);
print $request;
$ENV{PERL_LWP_SSL_VERIFY_HOSTNAME}=0;
my $client = REST::Client->new(host => $host);
my $encoded_auth = encode_base64("$user:$pwd", '');
$client->getUseragent()->ssl_opts( SSL_verify_mode => 0 );
$client->PUT("/Rest/NetworkDevice/Device",$request,
{'Authorization' => "Basic $encoded_auth"});
Also, I have tested on Postman to put only xml to https://xx.xx.xx.xx/Rest/NetworkDevice/Device/ and it's not working too. I've got 400 Bad Request form the return. could you please advise how to add device using Rest API?
Here is the XML that I use
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:device xmlns:ns2="networkdevice.rest.mgmt.acs.nm.cisco.com">
<description>Test - Created by REST</description>
<name>TEST-REST-TEST</name>
<groupInfo>
<groupName>All Locations</groupName>
<groupType>Location</groupType>
</groupInfo>
<groupInfo>
<groupName>All Device Types</groupName>
<groupType>Device Type</groupType>
</groupInfo>
<groupInfo>
<groupName>All Locations</groupName>
<groupType>Location</groupType>
</groupInfo>
<subnets>
<ipAddress>1.1.1.1</ipAddress>
<netMask>32</netMask>
</subnets>
<tacacsConnection>
<legacyTACACS>true</legacyTACACS>
<sharedSecret>helloAll</sharedSecret>
<singleConnect>false</singleConnect>
</tacacsConnection>
</ns2:device>
Regards,
06-22-2017 01:02 PM
Correct format of XML is and it is working for me
<ns2:device xmlns:ns2='networkdevice.rest.mgmt.acs.nm.cisco.com'>
<description>Test Adding device</description>
<name>test device</name>
<groupInfo>
<groupName>All Device Types</groupName>
<groupType>Device Type</groupType>
</groupInfo>
<groupInfo>
<groupName>All Locations</groupName>
<groupType>Location</groupType>
</groupInfo>
<subnets>
<ipAddress>1.1.1.1</ipAddress>
<netMask>32</netMask>
</subnets>
<tacacsConnection>
<legacyTACACS>true</legacyTACACS>
<sharedSecret>helloAll</sharedSecret>
<singleConnect>false</singleConnect>
</tacacsConnection>
</ns2:device>
Find answers to your questions by entering keywords or phrases in the Search bar above. New here? Use these resources to familiarize yourself with the community: