Python simple HTTP server

One of my friends presented this nice tool. Very useful if you want to share a folder but is not interested in running a full HTTP server:

python -m SimpleHTTPServer 80

(where 80 is the http port and may be changed for any other port you prefer).

Checking if there is unauthorized DHCP servers in a LAN

I have found a very nice tool to probe for unauthorized DHCP servers in a network.  In Debian, you can use aptitude or apt-get to install it.

I hope it will be useful for more people.


#apt-get install dhcp_probe
#dhcp_probe -f -o /home/user/caps/dhcp.pcap eth0
note: starting, version 1.3.0
warn: received unexpected response on interface eth0 from BootP/DHCP server with IP source 192.168.0.1 (ether src 70:ca:9b:15:e1:9).
warn: received unexpected response on interface eth0 from BootP/DHCP server with IP source 192.168.0.1(ether src 70:ca:9b:15:e1:9).
warn: received unexpected response on interface eth0 from BootP/DHCP server with IP source 192.168.0.2 (ether src 70:ca:9b:15:e1:2).

samba auto mounting user folders according to groups

So, in my workplace, we were building a file server and the directory distribution would work according to the groups the user is part of.  So we built  shell scripts which would automatically mount the folders for the user.

Eg: If I am the John user and I am part of the groups “HR”, “IT and “marketing”. Our script will automatically fetch the groups from the John user and create the folders “HR”, “IT and “marketing” into his user directory.

To achieve this, we built the directory tree like this:

/srv/files/groups/infrastructure,HR,IT,marketing  (here is where all the files will actually be placed, according to the groups)

/srv/files/users/john,lisa,hans/”groups for each user” (here is where all the users will be placed after they remotely connect from their CIFS clients operating systems. There will be a folder for each user and inside each user folder will be created the mounting point to the groups, according to their respective groups).

In order to achieve this all, the following samba configuration (/etc/samba/smb.conf) was used:

[global]
workgroup = Organization
server string = %h server (Samba)
dns proxy = no
log file = /var/log/samba/log.%m
max log size = 1000
syslog = 0
panic action = /usr/share/samba/panic-action %d
hosts allow = 127.0.0.1 192.168.
hosts deny = ALL
os level = 100
security = user
encrypt passwords = true
passdb backend = tdbsam
obey pam restrictions = yes
unix password sync = yes
passwd program = /usr/bin/passwd %u
passwd chat = *Enter\snew\s*\spassword:* %n\n *Retype\snew\s*\spassword:* %n\n *password\supdated\ssuccessfully* .
pam password change = yes

[Org]
comment = Organization Foo Bar
path = /srv/files/users/%U
browseable = yes
guest ok = no
read only = no
force create mode = 0770
force directory mode = 0770
writable = yes
root preexec = bash /root/script_preexec_samba.sh %U %g

Additionally, the following script was used for the “root preexec” function from smb.conf, which means, will be executed when the user opens it’s session in the samba server:

#!/bin/bash
grupos=`groups $1 | cut -d: -f2`

if [ ! -d "/srv/files/users/$1" ]; then
 mkdir /srv/files/users/$1;
fi

umount /srv/files/users/$1/* >/dev/null

chown -R $1:$2 /srv/files/users/$1

for i in $grupos;
do
 if [ ! -d "/srv/files/users/$1/$i" ]; then
 mkdir /srv/files/users/$1/$i;
 fi

chgrp -R $i /srv/files/users/$1/$i;
 mount -o bind /srv/files/groups/$i /srv/files/users/$1/$i;
done

chmod -R 0770 /srv/files/users/$1/

exit 0

To do:

*To create a safe way to use the “postexec” function to automatically unmount and remove the user folders

 

Conclusions: This problem is certainly something more sysadmins had and this solution will certainly benefit many of them. If you find any issues, or if you haven’t understood some parts of this solution or even if you have found a better way to implement this solution, please do not hesitate to write me here in this post.

Rotational backup shell script

So I wrote this shell script for a client which needed a simple solution for daily backups. It will:

1. Check if the USB HDD is mounted, if it is not, it will mount it.

2. It will start writing it is own log file, will check for files older than 60 days, if it finds, it will delete it. Therefore the backups will never exceed the space in the external HDD (500G HDD).

I am using “tar.bz2″ for really good compression and setting the arguments to create files in the format “DAY-MONTH-YEAR”

It’s working in a server for a small office (trusted users) and all the files are shared in the same directory in a samba server (/files), which are backuped in a daily basis in a rotational system of back to 60 days older files, as I have already explained.

I know I  have to “clean it up” my code, especially removing all this static code and insert them into variables for a better reading AND eventually editing. I will work on it in the next days.

Well, I hope it will be useful to anyone on the net. Let me know if you have used it anywhere and if it is working for you (or not).  :)

#!/bin/bash
################# BACKUP SCRIPT #####################
chkmnt() {

cat /etc/mtab | grep /mnt/usb >/dev/null
 if [ "$?" -eq "0" ]; then
 echo mounted &&
 start_backup
 else
 date >> /home/backup/backup_hist.txt && echo "cannot be mounted, exiting..." >> /home/backup/backup_hist.txt
 exit
 fi
}

######################################

mount_backup() {

echo Mounting disk...

mount /mnt/usb
}

######################################

start_backup() {

echo Starting backup... &&

echo "::Starting backup::" >> /home/backup/backup_hist.txt && date >> /home/backup/backup_hist.txt

find /mnt/usb/ -mtime +60 -type f -exec rm -rf {} \; && tar -cvjf /mnt/usb/`date +%d-%m-%Y.tar.bz2` /files &&
umount /mnt/usb &&

echo "::End of backup::" >> /home/backup/backup_hist.txt
date >> /home/backup/backup_hist.txt
echo "-----------------------" >> /home/backup/backup_hist.txt &&

echo Backup concluded.

}

#####################################

mount_backup

chkmnt

automatically adding the unix users to the samba smb.conf shares

With the following script we can automatically add shares to the smb.conf (/etc/samba/smb.conf) based on the actual unix users:

#!/bin/bash

writesmb()
{

echo "[$f]" >> /etc/samba/smb.conf
echo " read only = no" >> /etc/samba/smb.conf
echo " valid users = $f" >> /etc/samba/smb.conf
echo " path = /home/$f" >> /etc/samba/smb.conf
echo " create mask = 0777" >> /etc/samba/smb.conf
echo " directory mask = 0777" >> /etc/samba/smb.conf
echo "" >> smb.conf
}

for f in $( ls /home ); do writesmb; done

Removing specific occurrences of files

Hi,

let’s say we have a Linux file server (samba) and the users insist in keep adding mp3 files into it. With the following command (and I suggest you adding the script to your crontab) we can remove all the occurrences of mp3/MP3 files everyday without doing it manually.

#find /home -type f -iname "*.mp3" -exec rm -fv {} \;

Where “find /home” will search recursively in your home directory, looking for “files” only (-type f) for the all the files containing the last characters as “mp3 or MP3″ and then it will remove all these results (rm -fv).

adding standard samba password to a user list

In my workplace we had about 55 users and we needed to set a standard password for all these users (of course we would let them modify it for a secret password after the first login). I am lazy, therefore I didn’t want to set a standard password one by one. With the code below it was possible to get each user listed in the file users.txt and set the standard password “pass” for each one of them.


for f in $( cat /root/users.txt ); do echo -ne "pass\npass\n" | smbpasswd -a -s $f; done

Of course, alternatively, I could use “ls /home” instead of “cat /root/users.txt”.

Follow

Get every new post delivered to your Inbox.

Join 70 other followers