ptlab

Packet Tracer wrapper script for managing Wendell's config labs
Log | Files | Refs | README

ptlab (1007B)


      1 #!/bin/sh
      2 
      3 set -e
      4 
      5 log() {
      6 	echo "ptlab: $*" 1>&2
      7 }
      8 
      9 err() {
     10 	log "$@"
     11 	exit 1
     12 }
     13 
     14 usage() {
     15 	echo 'usage: ptlab [-lnkux] [lab]' 1>&2
     16 	exit 1
     17 }
     18 
     19 pt() {
     20 	packettracer "$@"
     21 }
     22 
     23 baseurl=https://files.certskills.com/virl
     24 labs="$HOME"/ptlabs
     25 
     26 update=false
     27 fetchonly=false
     28 local=false
     29 keep=false
     30 while test $# -gt 0
     31 do
     32 	case "$1" in
     33 	(-l) local=true ;;
     34 	(-n) fetchonly=true ;;
     35 	(-k) keep=true ;;
     36 	(-u) update=true ;;
     37 	(-x) set -x ;;
     38 	(--) shift; break ;;
     39 	(-*) usage ;;
     40 	(*) break ;;
     41 	esac
     42 	shift
     43 done
     44 
     45 if $local || test $# -eq 0; then
     46 	pt "$@"
     47 	exit
     48 fi
     49 
     50 lab="$(basename "$1")"
     51 file=clab"${lab#clab}".pkt
     52 
     53 path="$labs"/init/"$file"
     54 if $update || ! test -f "$path" 
     55 then
     56 	url="$baseurl"/"$file"
     57 	mkdir -p "$(dirname "$path")"
     58 	curl -SsLo "$path".tmp "$url" ||
     59 		err "Unable to fetch $url"
     60 	chmod a-w "$path".tmp
     61 	mv -f "$path".tmp "$path"
     62 fi
     63 
     64 $fetchonly &&
     65 	exit
     66 
     67 runpath="$labs"/run/"$file"
     68 mkdir -p "$labs"/run
     69 if ! $keep || ! test -e "$runpath"
     70 then
     71 	cp "$path" "$runpath"
     72 	chmod ug+w "$runpath"
     73 fi
     74 pt "$runpath"