20

I am writing one script. it will run one linux machine. its targets includes linux, solaris and windows.

I know that i can use scp to copy files between *nix. but how about copying file from windows?

These windows are test machines, which can not be expected to have specific software installed, as the OS can be reinstalled often.

4 Answers 4

26

Use Samba.

Samba provides a file sharing service compatible with Windows.

You can share a directory (for example /srv/samba/sharename) on your Linux computer and access it from Windows like this: \\linuxservername\sharename. It doesn't need any extra software on the Windows side.

Samba is included in most Linux distributions. Install it using the package manager, for example:

  • Ubuntu: sudo apt-get install samba smbclient

  • SUSE: zypper samba

  • CentOS/RedHat: yum install samba

To configure Samba, see:

or search Google to find a guide for your distribution.

Note that you may need to configure your Linux firewall, in case you use one, to accept connections to ports 137/tcp, 138/tcp, 139/tcp and 445/tcp (see this and this).


In the description above the Linux computer is a Samba server and Windows mounts a share from it.

In your comment below you mention that you'd like to automate the transfer process. This can be achieved by reversing the roles so that Windows acts as the server and Linux connects to it using smbclient.

With smbclient you don't need to mount the Windows share at all. For example, to retrieve C:\Directory\file.txt and copy it to /tmp on your Linux computer do this:

smbclient '//windowsserver/c$' -c 'lcd /tmp; cd Directory; get file.txt' -U administrator%password

-c Command to execute. See man smbclient for details.

-U Username and password for accessing the share specified as username%password

Modify it to your needs and add it to your script.

4
  • @max, Thanks for your great answers. I know samba can work with Windows. But it still needs human being involvements when copying a file. As my script will run on one linux and the machine has installed samba, how can I let the one script do the copying work? It seems that I had better mount the Windows disk to Linux, then I can access it in Linux. Can samba do this?
    – Alex
    Mar 11, 2013 at 4:50
  • Yes, Samba can do this. Even better, you don't need to mount the Windows share at all. You can use smbclient to connect to the Windows share, copy a file and disconnect. I edited my answer, take a look at it.
    – jaume
    Mar 11, 2013 at 10:30
  • You're welcome, I'm glad I could help.
    – jaume
    Mar 11, 2013 at 13:27
  • 1
    Especially the -c option is worth using in simple cases. On my machine the password comes as a bare argument (without any option). And besides - I prefer to keep the credentials away from commandline readers (using ps or [h]top, for example), by calling -U $(cat .username) $(cat .password). Jun 18, 2014 at 14:47
3

I know you said you can't expect specific software to be installed, but there are SSH/SCP/SFTP clients for Windows which do not require any particular installation; only the executable being available. One that I keep turning to is PuTTY with its companion pscp and psftp tools, but I am certain that alternatives exist. pscp and psftp can be driven completely from the command line, and thus are well-suited for automation tasks. Both of them even have a -batch switch which is described as "disable all interactive prompts" and almost certainly can be leveraged.

Since you presumably already have a SSH/SCP/SFTP server installed and configured on the server, this avoids having to install any software on either host just for the purpose of copying those files.

Another upside might be the fact that PuTTY is open source under a permissive license, so if it doesn't do what you want straight out of the box, it should be relatively easy to make it act the way you prefer.

1
  • 1
    Thank you for your recommendation. I will try pscp and psftp. They are also great tools.
    – Alex
    Mar 11, 2013 at 13:18
2

The easiest way I found is using smbget

smbget smb://PATH/TO/FILE/test.txt

If authentication is needed (if password is not provided, it will prompt for password):

smbget smb://PATH/TO/FILE/test.txt -U "[email protected]%myPassword"

You can also specify where to save the file and the name of the file after copy:

smbget smb://PATH/TO/FILE/test.txt -U "[email protected]%myPassword" -o /LINUX/PATH/remote_test_file
1

I'm a CentOS 6 User so I tried this method on CentOS 6

Install samba using

[root@server ~]# yum install samba

Query for confirm

[root@server ~]# rpm -qa samba
samba-3.5.10-125.el6.i686

Create a Samba User.

[root@server ~]# useradd sambashare

Now create samba password for username sambashare using smbpasswd command.

[root@server ~]# smbpasswd -a sambashare
New SMB password:
Retype new SMB password:
Added user sambashare.

Create a samba share directory

[root@server ~]# mkdir /share

change the ownership for the share folder

[root@server ~]# chown -R sambashare:sambashare /share/

Open the file /etc/samba/smb.conf and add the below lines.

[share]
comment = Share
path = /share
writable = yes
valid users = sambashare

Start samba service

[root@server ~]# service smb restart
Shutting down SMB services:                                [  OK  ]
Starting SMB services:                                     [  OK  ]

Check your configuration by using testparm command

.
.
[share]
comment = share
path = /share
valid users = sambashare
read only = No

In windows system type this in run

enter image description here

Now enter samba username and password

enter image description here

You are done

enter image description here

2
  • Thank you very much, max. for your very vivid tutorial for a newbie.
    – Alex
    Mar 11, 2013 at 13:19
  • @Alex you are welcome... :-)
    – max
    Mar 12, 2013 at 4:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .