前提条件:
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

Samba 和Shell Script 的应用相关推荐

  1. 20单元——学习正解表达式及学习实践 Shell script

    正则表达式: 处理字符串的表示方式,它以行为单位来进行字符串的处理操作,正则表达式通过一些特殊符号的辅助,可以让用户轻易地完成[查找.删除.替换]某特定字符串的处理过程. 正则表达式的字符串表达方式依 ...

  2. 「学习笔记-Linux」学习Shell Script

    学习Shell Script Table of Contents 1 什么是Shell Scipt 1.1 程序书写 1.2 程序执行 2 简单Shell练习 2.1 例1 接收用户输入 2.2 例2 ...

  3. linux 下 将 shell script 与 一个桌面图标联系在一起 (2)

    Cy163注: 步骤如下: (1)         建立 xxxxx.sh (2)         在 vnc 虚拟终端 的desktop上,右键,然后选择"创建启动器" (3)  ...

  4. Linux08--Shell程序设计03 shell script

    第一个Shell脚本--HelloWorld [root@localhost ~]# vi sh01.sh #!/bin/bash #!表明使用哪种shell # this is my first s ...

  5. 那些年我们一起追过的Shell Script

    原本这是自己在几个月前为公司的一个分享活动写的一个投影片,今天趁大脑负荷比较小,把这个话题拿到blog上面来分享一下.从知道shell算起至今也就几个年头而已,如今勉强算是入门了.对某一个新事物的掌握 ...

  6. Linux shell script 的语法汇总

    2019独角兽企业重金招聘Python工程师标准>>> 条件判断式 if判断式 if [ 条件判断式1 ]:then执行动作 elif [ 条件判断式2 ]:then执行动作 els ...

  7. linux-basic(13)学习shell script

    [13.1]什么是shell script? 1)shell script 是利用 shell 的功能所写的一个『程序 (program)』,这个程序是使用纯文字档,将一些 shell 的语法与命令( ...

  8. Shell Script 学习一

    Shell Script编写注意事项: 1.命令的执行是从上而下,从左至右地分析与执行. 2.命令执行中,命令与参数间的多个空白都会被忽略掉. 3.空白行也被将忽略掉,并且[tab]按键所得的空白同样 ...

  9. shell script if嵌套for循环脚本

    通过一个脚本介绍shell script编写的三个注意事项 文章目录 脚本内容 脚本关键点讲解 脚本内容 输出设备告警并将其清除 bash-4.3$ cat clear_alarm.sh #!/bin ...

最新文章

  1. python生成简单的FTP弱口令扫描
  2. sorl实现商品快速搜索
  3. 用navixt连接mysql连接不上_AMD神秘新显卡现身CompuBench数据库,可能是7nm Navi
  4. mybatis入门(二)之XML 映射配置文件
  5. 【Python基础知识-pycharm版】第一节-基础
  6. flask连接mysql数据库_Flask与Mysql数据库建立连接
  7. HDU-5781 ATM Mechine(概率DP)
  8. ios给控件添加动画效果
  9. java B2B2C 多租户电子商城系统-SpringCloud动态刷新配置信息
  10. python爬去微博签到数据_GitHub - fs6/weiboSpider: 新浪微博爬虫,用python爬取新浪微博数据...
  11. Ubuntu 18.04截屏
  12. 为计算机技术奉献一生语录,乐于奉献的名言警句40句
  13. 杭州电子科技大学2020计算机研究生考研经验贴
  14. 爬虫练手小项目:豆瓣高分图书TOP100
  15. SQL 实验项目8_事务控制
  16. c语言普及组复赛题目大全,NOIP 2016普及组复赛C/C++详细题解报告
  17. 墨菲定律和吉德林法则
  18. 中国建造了世界上最大的超级计算机,中国超级计算机创造出世界最大虚拟宇宙...
  19. 一起来学自然语言处理----加工原料文本
  20. CSGO 播放DEMO 闪退跳出到桌面无法播放问题解决

热门文章

  1. 4.2 SE38数据对象
  2. python 默认参数_避坑指南!Python里面的这九个坑,坑的就是你!
  3. hive load data inpath 空目录_hive学习笔记之四:分区表
  4. rabbitmq报错:PRECONDITION_FAILED - parameters for queue ‘test-1‘ in vhost ‘/‘ not equivalent
  5. 基于Android的智能家居手持终端系统开发 毕业论文-A
  6. [Origin]图像属性:画图时修改直线的起点和终点、随图层和坐标变化、多y轴图独立编辑线条属性
  7. webserver之日志系统
  8. gdt描述_GDT(Global Descriptor Table)全局描述符表
  9. phpcmsV9文章页时间 - 踩坑篇
  10. 高精度矢量汉字的一种填充方法_惯导解算数学基础4(等效旋转矢量解,圆锥补偿解)...