Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SMB Client Commands Through Shell Script

Tags:

shell

unix

smb

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?

like image 278
Dustin Cook Avatar asked Feb 24 '15 14:02

Dustin Cook


People also ask

What is the function of the SMB client command?

The smbclient utility is the Swiss Army knife of Linux-to-NT tools. This command lets you send messages to workstations, display browse lists and connect to SMB shares.

How do I run a Smbclient command in Linux?

Using smbclient Basic use is fairly straightforward: type smbclient , followed by a NetBIOS name and share name in the form // SERVER / SHARE . The result is a prompt for a password followed by smbclient's own prompt. You can then type FTP-style commands, such as dir, get, put, and exit.

What is the command we can use within the SMB Shell to download the files we find?

smbget is a simple utility with wget-like semantics, that can download files from SMB servers. You can specify the files you would like to download on the command-line. The files should be in the smb-URL standard, e.g. use smb://host/share/file for the UNC path \\\\HOST\\SHARE\\file.


2 Answers

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.

like image 198
Dustin Cook Avatar answered Sep 21 '22 23:09

Dustin Cook


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.

like image 34
Calchas Avatar answered Sep 17 '22 23:09

Calchas