proc CopyFileFromLinuxApiServer {args} { # Description # Using REST API to copy files from a Linux API server to local filesystem. # # Requirements # IxNetwork 8.50+ because the package IxTclNetwork comes with http, tls and json # that this proc needs. Otherwise, you need to install http, tls and json. # # Parameters # -apiServerIp: The Linux API server IP address # -sessionId: The session ID number # -apiKey: The unique user account api-key # -pathExtension: All files are located in a predefined path in the Linux API server. # In some situation like packet capturing, the captured file is located. # in deeper subdirectories. Provide the extended subdirectory without the file name. # You need to include the forward slash. # For example: /captures/packetCapture # -srcFile: The filename to copy from the Linux API server. # -dstFilePath: The path+filename in the local filesystem where you want to copy the file to. # # Example: # # package require http # package require json # package require tls # ::http::register https 443 ::tls::socket # # # Example: In Quick Test, use api to get the $resultPath and pass it in with the -linuxSrcFilePath parameter. # # This method applies to other tests where there should be an api to get the $resultPath. # CopyFileFromLinuxApiServer \ # -apiServerIp $apiServerIp \ # -sessionId 5 \ # -apiKey $apiKey \ # -linuxSrcFilePath $resultPath \ # -srcFile logFile.txt \ # -dstFilePath ./qtLogFile.txt # # # Example with pathExtension for packet capture # copyFileFromLinuxApiServer \ # -apiServerIp $linuxApiServerIp \ # -sessionId 7 \ # -apiKey e5d16d3258014241872990301abc \ # -pathExtension /captures/packetCapture \ # -srcFile port2_HW.cap \ # -dstFilePath ./port2_HW.cap set linuxSrcFilePath "None" set pathExtension "" set argIndex 0 while {$argIndex < [llength $args]} { set currentArg [lindex $args $argIndex] switch -exact -- $currentArg { -apiServerIp { set apiServerIp [lindex $args [expr $argIndex + 1]] incr argIndex 2 } -sessionId { set sessionId [lindex $args [expr $argIndex + 1]] incr argIndex 2 } -apiKey { set apiKey [lindex $args [expr $argIndex + 1]] incr argIndex 2 } -pathExtension { set pathExtension [lindex $args [expr $argIndex + 1]] incr argIndex 2 } -linuxSrcFilePath { set linuxSrcFilePath [lindex $args [expr $argIndex + 1]] incr argIndex 2 } -srcFile { set srcFile [lindex $args [expr $argIndex + 1]] incr argIndex 2 } -dstFilePath { set dstFilePath [lindex $args [expr $argIndex + 1]] incr argIndex 2 } } } if {$linuxSrcFilePath == "None"} { set response [::http::geturl https://$apiServerIp\/api/v1/sessions/$sessionId\/ixnetwork/files -headers [list "x-api-key" $apiKey] -binary True] set jsonData [::http::data $response] set json2Dict [::json::json2dict $jsonData] set linuxSrcFilePath [dict get $json2Dict absolute] puts "\nlinuxSrcFilePath: $linuxSrcFilePath" } set handle [::http::geturl "https://$apiServerIp/api/v1/sessions/$sessionId/ixnetwork/files?absolute=$linuxSrcFilePath$pathExtension&filename=$srcFile" -headers [list "x-api-key" $apiKey]] set infile [open $dstFilePath "wb"] puts $infile [::http::data $handle] close $infile }