FortiEdgeCloud
Hosted cloud-based management platform for the Fortinet Secure LAN Edge (FortiSwitch and FortiAP), and FortiExtender 5G/LTE Gateways
sachitdas_FTNT
Article Id 367726
Description This article describes how to use Perl script to undeploy all FortiSwitches from FortiEdge Cloud Network.
Scope FortiEdge Cloud v24.4_0005.
Solution

In the current version, selecting multiple FortiSwitches and undeploying from a network is not supported.

Refer to the below screenshot:

 

Untitled.jpg

 

Selecting one FortiSwitch and undeploying it works, but if there are many FortiSwitches deployed in the network and you need to undeploy all of Them, a Perl script can be used.

 

For Windows, download and install the latest 'msi' file from this link https://strawberryperl.com/

 

Use the command 'perl -v' to check the Perl version:

 

perl version.jpg

 

A sample script is shown for reference, edit the script as per the requirements:-

 

my $server = "ca.fortiedge.forticloud.com";  --> ca is for Global/Canada domain. For Europe, replace 'ca' with 'eu'
my $username = 'email address'; --> enter FortiEdge login email address
my $password = 'password'; --> enter the password

my $url = "https://$server/api/v1/networks/".$apnwoid->{"Combined Default"}."/fsw/switch/switches/";  <----- Script will undeploy all FortiSwitches from 'Combined Default' network, replace 'Combined Default' with Network Name.

 

Copy and Paste the below script to a notepad file and save the file with pl extension, for example, 'fsw_undeploy.pl'.

 

#!/usr/bin/perl
use LWP::UserAgent;
use JSON;
use HTTP::Headers;
use Data::Dumper;


my $server = "ca.fortiedge.forticloud.com";
my $username = 'email address';
my $password = 'password';


print "===> Server: $server, username: $username, password: $password <===\n";

my $accesstoken ;

&generate_token();
print "Access token is $accesstoken\n";

 


################################ ADD and GET AP-network - 300 ##########################################

my $url = "https://$server/api/v1/networks/";
my $header = ['Content-Type' => 'application/json','Accept' => 'application/json','Authorization' => 'Bearer '.$accesstoken];
my $ua1 = LWP::UserAgent->new(ssl_opts => { verify_hostname => 0 });
print "\n URL -> $url";
my $req = HTTP::Request->new('GET', $url,$header);
my $response = $ua1->request($req);
my $message = $response->decoded_content;
my $hash_message = decode_json($message);
#print Dumper($hash_message);
my $apnwoid = {};
foreach (@{$hash_message->{result}}) {
next if($_->{name} eq '');
$apnwoid->{$_->{name}}=$_->{oid};
}

 


my $header = ['Content-Type' => 'application/json','Accept' => 'application/json','Authorization' => 'Bearer '.$accesstoken];
my $url = "https://$server/api/v1/networks/".$apnwoid->{"Combined Default"}."/fsw/switch/switches/";
my $ua1 = LWP::UserAgent->new(ssl_opts => { verify_hostname => 0 });


my $req = HTTP::Request->new('GET', $url,$header);
my $response = $ua1->request($req);
my $message1 = $response->decoded_content;
#print "$message1\n";
my $myHashRefDecoded = decode_json($message1);
my %myHashDecoded = %$myHashRefDecoded;
my $results = $myHashDecoded{'result'};
#print "$results \n";
foreach $data (@$results)
{
my $sn = $data->{sn};
#print "$sn\n";
my $url1 = "https://$server/api/v1/networks/".$apnwoid->{"Combined Default"}."/fsw/switch/switches/";
my $ua1 = LWP::UserAgent->new(ssl_opts => { verify_hostname => 0 });
my $json_hash = ["$sn"];
my $encoded_json = encode_json($json_hash);
print "\n URL -> $url, encoded_json -> $encoded_json";
my $req = HTTP::Request->new('DELETE', $url,$header,$encoded_json);
my $response = $ua1->request($req);
my $message1 = $response->decoded_content;
print "$message1\n";
sleep 1;
}

 

############################# To Generate Access token ###########################################

sub generate_token(){

my $ua = LWP::UserAgent->new(ssl_opts => { verify_hostname => 0 });
my $header = ['Content-Type' => 'application/json'];
my %rec_hash = ('userName'=>$username,'password'=>$password,'accountId'=>$username);
my $json = encode_json \%rec_hash;
my $url = "https://$server/api/v1/auth/";
my $req = HTTP::Request->new('POST', $url, $header, $json );
my $resp = $ua->request($req);
#print Dumper($resp);
if ($resp->is_success)
{
my $message = $resp->decoded_content;
print "Received reply: $message\n";
my $myHashRefDecoded = decode_json($message);
my %myHashDecoded = %$myHashRefDecoded;
$accesstoken = $myHashDecoded{'access_token'};
}
else {
print "HTTP POST error code: ", $resp->code, "\n";
print "HTTP POST error message: ", $resp->message, "\n";
}
}

=====================

 

Execute the script in Perl using the command 'perl fsw_undeploy.pl'.

 

perl execute.jpg