Skip to content

Instantly share code, notes, and snippets.

@ariary
Last active December 1, 2021 09:32
Show Gist options
  • Save ariary/7bd45b954657ed841c5dc9937bd3dc26 to your computer and use it in GitHub Desktop.
Save ariary/7bd45b954657ed841c5dc9937bd3dc26 to your computer and use it in GitHub Desktop.
Compile go code with remote go installation (without local one)

Prerequisites

2 Machines:

  • Local: nc
  • Remote: nc and go

Remote go build 👽

On local you have your toto.go. You want to compile it w/o installing go (You are lazy or you don't want to install go for stealth/quickness reasons or whatever)

On Remote machine:

nc -l -p 1234 > dummy.go && go build -o dummy dummy.go && nc -w 3 [LOCAL_MACHINE_IP] 4444 < dummy

On Local machine:

nc -w 3 [REMOTE_MACHINE_IP] 1234 < ./toto.go && nc -l -p 4444 > toto

As a shortcut ✂️

On remote

wait for compilation demands: nc -l -p 1234 > dummy.go && go build -o dummy dummy.go && nc -w 3 [LOCAL_MACHINE_IP] 4444 < dummy

On local machine

1.Source this shell script:

#!/bin/bash

remotego(){
  FILENAME=$(echo $2 |cut -d "." -f -1)
  if [ "$1" == "build" ]; then
      nc -w 3 $REMOTE 1234 < $2 && nc -l -p 4444 > $FILENAME && chmod +x $FILENAME
  fi
  if [ "$1" == "run" ]; then
      nc -w 3 $REMOTE 1234 < $2 && nc -l -p 4444 > $FILENAME && chmod +x $FILENAME && ./$FILENAME && rm $FILENAME
  fi
}
  1. Configure $REMOTEenvar: export $REMOTE=[REMOTE_MACHINE_IP]
  2. Play with: remotego build toto.go or even remotego run toto.go

Remotego faster 🏃

Limits: run is not avaiblable + binary filename will be "dummy" (don't succeed to use xargs to exec a binary and rename my binary file)

Prerequisites

On both machine: tar,compress,uncompress

On Remote machine

nc -l -p 1234| uncompress -c | tar xvfp - |xargs go build -o dummy && tar cfp - dummy | compress -c | nc -w 3 [LOCAL_MACHINE_IP] 4444

On Local machine

#!/bin/bash

remotego(){
  FILENAME=$(echo $2 |cut -d "." -f -1)
  if [ "$1" == "build" ]; then
      tar cfp - $2 | compress -c | nc -w 3 $REMOTE 1234 && nc -l -p 4444| uncompress -c | tar xvfp -
  fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment