Skip to content

Instantly share code, notes, and snippets.

@thiswillbeyourgithub
Last active November 17, 2025 08:14
Show Gist options
  • Select an option

  • Save thiswillbeyourgithub/53befc8223f37aa929b35c6ebf8e6365 to your computer and use it in GitHub Desktop.

Select an option

Save thiswillbeyourgithub/53befc8223f37aa929b35c6ebf8e6365 to your computer and use it in GitHub Desktop.
Zsh scripts for edgevpn to send and receive files
#!/usr/bin/zsh
edge_send() {
local name=$(echo $EDGEVPNTOKEN | sha256sum)
local name="edgevpn_file_${name:0:20}"
local pathoffile=$1
echo "Edge token:\n$EDGEVPNTOKEN\nName: $name\nPath: $pathoffile\n\n"
if [[ -z "$pathoffile" ]]
then
echo "Error: Must supply a path"
else
echo "Creating metadata..."
local metadata_file="metadata_${name}.json"
echo '{"filename": "'"$pathoffile"'", "timestamp": "'$(date -Iseconds)'"}' > "$metadata_file"
echo "Creating tar archive..."
if [[ -d "$pathoffile" ]]; then
echo "Sending directory: $pathoffile"
tar -czf "${name}.tar.gz" "$pathoffile" "$metadata_file"
else
echo "Sending file: $pathoffile"
tar -czf "${name}.tar.gz" "$pathoffile" "$metadata_file"
fi
echo "Cleaning up metadata file..."
rm "$metadata_file"
echo "Sending file..."
# edgevpn file-send --name "$name" --path "${name}.tar.gz"
start_time=$(date +%s)
expect -c "set timeout -1; spawn edgevpn file-send --name \"$name\" --path \"${name}.tar.gz\" ; expect \"(file $name) Done handling \" {puts \"File successfully sent\"; sleep 3; exit 0}"
end_time=$(date +%s)
duration=$((end_time - start_time))
echo "File transfer took $duration seconds."
echo "Cleaning up tar archive..."
rm "${name}.tar.gz"
echo "File transfer complete."
fi
}
edge_receive() {
local name=$(echo $EDGEVPNTOKEN | sha256sum)
local name="edgevpn_file_${name:0:20}"
local pathoffile="/tmp/edgevpn_transfer.$(date +'%Y%m%d_%H%M%S').tar.gz"
echo "Edge token:\n$EDGEVPNTOKEN\nName: $name\nPath: $pathoffile\n\n"
echo "Receiving file..."
while true; do
edgevpn file-receive --name "$name" --path "$pathoffile"
if [[ -s "$pathoffile" ]]; then
echo "File received successfully."
break
else
echo "Received file is empty. Deleting and retrying..."
rm "$pathoffile"
sleep 5 # Wait for 5 seconds before retrying
fi
done
echo "Extracting contents..."
local extract_dir="/tmp/extracted_$(basename "$pathoffile" .tar.gz)"
mkdir -p "$extract_dir"
tar -xzf "$pathoffile" -C "$extract_dir"
if [[ -f "$extract_dir/metadata_${name}.json" ]]; then
echo "Metadata found:"
cat "$extract_dir/metadata_${name}.json"
rm "$extract_dir/metadata_${name}.json"
else
echo "No metadata file found."
fi
echo "Moving extracted files to current directory..."
mv "$extract_dir"/* .
echo "Cleaning up temporary files..."
rm -r "$extract_dir"
rm "$pathoffile"
echo "File transfer and extraction complete."
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment