Files
eve-ng/listimages
Pellegrino 1a8a82c3af checkpoint
2023-01-19 12:56:17 -05:00

51 lines
1.7 KiB
Bash

#!/bin/bash
#get all used images and their backing files
#list all unused qemu images in eve-ng
#will not include "wiped" nodes
#there cannot be any running nodes
#
#very "hacky"
#
#find /opt/unetlab/tmp -type f -name \*.qcow2 -exec qemu-img info {} \; > test
#set record delimiter to blank line
#sed -i s/\ \ \ \ corrupt:\ false// test
#sed -i s/\ \ \ \ corrupt:\ true// test
#parse w/ blank line for record separateor and newline for field separator
# 1 is image file (provisioned device)
# 2 is for the source
#awk 'BEGIN {FS="\n"; RS=""} {print $1 $6}' test > test2
#awk 'BEGIN {FS="\n"; RS=""} {print $6}' test > test2
#clean up leaving two files
#sed -i 's/image: //' test2
#sed -i 's/backing file://' test2
# list of all used images
#cat test2 | sort -k 2 -u
#cat test2 | sort -u
#
#get just the directory names for qemu (lists only used versions)
#cat test2 | cut -f 6 -d '/'
#
#cat test2 | sort -u | cut -f 6 -d '/' > test3
#
#list installed qemu versions
#ls -l /opt/unetlab/addons/qemu/ | cut -b 42- > test4
#grep -Fxv `ls -l /opt/unetlab/addons/qemu/ | cut -b 42-` test3
#grep -Fxv -f test3 test4
echo "Please enter username"
read USERNAME
echo "Please enter password"
read -s PASSWORD
rm list.json
curl -k -s -b /tmp/cookie -c /tmp/cookie -X POST -d '{"username":"'"$USERNAME"'","password":"'"$PASSWORD"'"}' https://127.0.0.1/api/auth/login
ls -R /opt/unetlab/labs/*.unl > labs_list
sed -i 's/\ /%20/g' labs_list
sed -i 's/\/opt\/unetlab//g' labs_list
while read i; do
curl -k -s -c /tmp/cookie -b /tmp/cookie -X GET -H 'Content-type: application/json' https://127.0.0.1/api$i/nodes >> list.json
#echo "$i";
done < labs_list
cat list.json | jq -r '.data' | grep image | sort -u