Dustin Cook
Dustin Cook

Reputation: 1305

SMB Client Commands Through Shell Script

I have a shell script, which I am using to access the SMB Client:

#!/bin/bash
cd /home/username
smbclient //link/to/server$ password -W domain -U username
recurse
prompt
mput baclupfiles
exit

Right now, the script runs, accesses the server, and then asks for a manual input of the commands.

Can someone show me how to get the commands recurse, prompt, mput baclupfiles and exit commands to be run by the shell script please?

Upvotes: 9

Views: 32735

Answers (4)

thepappas
thepappas

Reputation: 81

smbclient //link/to/server$ password -W domain -U username -c "recurse;prompt;mput backupfiles"

I would comment to Calchas's answer which is the correct approach-but did not directly answer OP's question-but I am new and don't have the reputation to comment.

Note that the -c listed above is semicolon separated list of commands (as documented in other answers), thus adding recurse and prompt enables the mput to copy without prompting.

You may also consider using the -A flag to use a file (or a command that decrypts a file to pass to -A) to fully automate this script

smbclient //link/to/server$ password -A ~/.smbcred -c "recurse;prompt;mput backupfiles"

Where the file format is:

username = <username>
password = <password>
domain = <domain>
workgroup = <workgroup>

workgroup is optional, as is domain, but usually needed if not using a domain\username formatted username.

I suspect this post is WAY too late to be useful to this particular need, but maybe useful to other searchers, since this thread lead me to the more elegant answer through -c and semicolons.

Upvotes: 8

user4891857
user4891857

Reputation:

smbclient accepts the -c flag for this purpose.

 -c|--command command string
       command string is a semicolon-separated list of commands to be executed instead of
       prompting from stdin.
       -N is implied by -c.

       This is particularly useful in scripts and for printing stdin to the server, e.g.
       -c 'print -'.

For instance, you might run

$ smbclient -N \\\\Remote\\archive -c 'put /results/test-20170504.xz test-20170504.xz'

smbclient disconnects when it is finished executing the commands.

Upvotes: 13

Dustin Cook
Dustin Cook

Reputation: 1305

I worked out a solution to this, and sharing for future references.

#!/bin/bash
cd /home/username
smbclient //link/to/server$ password -W domain -U username << SMBCLIENTCOMMANDS
recurse
prompt
mput backupfiles
exit
SMBCLIENTCOMMANDS

This will enter the commands between the two SMBCLIENTCOMMANDS statements into the smb terminal.

Upvotes: 13

dft99
dft99

Reputation: 69

I would take a different approach using autofs with smb. Then you can eliminate the smbclient/ftp like approach and refactor your shell script to use other functions like rsync to move your files around. This way your credentials aren't stored in the script itself as well. You can bury them somewhere on your fs and make it read only by root an no one else.

Upvotes: 2

Related Questions