Skip to content

Bash script to toggle samba on and off (and a button KDE)

by mat on April 13th, 2010

I like using samba as a password-less method for people on a network to share and access files on my computer. This works well on a network you trust like a small home network or a small private network, however on different networks restricted access is preferred.

For me this problem is solved by simply having a button to turn samba on and off. This will also have the side effect that you will not be able to access anyone else’s shares but is sufficient for my needs.

Bash script

#!/bin/bash
smb=`sudo /etc/init.d/samba status | grep running | wc -l`
if [ $smb -gt 0 ]
then
	# Stop samba
	sudo /etc/init.d/samba stop
else
	# Start samba
	sudo /etc/init.d/samba restart
fi

Where username is replaced with your username.

The script simply checks the status of samba to see if it is running, if it is then it is killed otherwise it is started. kdialog is used to display a notification of which toggle state we are in.

Adding a notification (KDE)

#!/bin/bash
smb=`sudo /etc/init.d/samba status | grep running | wc -l`
if [ $smb -gt 0 ]
then
	# Stop samba
	sudo /etc/init.d/samba stop
	sudo -u username kdialog --passivepopup 'Samba off' 3
else
	# Start samba
	sudo /etc/init.d/samba restart
	sudo -u username kdialog --passivepopup 'Samba on' 3
fi

We run kdialog through `sudo -u username` because when the script is run with sudo the current user will be root, and using this will cause kdialog to display an ugly notification.

For an example of this try running `sudo kdialog “ugly message” 3`

kdialog run as root showing an ugly notification

kdialog run as root showing an ugly notification

As opposed to `sudo -u username kdialog –passivepopup ‘lovly message’ 3`

kdialog run as user showing an pretty notification

kdialog run as user showing an pretty notification

Creating an icon/button (KDE)
You can then turn this into a button you can simply click my creating a desktop file. In KDE Right click > new > link to application. Then fill in the application tab with the information as in the following image:

Samba toggle desktop button configuration

Samba toggle desktop button configuration

In the General tab you can give the button a name and choose the icon of your choice, click OK, and now you can drag your button to where ever you want it (taskbar, desktop, panel etc.) Then you can simple push the button to toggle samba on and off.

One Comment

Trackbacks & Pingbacks

  1. Installation Thunderbird 3 Ubuntu 9.10 | PSchwan

Leave a Reply

Note: I am currently writing my thesis so probably wont have time to reply to your comment
Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS