Skip to content

Instantly share code, notes, and snippets.

@ChatchaiJ
Created March 16, 2020 11:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChatchaiJ/8d787c0b65f8dd127f8d6ba9d5bc9335 to your computer and use it in GitHub Desktop.
Save ChatchaiJ/8d787c0b65f8dd127f8d6ba9d5bc9335 to your computer and use it in GitHub Desktop.
Create script and passwd for runing vnc at remotehost
#!/bin/sh
# What need to be done
# 1. local binary script: ${REMOTE}-vnc
#
#| #!/bin/sh
#|
#| PORT=5998
#| tmux new ssh -L${PORT}:localhost:${PORT} pi-01 bin/vnc-at-${PORT} \; \
#| split -v "sleep 3; vncviewer -passwd $HOME/.vnc/pi-01-${PORT} localhost:${PORT}" \; \
#| detach
#
# 2. remote binary script: vnc-at-${PORT}
#
#| #!/bin/sh
#|
#| vncserver :98 -geometry 1600x900 -depth 24 -name vnc5998 -nevershared -localhost
#| tail -f $HOME/.vnc/pi-01:98.log
#
# 3. password file for vnc
#
# printf "$RWPASSWD\n$ROPASSWD\n" | vncpasswd -f > passwd
[ -z "$2" ] && echo "Usage: $0 hostname portno" && exit
HOSTNAME="$1"
PORTNO="$2"
D=`mktemp -d`
LSCR="$D/local-script"
RSCR="$D/remote-script"
PASS="$D/passwd"
L_SCRIPT="$HOME/bin/${HOSTNAME}-vnc"
R_SCRIPT="bin/vnc-at-${PORTNO}"
L_PASSWD="$HOME/.vnc/${HOSTNAME}-${PORTNO}"
R_PASSWD=".vnc/passwd"
# Here, we generated 'random' password for read/write access
# you might want to change the read-only access below
RWPASSWD=`date|md5sum|cut -c1-8`
ROPASSWD="READONLY"
printf "$RWPASSWD\n$ROPASSWD\n" | vncpasswd -f > $PASS
cat <<_EOT_ > $LSCR
#!/bin/sh
PORT=$PORTNO
tmux new ssh -L\${PORT}:localhost:\${PORT} $HOSTNAME bin/vnc-at-\${PORT} \\; \\
split -v "sleep 3; vncviewer -passwd \$HOME/.vnc/$HOSTNAME-\${PORT} localhost:\${PORT}" \\; \\
detach
_EOT_
DISP=`expr $PORTNO - 5900`
cat <<_EOT_ > $RSCR
#!/bin/sh
vncserver :$DISP -geometry 1600x900 -depth 24 -name vnc${PORTNO} -nevershared -localhost
tail -f \$HOME/.vnc/\`hostname\`:${DISP}.log
_EOT_
[ -f "$L_SCRIPT" ] && echo "$L_SCRIPT already exist" && exit
[ -f "$L_PASSWD" ] && echo "$L_PASSWD already exist" && exit
# Here, the part we need to run to actual create local/remote files
echo "## Please copy and paste the following commands to create the script and config"
echo cp -v $LSCR $L_SCRIPT
echo ssh $RSCR ${HOSTNAME} mkdir -p bin .vnc
echo scp $RSCR ${HOSTNAME}:$R_SCRIPT
echo cp -v $PASS $L_PASSWD
echo scp $PASS ${HOSTNAME}:$R_PASSWD
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment