Samba 和Shell Script 的应用

前提条件:
1、Windows和Linux共处于同一个局域网中,可以互相ping通,都未设置防火墙
2、Linux上安装有samba-client的RPM包,它是Samba客户端工具

3.   Windows下共享文件夹的权限要设置好

1. 在Linux下存取分享: smbclient基本应用

列出192.168.1.3 的分享:

 root # smbclient -L //192.168.1.3 -N 

  Domain=[PC01] OS=[Windows 5.0] Server=[Windows 2000 LAN Manager] 

  Sharename Type Comment 
  --------- ---- ------- 
  aaaaaaaaaaa Disk 
  bbie_10 Disk 
  uncd Disk 
  av Disk 
  hpdeskje Printer hp deskjet 5600 series 
  Download_S Disk 
  upload Disk 
  D_S01 Disk 
  D_S02 Disk 
  D_S03 Disk 
  root # 

进入192.168.1.3 里的bbie_10 分享里,共使用者是steven,且密码是a12345:

 root # smbclient //192.168.1.3/bbie_10 -U steven%a12345 
  Domain=[PC01] OS=[Windows 5.0] Server=[Windows 2000 LAN Manager] 
  smb: > ls 
  . DA 0 Mon Apr 18 21:20:44 2005 
  .. DA 0 Mon Apr 18 21:20:44 2005 
  bbie.exe A 20992 Tue Nov 13 14:11:22 2001 
  bbie.lic A 6251 Mon Aug 25 14:35:50 2003 
  bt DA 0 Sun Feb 27 20:24:32 2005 
  image1.bin A 2048 Mon Apr 18 21:20:44 2005 

              40036 blocks of size 262144. 9962 blocks available 
  smb: > 

列出目录,并取得bbie.exe 档案:

 smb: > ls 
    . DA 0 Mon Apr 18 21:20:44 2005 
    .. DA 0 Mon Apr 18 21:20:44 2005 
    bbie.exe A 20992 Tue Nov 13 14:11:22 2001 
    bbie.lic A 6251 Mon Aug 25 14:35:50 2003 
    bt DA 0 Sun Feb 27 20:24:32 2005 
    image1.bin A 2048 Mon Apr 18 21:20:44 2005 

  40036 blocks of size 262144. 10133 blocks available 
  smb: > get bbie.exe 
  getting file bbie.exe of size 20992 as bbie.exe (70.9 kb/s) (average 70.9 kb/s) 
  smb: > 

查看有那些指令可以使用​​:

 smb: > help 
  ? altname archive blocksize cancel 
  case_sensitive cd chmod chown del 
  dir du exit get hardlink 
  help history lcd link lowercase 
  ls mask md mget mkdir 
  more mput newer open print 
  printmode prompt put pwd q 
  queue quit rd recurse reget 
  rename reput rm rmdir setmode 
  stat symlink tar tarmode translate 
  vuid logon ! 
  smb: > 

2. 在Linux下存取分享: smbclient进阶应用

若你常用smbclient 这个指令,那么你一定会常常下-U user%password 这个参数以登入到提供分享的主机做存取,但是你是否想过这样的话万一遇到有心人,只要在你身旁一看,那你的密码就一览无遗(总该不可能有人会去背一长串加密过后的密码吧?)。 现在只要把你的帐号码密放在一个档案里,在使用smbclient 时同使用-A 选项,就可以不用明明白白的把密码打在营幕上了! 现在我们来看看应该如何应用吧。

建立帐号密码档:

设立帐号为steven;密码为a12345(当然你不可能在指令上直接打帐号密码,我是为了方便才这样)

root # echo -e "username = stevennpassword = a12345" > /misc/smbpw/pws; 

开始使用这个帐号密码档,并进入192.168.1.3 的bbie_10 目录里:

 root # cat /misc/smbpw/pws 
  username = steven 
  password = a12345 
  root # 

直接在bash 下smbclient 指令:

 root # smbclient //192.168.1.3/bbie_10 -A /misc/smbpw/pws 
  Domain=[PC01] OS=[Windows 5.0] Server=[Windows 2000 LAN Manager] 
  smb: > ls 
    . DA 0 Mon Apr 18 21:20:44 2005 
    .. DA 0 Mon Apr 18 21:20:44 2005 
    bbie.exe A 20992 Tue Nov 13 14:11:22 2001 
    bbie.lic A 6251 Mon Aug 25 14:35:50 2003 
    bt DA 0 Sun Feb 27 20:24:32 2005 
    image1.bin A 2048 Mon Apr 18 21:20:44 2005 

                  40036 blocks of size 262144. 11111 blocks available 
  smb: > q@eb 
  root # 

直接上传一个档案到192.168.1.3/upload:

 root # smbclient //192.168.1.3/upload -c "mput firefox-1.5.0.2.tar.gz" -A /misc/smbpw/pws 
  Domain=[PC01] OS=[Windows 5.0] Server=[Windows 2000 LAN Manager] 
  Put file firefox-1.5.0.2.tar.gz? y 
  putting file firefox-1.5.0.2.tar.gz as firefox-1.5.0.2.tar.gz (432.6 kb/s) (average 432.6 kb/s) 
  root # 

3. 在Linux下存取分享: smbmount的使用

Unix/Linux 档案系统里有一个很大的功能就是挂载磁碟机,不论是floppy、NFS Filesystem、CD/DVD-ROM ​​... 等格式都可以,那么如何挂载资源分享的目录呢,其实很简单,只要使用smbmount 就可以了。

把192.168.1.3 的upload 挂载到/smb-dir 上

 root # smbmount //192.168.1.3/upload /smb-dir/ -o "username=steven%a12345"; [ $? == 0 ] && mount 
  /dev/hda2 on / type ext3 (rw) 
  none on /proc type proc (rw) 
  none on /sys type sysfs (rw) 
  none on /dev/pts type devpts (rw,gid=5,mode=620) 
  usbfs on /proc/bus/usb type usbfs (rw) 
  /dev/hda1 on /boot type ext3 (rw) 
  /dev/hda3 on /burn type ext3 (rw) 
  /dev/hda5 on /data type ext3 (rw) 
  none on /dev/shm type tmpfs (rw) 
  /dev/hda7 on /home type ext3 (rw) 
  none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw) 
  //192.168.1.3/upload on /smb-dir type smbfs (0) 
  root # 

直接使用/misc/smbpw/pws 的帐号密码而不在营幕输入

 root # smbmount //192.168.1.3/upload /smb-dir/ -o "credentials=/misc/smbpw/pws" 
  root # [ $? == 0 ] && mount && ls /smb-dir/ 
  /dev/hda2 on / type ext3 (rw) 
  none on /proc type proc (rw) 
  none on /sys type sysfs (rw) 
  none on /dev/pts type devpts (rw,gid=5,mode=620) 
  usbfs on /proc/bus/usb type usbfs (rw) 
  /dev/hda1 on /boot type ext3 (rw) 
  /dev/hda3 on /burn type ext3 (rw) 
  /dev/hda5 on /data type ext3 (rw) 
  none on /dev/shm type tmpfs (rw) 
  /dev/hda7 on /home type ext3 (rw) 
  none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw) 
  //192.168.1.3/upload on /smb-dir type smbfs (0) 
  #这个就是/smb-dir挂载后的结果03.mpg firefox-1.5.0.2.tar.gz Thumbs.db  
  [root@dns /]# 

来源:http://www.l-penguin.idv.tw/article/smb_script.htm

更多参考:

smbclient主要用在Linux访问windows的共享文件,或者是访问Linux的samba服务器。

功能说明:可存取SMB/CIFS服务器的用户端程序。

语 法:smbclient [网络资源][密码][-EhLN][-B<IP地址>][-d<排错层级>][-i<范围>][-I<IP 地址>][-l<记录文件>][-M<NetBIOS名称>][-n<NetBIOS名称>][-O< 连接槽选项>][-p<TCP连接端口>][-R<名称解析顺序>][-s<目录>][-t<服务器字 码>][-T<tar选项>][-U<用户名称>][-W<工作群组>]

补充说明:SMB与CIFS为服务器通信协议,常用于Windows95/98/NT等系统。smbclient可让Linux系统存取Windows系统所分享的资源。

参数:
[网络资源] [网络资源]的格式为//服务器名称/资源分享名称。
[密码] 输入存取网络资源所需的密码。

-B<IP地址> 传送广播数据包时所用的IP地址。
-d<排错层级> 指定记录文件所记载事件的详细程度。
-E 将信息送到标准错误输出设备。
-h 显示帮助。
-i<范围> 设置NetBIOS名称范围。
-I<IP地址> 指定服务器的IP地址。
-l<记录文件> 指定记录文件的名称。
-L 显示服务器端所分享出来的所有资源。
-M<NetBIOS名称> 可利用WinPopup协议,将信息送给选项中所指定的主机。
-n<NetBIOS名称> 指定用户端所要使用的NetBIOS名称。
-N 不用询问密码。
-O<连接槽选项> 设置用户端TCP连接槽的选项。
-p<TCP连接端口> 指定服务器端TCP连接端口编号。
-R<名称解析顺序> 设置NetBIOS名称解析的顺序。
-s<目录> 指定smb.conf所在的目录。
-t<服务器字码> 设置用何种字符码来解析服务器端的文件名称。
-T<tar选项> 备份服务器端分享的全部文件,并打包成tar格式的文件。
-U<用户名称> 指定用户名称。
-W<工作群组> 指定工作群组名称。

命令 说明
?或help [command] 提供关于帮助或某个命令的帮助
![shell command]   执行所用的SHELL命令,或让用户进入 SHELL提示符
cd [目录]  切换到服务器端的指定目录,如未指定,则 smbclient 返回当前本地目录
lcd [目录]  切换到客户端指定的目录;
dir 或ls   列出当前目录下的文件;
exit 或quit    退出smbclient
get file1  file2 从服务器上下载file1,并以文件名file2存在本地机上;如果不想改名,可以把file2省略
mget file1 file2 file3  filen 从服务器上下载多个文件;
md或mkdir 目录 在服务器上创建目录
rd或rmdir 目录 删除服务器上的目录
put file1 [file2] 向服务器上传一个文件file1,传到服务器上改名为file2;
mput file1 file2 filen 向服务器上传多个文件

使用说明
1、列出某个IP地址所提供的共享文件夹
smbclient –L IP或者NETBIOS名称,作用是显示某台提供samba服务的服务器上的共享资源。命令执行效果如下:
#查看linux服务器的共享。
# smbclient -L 192.168.18.3 -U hto%passwd
Domain=[MYGROUP] OS=[Unix] Server=[Samba 3.5.10-0.110.el5_8]
    Sharename       Type      Comment
    ---------       ----      -------
    IPC$            IPC       IPC Service (Samba Server Version 3.5.10-0.110.el5_8)
    hto             Disk      Home Directories
Domain=[MYGROUP] OS=[Unix] Server=[Samba 3.5.10-0.110.el5_8]
    Server               Comment
    ---------            -------
    Workgroup            Master
    ---------            -------

#查看windows服务器的共享。
# smbclient -L 192.168.18.98 -U hto%passwd
Domain=[IMO-PC] OS=[Windows 7 Ultimate 7600] Server=[Windows 7 Ultimate 6.1]
    Sharename       Type      Comment
    ---------       ----      -------
    ADMIN$          Disk      远程管理
    C$              Disk      默认共享
    D$              Disk      默认共享
    E$              Disk      默认共享
    IPC$            IPC       远程 IPC
    sd              Disk      
session request to 192.168.18.98 failed (Called name not present)
session request to 192 failed (Called name not present)
session request to *SMBSERVER failed (Called name not present)
NetBIOS over TCP disabled -- no workgroup available

第一段列举了该机器上的共享资源,第二段列举了其所在的工作组里面所有提供samba服务的机器,第三列举了其他工作组提供 browse服务的Master机器。

2、像FTP客户端一样使用smbclient
使用网络资源:
smbclient //IP或者NETBIOS名称/共享资源名 [-U 用户名]
命令执行效果如下:
smbclient //192.168.18.98/sd/

接下来的操作和ftp的时候命令一样,用get下载文件而用put上传文件,命令可以用help查看。还有一种方法是用文件系统的方式(这种方式要确定你的内核支持smbfs,或通过安装'smbfs'软件来实现。

smbmount //IP或者NETBIOS名称/共享资源名 /本地挂接点 [-o option]

常 用的的option有username=<用户名>,password=<密码>,guest(指定为用guest访问,不用提 供密码,前面的即使用username=guest参数的话也会要求输入密码),ro(有时候为了系统安全要指定为只读模式),rw,同时多个 option的话用逗号隔开。对于使用了认证密码的挂载,可将密码写入文件中,如(/etc/fstab):
//192.168.18.98/sd/    /home/sd    smbfs    rw,iocharset=utf8,credentials=/root/pri/smb.passwd    0    0

文件'/root/pri/smb.passwd'格式如下:
username=freeoa
password=passwd

注意要设定好该文件的权限,以防被他人看到内容。

或者可以用mount –t smbfs [–o option] //IP或者NETBIOS名称/共享资源名 /本地挂接点 来实现同样的功能。

例子如下:
smbmount //ip/littlep /test -o guest
或者
mount –t smbfs –o guest //ip/littlep /test,

然后就能通过访问/test来使用网络上的资源了。如果不需要使用的时候,可以简单地使用smbumount /test或者umount /test来解除这个挂接。

3、直接一次性使用smbclient命令

smbclient -c "ls"  //192.168.18.3/tmp  -U username%password
#和功能一样的
smbclient //192.168.18.3/tmp  -U username%password
smb:\>ls

 
例,创建一个共享文件夹

smbclient -c "mkdir share1"  //192.168.18.3/tmp  -U username%password

 
如果用户共享//192.168.18.3/tmp的方式是只读的,会提示
NT_STATUS_ACCESS_DENIED making remote directory \share1

# mount -t smbfs -o username="administrator",password="" //192.168.18.3/cp /mnt/ntfs


[Mount a CIFS share via fstab]
通过'fstab'文件来挂载samba共享,在系统启动时自动挂载cifs共享,而不是手动挂载,可在'/etc/fstab'文件记下相关的记录行,大致如下:

//<ip.address>/<share_name>       /mount/point       cifs       credentials=/path/to/.credentials_file,_netdev,iocharset=utf8,uid=<numeric user id>,gid=<numeric group id>,file_mode=0750,dir_mode=0750,rw     0 0

解析如下:

_netdev -- this option tells fstab that we don't want to try mounting this share until the network manager is running, probably a good idea since a cifs share is, naturally, mounted over the network!

iocharset -- the above might differ depending on the language your files are named with, play with this if you use a different character set.

uid and gid -- setting these for the share determines who appears as the owner when you browse to the mount point, if you don't set these options root will appear to own the mount point and all the files in it, which is most likely not what you want! To find your user and group id's you can type "id <username>" in the terminal and it will print out the numeric id for your username and all the groups your id is member of

file_mode and dir_mode -- these determine the permissions of the mount point locally and they work just like any other user permissions on a unix system, the owner permissions will be relative to the uid you set and the group permissions will be relative to the gid you set. This means if you use the above example and have 5 for the group permissions and then, as a user other than uid and who isn't a part of gid, you try to write to the share, you'll get permission denied.

Note: even if you set permissions here to give you write access but the share source doesn't give you write permissions, you still won't be able to write to the share!

credentials -- this is a nice way to avoid posting your username and password directly in your fstab! For example, instead of using a credentials files you could put username=SomeUser,password=SomePassword right into the options and it would work, but that would expose your password to anyone who had viewing rights to the fstab file, which may not matter in the scheme of things for a private server, but it's still poor practice.

The contents of a credentials file is very simple, consider the below
 username=SomeUser
 password=SomePassword

Note: CIFS can be very sensitive to extra white spaces, so make sure each line ends with the end of your username / password and not a white space, also make sure that your file ends on the password line and not a blank line, this can be a good place to check if your mount is failing and you're sure that your login information is correct. Also, do not use quotation marks, this will cause login failures.

来源: http://www.freeoa.net/osuport/servap/smbclient-usage_946.html

猜你喜欢

转载自justcoding.iteye.com/blog/1941709
今日推荐