Bash function to ease smbclient usage

Quick and dirty: I find smbclient incredibly useful at the command line in Linux, but I can never remember how to put the command together. So I wrote a bash function to simplify it for me. After the function code, I’ll describe how to store your credentials safely so you don’t need to type them in each time.

Here it is:

sm ()
{
    # Samba client connection function
    # 2012-11-15 mike@diehn.net
    #   Simply command line file transfer connections tor
    #   windows systems from Linux.  You need smbclient.
    #
    # Gotta give at least a hostname
    [ -z "$1" ] && {
      echo "sm: usage: sm hostname [service]"
      return
    }
 
    # Putchyer own creds file in place, put the
    # pathname here, and prolly better do this
    #   chmod -R go-rwx # $HOME/.creds
    #
    #auth="-A $HOME/.creds/{REALM}/{username}"
 
    host=${1}
    nbname=${host##.*}
 
    # If no service given, list what's available, otherwise
    # connect to the service
    [   -z "$2" ] && cmd="-L $nbname" || cmd="//$nbname/${2}"
 
    # execute the command
    echo smbclient $auth -I $host $cmd
    smbclient $auth -I $host $cmd
 
}

Credential storage for use with smbclient and friends:

I create a $HOME/.creds directory and in it, I make a dir for each authentication realm and then in those, I make a file for each username. In the username file, I put this:

username = myusername
password = mypassword
domain   = MY_AD_DOMAIN_NAME

Enjoy!

Leave a Reply

You must be logged in to post a comment.