10 Eylül 2015 Perşembe

How to write VNC server (Raspberry Pi) startup and VNC client script



   In this post, I'm going to share  how to write a  VNC server start up script and client script. Before you continue with this tutorial, please make sure that your VNC server and client is working.You can check how do I do it in my previous post here.

   What will be accomplish by running this script on your VNC server (Raspberry Pi) is your Pi will automatically run at boot time.You can straight away  log in to your Pi from your system (client) once it is booted  up.Client side script is optional.I do it because I'm too lazy to remember the setting and need more space in my brain :-).

   I  used "case " statement for control structure on both server and client side. I use text editor on the client side and nano editor on server side to edit the script. You can download the full script at the end of this tutorial. Below are the steps I took to do this script .

1.Client

    I save this script in a folder that I normally put my script in .Visit my previous post on how do I do it . I name this script as "pi" .


Pi script piece by piece
------------------------
1.1) The script start off like normal script header

#!/bin/bash
#Title : Client VNC viewer login
#Author :Nerverwreck
#Website :http://www.geckogeeky.blogspot.com
#Date :December 2013
#Rev :0.1


1.2)It continue to the main content of the script .This script will only have 3 case to be select from . Once the user choose to select the case, it will automatically execute the following bash command.To execute this script,you need to type on terminal " pi [-selection ] ".The "$1" sign will take the input directly from the teminal.

If the user enter command that is not in the case, the "*" sign will execute its command by stating how to use the script.

case "$1" in
    -ping)
    echo "Ping to Pi VNC Server"
    ping 10.42.0.68                             # 10.42.0.68 is my Pi IP replace with your server IP
    ;;

    -ssh)   
    echo "SSH to Pi VNC Server"
    ssh 10.42.0.68 -l pi
    ;;

    -vnc)
    echo "Remote login to Pi "
    vncviewer 10.42.0.68:1 -geometry 1280x800  # use geometry to define your display
    ;;

    *)
    echo "`basename $0`:usage:[-ping][-ssh][-vnc]"
    exit 1
    ;;
esac
exit 0




2.Server

   On server part, you need to store the script in "/etc/init.d"  .This folder consist of file that will be execute during the boot time . I name this script as "autovnc"

pi@raspberrypi ~ $ cd /etc/init.d



Autovnc script piece by piece
---------------------------------
2.1) start off as a normal script header


#!/bin/bash
#/etc/init.d/autovnc
#Author :N3rv3wreck
#Website :htttp://geckogeeky.blogspot.com
#Date :December 2013
#Rev 0.1

2.2) For Debian base Linux distro, we must include in this header as a part of boot up script. If you miss this part on the script, It will pop up 'LSB missing 'warning when you want to configure it to run at boot up. For more information, you can visit here .


### BEGIN INIT INFO
# Provides:          Auto VNC server startup at boot
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start VNC server daemon at boot time
# Description:       Enable service provided by daemon.
### END INIT INFO

2.3)Changing the folder . to where you install the VNC server program . in my script i wrote as

VNCUSER='pi'
eval cd ~$VNCUSER

eval command will takes a string as its argument, and evaluates it as if you'd typed that string on a command line.

    You can use other method in doing the same job as above. The idea of both line above is finding the location of  ".vnc" file. I installed the program in my home folder .In this case, I set my Pi username  as "pi" .You will have the same location as me if you follow my tutorial . You can use below command as a substitution of above two line.

cd /home/pi

2.4) Main content of the script. Basically the script idea is similar to what we have on client side. To execute the file  we need to run as "./autovnc [selection] " . In autovnc, there is "start" and "stop"  selection. By running "start" command, it will start the server and enabling it at display :1 with the screen size 1280x600 .

   The server will be kill if we choose  the  "stop"  case.

case "$1" in
        start)
        echo "Waddap..Im starting the VNC Servo!"
         su $VNCUSER -c  '/usr/bin/vncserver :1 -geometry 1280x800'
        ;;

        stop)
        echo "Hey..Im gonna stop the VNC Servo Yo!"
         /usr/bin/vncserver -kill :1
        ;;

        *)
        echo "`basename $0`:usage:[start][stop]"
        exit 1
        ;;
esac
exit 0

If you use " cd/home/pi" in changing the folder, you can substitute
su $VNCUSER -c  '/usr/bin/vncserver :1 -geometry 1280x800'
with
su pi  -c '/usr/bin/vncserver :1 -geometry 1280x800'   


3.Testing the script
 After finish configuring, you can try and run the command  by using"./autovnc  start" or "./autovnc stop" command.

Testing VNC server script 


4.Update the script to be include during the boot time .
 pi@raspberrypi /etc/init.d $ sudo update-rc.d autovnc defaults
 
success configuring init file


reboot the server.

pi@raspberrypi /etc/init.d $ sudo reboot

5. You can now then try to log in to your  pi from you client.

shark_attack@Positive-Space:~$ pi -vnc

VNC to your server

 What do you think about this tutorial?To download the full script,please click here  . Please subscribe this page and leave comment in the box below

  Visit my other post on How to use voice to login your remote server and do other basic system control stuff. Click here .

.

Hiç yorum yok:

Yorum Gönder