[Solved] Printing from windows guest through the host ssh tunnel.

Discussions about using Windows guests in VirtualBox.
Post Reply
kraf101
Posts: 15
Joined: 31. Aug 2010, 16:36
Primary OS: Ubuntu other
VBox Version: PUEL
Guest OSses: win xp

[Solved] Printing from windows guest through the host ssh tunnel.

Post by kraf101 »

Hi friends.

I can open reversed ssh tunnel from my local (ubuntu) to server (ubuntu) which is VirtualBox's host of windows guest (ssh/rdp).

Code: Select all

ssh -g -R 1631:127.0.0.1:631 -N -f -l <user> <server>
then i can print from sever to local, i.e.

Code: Select all

lpr -H 127.0.0.1:1631 -P Brother_MFC file.pdf
Is there a way to configure printer in windows guest, to use this host's tunnel?

Thanks
Last edited by kraf101 on 12. Jul 2019, 01:31, edited 1 time in total.
scottgus1
Site Moderator
Posts: 20965
Joined: 30. Dec 2009, 20:14
Primary OS: MS Windows 10
VBox Version: PUEL
Guest OSses: Windows, Linux

Re: Printing from windows guest through the host ssh tunnel.

Post by scottgus1 »

I have only seen Windows accessing printers through the normal file & printer sharing over a network. If you have a network between your Ubuntu & your Windows guest, you should be able to do anything with the Windows guest that you could do with a real Windows PC attached to a network.

Take Virtualbox out of the equation for a minute and try to solve this problem as if you have two or three PCs side by side with a network between them. Once you have the solution for real PCs, apply the solution to the Virtualbox guest. FWIW, Virtualbox's Bridged network puts the guest in the same LAN as the host, as long as the host is wired Ethernet. (Bridging to a wifi adapter might or might not work.)
kraf101
Posts: 15
Joined: 31. Aug 2010, 16:36
Primary OS: Ubuntu other
VBox Version: PUEL
Guest OSses: win xp

Re: Printing from windows guest through the host ssh tunnel.

Post by kraf101 »

I hope had figureout the solution, but not sure if gracefully.
The idea is, to create a folder on host, shared for guest plus proces in background which checks this folder. If any pdf file comes, it will be printed out to ssh tunnel, then removed.
Below working code made with inotify-tools package.

Code: Select all

sudo apt-get install inotify-tools
Let's name the folder "printorres" and ...
script "printerros".

Code: Select all

#!/bin/bash
inotifywait -m /home/.........../printorres -e create -e moved_to |
    while read path action file; do
        if [[ "${file^^}" =~ .*PDF$ ]]; then 
            # echo $file
            lpr -H 127.0.0.1:1631 -P Printer-Name /home/.........../printorres/$file
            rm /home/.........../printorres/$file
        fi
    done
If this proces is running we can print to offsite printer with "Print to PDF" command and place the printout to "printorres" folder. No matter if we print from host or guest desktop.

Now proposal of processing the ssh session from offsite computer connrcted to printer.

Code: Select all

#!/bin/bash

# Username and password
user="XXX"
pw=$(zenity --password) 

# Launching "printerros" script
sshpass -p $pw ssh $user@SERVER "nohup /................./printerros < /dev/null > /tmp/mylogfile 2>&1 &"  

# Open VNC/RDP and printing tunnels. Here VNC, for example
sshpass -p $pw ssh -L 5901:127.0.0.1:5901 -N -f $user@SERVER
sshpass -p $pw ssh -R 1631:127.0.0.1:631 -N -f $user@SERVER
# We can start headless VBox machine and exec VBoxManage commands

# Now open the client  - viewer. In this case VNC.
xtigervncviewer 127.0.0.1::5901

# When client is closed, let's kill "printerros" process.
# I've wrote additional script, because I cannot figureout how to pass 
# kill $(pidof inotifywait) trough ssh tunnel
sshpass -p $pw ssh $user@SERVER "/................./kill-printeros"

# Close ssh tunnels
kill $(pidof ssh)
Script kill-printeros

Code: Select all

#!/bin/bash
# Printerros can be killed by killing "inotifywait" process.
kill $(pidof inotifywait)
It looks like it works pretty good, but can I trust it will be reliable at real field?
So far, hope [SOLVED] ;)
Post Reply