Skip to content

vnc-ssh-tunnel.sh: Script to connect to server over ssh tunnel#67

Open
ckorn wants to merge 1 commit intoany1:masterfrom
ckorn:patch-1
Open

vnc-ssh-tunnel.sh: Script to connect to server over ssh tunnel#67
ckorn wants to merge 1 commit intoany1:masterfrom
ckorn:patch-1

Conversation

@ckorn
Copy link

@ckorn ckorn commented Aug 17, 2025

It is recommended the server only allowed connections from localhost. Therefore a ssh port forwarding connection to the local port on the server is required. This script opens a port forwarding connection to the server via ssh. wlvncc then connects to this local port. When the script terminates the ssh connection is closed.

Thanks to @any1 for the initial script.

Closes: #32

Copy link
Owner

@any1 any1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the style of commit messages used for this project and many others, people would normally write something like "Add script for connecting over ssh tunnel".

It's also customary to wrap git messages to 72 characters.


show_help()
{
echo "usage: $(basename $BASH_SOURCE) [-p|--ssh-port port] host vnc_port"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you know about heredoc? Instead of repeating the echos, you can do this:

cat <<END_OF_TEXT
usage: do-a-thing [options] <whatever>

This command does a thing with some options and whatnot.

Options:
   --stuff   Do stuff
   --help    Get help!
END_OF_TEXT

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Changed the code accordingly.

exit 1
}

free_port()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: better call this find_free_port

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Changed the code accordingly.

local start=49152
local range=5000
while true; do
local port=$[$start + ($RANDOM % $range)]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why randomise?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just c/p. Changed it to incrementaly.

local host="$1"
local port="$2"

if [ -z "$host" ]; then
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those ifs can be one

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Changed the code accordingly.

fi
shift; shift

master_file="$(mktemp -d)/wlvncc"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should be local.

The cleanup is missing for the temp directory.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

locals added.

The cleanup seems to be done automatically.
Also the trap code outputs "Control socket connect(/tmp/tmp.n89FXnzOHT/wlvncc): No such file or directory" so I added 2>/dev/null

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The cleanup seems to be done automatically

The ssh process might clean up the control socket, but I find it very unlikely that it would remove this directory for you. That would be very dangerous.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I can no longer reproduce this. Added rm -rf for the temp dir in the trap.

{
POSITIONAL_ARGS=()
local remote_port="22"
while [[ $# -gt 0 ]]; do
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better to use getopt(1) or no option parsing at all. Home brew solutions almost always behave in surprising ways. E.g. here you would expect --ssh-port=1337 to behave the same way as --ssh-port 1337.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Used getopt here. Took the example code from: /usr/share/doc/util-linux/getopt-example.bash

@ckorn
Copy link
Author

ckorn commented Aug 19, 2025

Hopefully the script is fine now.

My usual bash scripts are not longer than three lines but wanted to give it a try anyways ^^

Should I rebase my branch to delete the first commit?

Copy link
Owner

@any1 any1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, fixup commits should always be squashed into whatever commit it is that they're fixing.

local start=49152
local range=5000
local i=0
while true; do
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A simpler way to iterate over a range would look like this:

let start=49152
let range=5000
for ((i = start; i < start + range; ++i)); do


main()
{
TEMP=$(getopt -o 'hp:' --long 'help,ssh-port:' -n "$(basename $BASH_SOURCE)" -- "$@")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't option parsing overkill for this? Also, I believe that TEMP could really be a local.

Generally, I would just keep a shell script like this simple and just use environment variables for options. I.e. if you wanted a different port, you would have to call SSH_PORT=2200 vnc-ssh-tunnel.sh.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is what I wanted to ask anyway. Because with the current solution I can no longer forward options like -e raw to the actual process.

Will rewrite it with environment variables.

@ckorn ckorn force-pushed the patch-1 branch 2 times, most recently from 0623287 to 577543d Compare August 22, 2025 17:20
It is recommended the server only allowed connections from localhost.
Therefore a ssh port forwarding connection to the local port on the
server is required.

This script opens a port forwarding connection to the server via ssh.
wlvncc then connects to this local port.
When the script terminates the ssh connection is closed.

Closes: any1#32
@ckorn
Copy link
Author

ckorn commented Aug 22, 2025

Ok, finally I think to have fixed everything.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SSH tunnel handling like what tigervnc does

2 participants