Bash script to toggle samba on and off (and a button KDE)
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.
#!/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`
As opposed to `sudo -u username kdialog –passivepopup ‘lovly message’ 3`
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:
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.