Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I'm in an office where samba shares are always different depending on the time, for instance at this time, jack and josh are connected, but jane is out of the office. Later, jane is here but jack has gone. You get the picture.
At the moment, I mount my samba shares manually from the terminal, because I use Gnome. I'd like a script to run each minute with a Cron job to check available samba shares on the network, and to mount or unmount them silently, depending if the share is available or not. Does some one have a script like this to create dynamically mount points under /mnt ? How could we do it? Thanks
oh no no no you don't want to do that. you want to use autofs, which does exactly what you want... mounting on demand. centos does contain a script to mount smb shares, but afaik it's actually horribly horribly borked, unliek the nfs one which always works fine. what i'd suggest is just defining a few specific entries for autofs, rather than the cleverer approach the defective scripts in /etc/ try to use.
Last edited by acid_kewpie; 02-13-2007 at 06:50 AM.
You need an auto.master and auto.<service_name> file, usually in /etc/. Both files should be executaable.
Here's mine, from my Fedora system:
Code:
$ cat /etc/auto.master | grep -v ^#
/smb /etc/auto.cifs --timeout=0
+auto.master
$ cat /etc/auto.cifs | grep -v ^#
#!/bin/bash
key="$1"
credfile="/etc/auto.smb.$key"
mountopts="-fstype=cifs,file_mode=0775,dir_mode=0775,uid=$USER,gid=smb"
smbclientopts=""
SMBCLIENT=$(whereis smbclient | cut -f 2 -d " ")
if ! [ -x $SMBCLIENT ]
then
echo No cmbclient executable found. Have you installed samba? >2
exit 1
fi
if [ -r "$credfile" ]
then
mountopts=$mountopts",credentials=$credfile"
smbclientopts="-A "$credfile
else
smbclientopts="-N"
fi
$SMBCLIENT $smbclientopts -gL $key\
| awk -v key="$key" -v opts="$mountopts" -F'|' -- '
BEGIN { ORS=""; first=1 }
/^Disk[^$]*$/ { if (first) { print opts; first=0 };
sub(/ /, "\\ ", $2);
if (length($2)==1) sub(/ ^/, "", $2);
print " \\\n\t /" tolower($2), "://" key "/" $2 }
END { if (!first) print "\n"; else exit 1 }
'
This scrip mounts the shares under a root directory, /smb visable to everyone connected to the Linux system. (Most useres are using are running Linux on a laptop, with remote login disabled, so that no problem for us.) In a multi-user system, a mount point in each user's home directory would probably make more sense.
Notice that the connection script also looks for a "credentials" file containing the remote login information. Since my systems are open to everyone on the local net, I put them (the credentials) in /etc, but a better solution would be to look in, say, ~/.smbpasswrd for the user's credentials.
The autofs mounts the shares when they are first needed. The timeout=0 setting, above, keeps them mounted "forever," but other timeouts can be specified (in minutes).
Here's a script that could be called from a user's login to connect for them :
(The stuff before the last line is just to make sure the connection - to my AP, "T.S.S." - is established, since most users have laptops using WiFi, and they may not have established a connection when the script is run, or may be connected to a different AP.)
yeah, it's the awk statements in the script there that have always been totally screwed up. as above, if that is the case, i would strongly suggest just adding manual shares yourself, instead of this automated approach.
yeah, it's the awk statements in the script there that have always been totally screwed up. as above, if that is the case, i would strongly suggest just adding manual shares yourself, instead of this automated approach.
Chris, that awk code works fine for me. It's easy enough to test it -- just run it from a command line, piping the output from smbclient into the program. You'll notice that I've changed the code slightly to force all the generated share names to lower case, and to skip all the NT-based default share names -- the ones ending in dollar signs.
<edit>
As an alternative to the script, run it (the script) by hand, capture the output to a file, and write a bash script to cat the file when called. Then you can "tweak" the file to your hearts content before it's used, and, of course, have a different file for each user. The point in the script is to mount all shares available from $key, so "tweaking" might be desirable in some cases.
The point of the script is just to get the automount information into the correct format.
</edit>
Last edited by PTrenholme; 02-13-2007 at 09:36 AM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.