欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

路由器 访问windows共享文件,读取相关的文件,判断文件的大小

程序员文章站 2022-05-11 18:13:43
...

路由器 访问windows共享文件,读取相关的文件,判断文件的大小

一. ubuntu 1604测试如下

直接使用命令安装samba和smbclinet,同时安装cifs文件系统格式,即mount.cifs命令。

1. 关于Ubuntu16.04连接Windows10共享失败的问题解决

一开始以为是Windows防火墙的问题,但是关闭后故障依旧,把samba等一系列服务全部重装,问题依旧,尝试使用smbclient命令行工具连接共享,

smbclient -L 192.168.1.198 -U username%password
WARNING: The "syslog" option is deprecated
protocol negotiation failed: NT_STATUS_CONNECTION_RESET

2. 解决办法:

得到如下错误信息,后来在samba的一个国外wiki上找到了解决办法:

Protocol negotiation failed: NT_STATUS_CONNECTION_RESET
Probably the server is configured not to accept protocol SMB1. Add option client max protocol = SMB2 in /etc/samba/smb.conf. Or just pass argument -m SMB2 to smbclient.

意思是我们目前的samba客户端使用的协议是SMB1,但是Windows10已经使用SMB2这个协议了,所以我们可以使用-m SMB2这个命令行参数来强制sambaclient使用SMB2协议,但是每次都使用命令行显然不方便,那么我们可以修改配置文件,在/etc/samba/smb.conf文件中添加client max protocol = SMB2 ,重启samba服务,正常访问Windows。

注:在Windows10中最好在网络和共享中心——>高级共享设置中关闭密码保护共享,这样可以无密码的访问Windows共享,缺点是安全性下降。

3. 修改后,可以成功使用命令

  1. 查看远程主机的共享有那些?
[email protected]:~/work/p53/build$ smbclient -L 192.168.66.159 -U sean
WARNING: The "syslog" option is deprecated
Enter sean's password: 
Domain=[SEAN-PC] OS=[] Server=[]

	Sharename       Type      Comment
	---------       ----      -------
	ADMIN$          Disk      远程管理
	C$              Disk      默认共享
	D               Disk      
	D$              Disk      默认共享
	E               Disk      
	E$              Disk      默认共享
	IPC$            IPC       远程 IPC
	software        Disk      
	xsmk            Disk      
Connection to 192.168.66.159 failed (Error NT_STATUS_RESOURCE_NAME_NOT_FOUND)
NetBIOS over TCP disabled -- no workgroup available
[email protected]:~/work/p53/build$ 
[email protected]:~/work/p53/build$ 

2). 连接共享目录

[email protected]:~/work/p53/build$ smbclient //192.168.66.159/xsmk -U sean
WARNING: The "syslog" option is deprecated
Enter sean's password: 
Domain=[SEAN-PC] OS=[] Server=[]
smb: \> ls
  .                                   D        0  Fri Nov 16 20:30:18 2018
  ..                                  D        0  Fri Nov 16 20:30:18 2018
  1                                   A 11876640  Tue Jun 27 16:49:42 2017
  1.dat                               A      204  Fri Nov 16 10:15:52 2018
  10005.log                           A     2586  Fri Nov 16 13:01:25 2018
  10016.log                           A     2586  Fri Nov 16 13:01:35 2018
  10026.log                           A     2586  Fri Nov 16 13:01:46 2018
  10037.log                           A     2586  Fri Nov 16 13:01:56 2018
  10047.log                           A     2586  Fri Nov 16 13:02:06 2018
  10057.log                           A     2589  Fri Nov 16 13:02:17 2018
  10067.log                           A     2586  Fri Nov 16 13:02:27 2018
  10078.log                           A     2554  Fri Nov 16 13:02:37 2018
  10088.log                           A     2587  Fri Nov 16 13:02:48 2018
  10099.log                           A     2558  Fri Nov 16 13:02:59 2018
  10110.log                           A     2617  Fri Nov 16 13:03:09 2018
  10120.log                           A     2587  Fri Nov 16 13:03:20 2018
  9985.log                            A     2586  Fri Nov 16 13:01:04 2018
  9995.log                            A     2586  Fri Nov 16 13:01:15 2018
  device.txt                          A       36  Thu Nov 15 10:56:07 2018
  test.sh                             A      352  Thu Jan  1 08:32:41 1970

		25671842 blocks of size 4096. 10230783 blocks available
smb: \> 

3). 连接到共享后,使用的命令如下:

smb: \> ?
?              allinfo        altname        archive        backup         
blocksize      cancel         case_sensitive cd             chmod          
chown          close          del            dir            du             
echo           exit           get            getfacl        geteas         
hardlink       help           history        iosize         lcd            
link           lock           lowercase      ls             l              
mask           md             mget           mkdir          more           
mput           newer          notify         open           posix          
posix_encrypt  posix_open     posix_mkdir    posix_rmdir    posix_unlink   
print          prompt         put            pwd            q              
queue          quit           readlink       rd             recurse        
reget          rename         reput          rm             rmdir          
showacls       setea          setmode        scopy          stat           
symlink        tar            tarmode        timeout        translate      
unlock         volume         vuid           wdel           logon          
listconnect    showconnect    tcon           tdis           tid            
logoff         ..             !              
smb: \> 
smb: \> 

如果想判断共享目录中某个文件的大小或者其他属性,或者读取某个文件的内容,只能下载到本地(路由器),进行访问。此方法初步判断比较笨拙。

4. 方案二

采用另外一种挂载分区的方法,要简单的多。只是需要支持cifs文件系统格式即可。

[email protected]:~/work/p53/build$ sudo mount -t cifs -o username=sean,password=sean //192.168.66.159/xsmk /mnt/winshare/
[email protected]:~/work/p53/build$ 
[email protected]:~/work/p53/build$ 
[email protected]:~/work/p53/build$ ls -l /mnt/winshare/
total 36735
-rwxr-xr-x 1 root root 11876640 Jun 27  2017 1
-rwxr-xr-x 1 root root     2586 Nov 16 13:01 10005.log
-rwxr-xr-x 1 root root     2586 Nov 16 13:01 10016.log
-rwxr-xr-x 1 root root     2586 Nov 16 13:01 10026.log
-rwxr-xr-x 1 root root     2586 Nov 16 13:00 9964.log
-rwxr-xr-x 1 root root     2586 Nov 16 13:00 9974.log
-rwxr-xr-x 1 root root     2586 Nov 16 13:01 9985.log
-rwxr-xr-x 1 root root     2586 Nov 16 13:01 9995.log
-rwxr-xr-x 1 root root       36 Nov 15 10:56 device.txt
-rwxr-xr-x 1 root root      352 Jan  1  1970 test.sh
[email protected]:~/work/p53/build$ 

评述:此方法,简单易懂,方便操作。

二. 路由器调试

1. 内核支持 cifs 文件系统格式

进入linux目录,使用make menuconfig,进入config配置界面,打开如下功能:

    File systems  ---> 

         [*] Network File Systems  --->

                 <M>   CIFS support (advanced network filesystem, SMBFS successor)

2. 编译完成,进入系统,挂载目录

~ # mount -t cifs -o username=sean,password=sean //192.168.0.157/xsmk /mnt/winss
hare/

~ # ls /mnt/winsshare/
1          2594.log   3439.log   4308.log   5180.log   6068.log   6936.log
1.dat      2604.log   3449.log   4319.log   5190.log   6078.log   6947.log
13034.log  2615.log   3460.log   4329.log   5201.log   6089.log   6958.log
1606.log   2625.log   3470.log   4339.log   5211.log   6099.log   6969.log
1620.log   2636.log   3481.log   4350.log   5221.log   6110.log   6980.log
1630.log   2646.log   3491.log   4360.log   5232.log   6120.log   6990.log
1641.log   2656.log   3502.log   4370.log   5242.log   6131.log   7000.log
1651.log   2667.log   3512.log   4381.log   5253.log   6141.log   7011.log
1661.log   2677.log   3523.log   4391.log   5263.log   6151.log   7021.log
1671.log   2688.log   3533.log   4401.log   5273.log   6162.log   7032.log
1681.log   2698.log   3543.log   4412.log   5284.log   6172.log   7044.log
1691.log   2709.log   3553.log   4423.log   5294.log   6182.log   7054.log
1701.log   2720.log   3564.log   4433.log   5304.log   6193.log   7066.log
1711.log   2730.log   3574.log   4444.log   5315.log   6203.log   7076.log
1722.log   2741.log   3585.log   4454.log   5325.log   6213.log   7088.log
1732.log   2751.log   3595.log   4465.log   5335.log   6224.log   7102.log
1742.log   2762.log   3605.log   4476.log   5346.log   6234.log   7114.log
1752.log   2772.log   3616.log   4487.log   5357.log   6245.log   7125.log
1762.log   2782.log   3627.log   4497.log   5367.log   6255.log   7135.log
1772.log   2793.log   3637.log   4509.log   5377.log   6265.log   7146.log
1782.log   2804.log   3647.log   4520.log   5388.log   6276.log   7157.log
1965.log   2814.log   3658.log   4531.log   5398.log   6286.log   7168.log
1975.log   2825.log   3668.log   4542.log   5408.log   6296.log   7181.log
1985.log   2835.log   3678.log   4553.log   5419.log   6309.log   7191.log
1996.log   2845.log   3689.log   4563.log   5429.log   6319.log   7201.log
2          2856.log   3699.log   4574.log   5440.log   6329.log   7211.log
2006.log   2866.log   3712.log   4584.log   5450.log   6340.log   7222.log
2016.log   2876.log   3724.log   4594.log   5461.log   6350.log   7232.log
2027.log   2887.log   3736.log   4605.log   5471.log   6361.log   7242.log
2037.log   2898.log   3747.log   4615.log   5481.log   6371.log   7253.log
2048.log   2908.log   3760.log   4626.log   5492.log   6381.log   7263.log
2059.log   2919.log   3771.log   4637.log   5502.log   6392.log   7273.log
2069.log   2929.log   3781.log   4648.log   5513.log   6402.log   7284.log
2079.log   2940.log   3792.log   4659.log   5523.log   6412.log   7294.log
2090.log   2950.log   3802.log   4669.log   5533.log   6423.log   7304.log
2100.log   2961.log   3812.log   4680.log   5544.log   6434.log   7315.log
2111.log   2971.log   3823.log   4692.log   5554.log   6444.log   7325.log
2121.log   2982.log   3834.log   4707.log   5565.log   6454.log   7335.log
2132.log   2992.log   3844.log   4717.log   5575.log   6465.log   7346.log
2142.log   3          3855.log   4730.log   5586.log   6475.log   7356.log
2153.log   3002.log   3867.log   4741.log   5597.log   6485.log   7366.log
2163.log   3012.log   3878.log   4753.log   5608.log   6495.log   7377.log
2174.log   3023.log   3888.log   4763.log   5618.log   6506.log   7387.log
2185.log   3034.log   3899.log   4774.log   5629.log   6516.log   7397.log
2195.log   3044.log   3909.log   4784.log   5640.log   6526.log   7408.log
2205.log   3054.log   3920.log   4794.log   5651.log   6537.log   7418.log
2215.log   3064.log   3930.log   4805.log   5661.log   6547.log   7428.log
2226.log   3075.log   3940.log   4815.log   5672.log   6558.log   7439.log
2236.log   3085.log   3951.log   4825.log   5682.log   6568.log   7449.log
2247.log   3096.log   3961.log   4836.log   5693.log   6578.log   7459.log
2257.log   3106.log   3972.log   4846.log   5705.log   6588.log   7470.log
2268.log   3117.log   3983.log   4856.log   5715.log   6599.log   7480.log
2278.log   3127.log   3993.log   4866.log   5725.log   6609.log   7490.log
2289.log   3138.log   4004.log   4877.log   5736.log   6620.log   7501.log
2299.log   3148.log   4014.log   4888.log   5746.log   6631.log   7511.log
2310.log   3159.log   4025.log   4898.log   5757.log   6641.log   7521.log
2320.log   3169.log   4035.log   4908.log   5767.log   6652.log   7532.log
2330.log   3180.log   4046.log   4919.log   5778.log   6662.log   7542.log
2341.log   3190.log   4056.log   4929.log   5789.log   6673.log   7552.log
2351.log   3200.log   4067.log   4940.log   5800.log   6683.log   7563.log
2362.log   3210.log   4078.log   4950.log   5810.log   6693.log   7573.log
2372.log   3221.log   4089.log   4960.log   5820.log   6704.log   7583.log
2382.log   3231.log   4099.log   4971.log   5847.log   6714.log   7594.log
2393.log   3242.log   4110.log   4981.log   5858.log   6725.log   7604.log
2403.log   3252.log   4120.log   4992.log   5870.log   6736.log   7614.log
2414.log   3262.log   4130.log   5002.log   5888.log   6746.log   7625.log
2425.log   3273.log   4140.log   5012.log   5898.log   6756.log   7635.log
2435.log   3283.log   4151.log   5023.log   5908.log   6767.log   7645.log
2446.log   3293.log   4162.log   5033.log   5919.log   6777.log   7656.log
2457.log   3304.log   4172.log   5044.log   5929.log   6788.log   7666.log
2467.log   3314.log   4182.log   5054.log   5940.log   6798.log   7676.log
2478.log   3324.log   4193.log   5064.log   5954.log   6808.log   7687.log
2488.log   3335.log   4203.log   5074.log   5964.log   6819.log   7697.log
2500.log   3345.log   4214.log   5086.log   5975.log   6830.log   7708.log
2510.log   3356.log   4224.log   5097.log   5985.log   6840.log   7718.log
2520.log   3366.log   4235.log   5107.log   5996.log   6851.log   7729.log
2531.log   3376.log   4245.log   5118.log   6006.log   6863.log   test.sh
2542.log   3387.log   4256.log   5128.log   6016.log   6875.log
2552.log   3397.log   4266.log   5139.log   6027.log   6886.log
2562.log   3408.log   4276.log   5149.log   6037.log   6899.log
2573.log   3418.log   4287.log   5159.log   6047.log   6912.log
2583.log   3428.log   4298.log   5170.log   6058.log   6925.log
~ # 
~ # 

使用以上命令挂载时,共享目录下文件太多,有可能出现以下提示信息:

~ # mount -t cifs -o username=sean,password=sean //192.168.0.157/xsmk /mnt/winss
hare/
[ 4799.310000] CIFS VFS: default security mechanism requested.  The default security mechanism will be upgraded from ntlm to ntlmv2 in kernel release 3.3

本人测试时,共享文件3376个,就会出现以上的问题。删除一些后(600个文件),可以正常挂载,没有报错。也可以正常访问。

3. 测试其稳定性

使用如下测试脚本,进行长时间测试。

~ # cat /mnt/winsshare/test.sh 
#!/bin/sh


while [ 1 ]
do
        filename=$(date +%s)
        echo "$(date)" > $filename.log
        echo "------------------------------------------------------------" >> $filename.log
        echo "$(cat /proc/meminfo)" >> $filename.log 
        echo "------------------------------------------------------------" >> $filename.log
        echo "$(top -n 1)" >> $filename.log
        sleep 10
done
~ # 

测试24h,系统运行正常。不过查看可用内存,会一直减小到10M左右(开始测试前为28M左右)。此问题应该是cached的问题。

三. 路由器使用smbclient的方式

首先需要移植samba到路由器源码中,进行编译。

官网地址:https://www.samba.org/samba/download/
下载: git clone https://github.com/samba-team/samba.git

1. 使用smbclient命令,查看共享

~ # 
~ # smbclient -L 192.168.0.157/xsmk -U sean
params.c:OpenConfFile() - Unable to open configuration file "/lib/smb.conf":
        No such file or directory
smbclient: Can't load /lib/smb.conf - run testparm to debug it
Enter sean's password: 
Failed to load upcase.dat, will use lame ASCII-only case sensitivity rules
Failed to load lowcase.dat, will use lame ASCII-only case sensitivity rules
Domain=[SEAN-PC] OS=[Windows 7 Ultimate 7601 Service Pack 1] Server=[Windows 7 Ultimate 6.1]

        Sharename       Type      Comment
        ---------       ----      -------
        ADMIN$          Disk      \
                                   !
        C$              Disk      X$q+
        D               Disk      
        D$              Disk      X$q+
        E               Disk      
        E$              Disk      X$q+
        IPC$            IPC       \
                                    IPC
        software        Disk      
        xsmk            Disk      
session request to 192.168.0.157 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
~ # 
~ # 

基于以上的报错信息,可能是我们编译时,配置文件和data文件加载的目录指定问题。

2. 连接共享目录

~ # 
~ # smbclient //192.168.0.157/xsmk -U sean
params.c:OpenConfFile() - Unable to open configuration file "/lib/smb.conf":
        No such file or directory
smbclient: Can't load /lib/smb.conf - run testparm to debug it
Enter sean's password: 
Failed to load upcase.dat, will use lame ASCII-only case sensitivity rules
Failed to load lowcase.dat, will use lame ASCII-only case sensitivity rules
Domain=[SEAN-PC] OS=[Windows 7 Ultimate 7601 Service Pack 1] Server=[Windows 7 Ultimate 6.1]
smb: \> ls
  .                                   D        0  Mon Nov 19 15:50:05 2018
  ..                                  D        0  Mon Nov 19 15:50:05 2018
  1                                   A 11876640  Tue Jun 27 16:49:42 2017
  1.dat                               A      204  Fri Nov 16 10:15:52 2018
  13034.log                           A     2587  Fri Nov 16 13:51:53 2018
  1606.log                            A       29  Fri Nov 16 10:41:26 2018
  1620.log                            A       29  Fri Nov 16 10:41:40 2018
  1630.log                            A       29  Fri Nov 16 10:41:50 2018
  1641.log                            A       29  Fri Nov 16 10:42:00 2018
  1651.log                            A       29  Fri Nov 16 10:42:10 2018
  1661.log                            A       29  Fri Nov 16 10:42:20 2018
  1671.log                            A       29  Fri Nov 16 10:42:30 2018
  1681.log                            A       29  Fri Nov 16 10:42:40 2018
  1691.log                            A       29  Fri Nov 16 10:42:50 2018
  7645.log                            A     2586  Fri Nov 16 12:22:05 2018
  7656.log                            A     2586  Fri Nov 16 12:22:15 2018
  7666.log                            A     2586  Fri Nov 16 12:22:26 2018
  7676.log                            A     2586  Fri Nov 16 12:22:36 2018
  7687.log                            A     2586  Fri Nov 16 12:22:46 2018
  7697.log                            A     2586  Fri Nov 16 12:22:57 2018
  7708.log                            A     2586  Fri Nov 16 12:23:07 2018
  7718.log                            A     2586  Fri Nov 16 12:23:18 2018
  7729.log                            A     2586  Fri Nov 16 12:23:28 2018
  test.sh                             A      352  Thu Jan  1 08:32:41 1970

                50140 blocks of size 2097152. 19962 blocks available
smb: \>

跟ubuntu一样,也可以访问。其他使用还需要根据具体的需求进行调试。

smb: \> 
smb: \> ?
?              allinfo        altname        archive        blocksize      
cancel         case_sensitive cd             chmod          chown          
close          del            dir            du             echo           
exit           get            getfacl        geteas         hardlink       
help           history        iosize         lcd            link           
lock           lowercase      ls             l              mask           
md             mget           mkdir          more           mput           
newer          open           posix          posix_encrypt  posix_open     
posix_mkdir    posix_rmdir    posix_unlink   print          prompt         
put            pwd            q              queue          quit           
readlink       rd             recurse        reget          rename         
reput          rm             rmdir          showacls       setea          
setmode        stat           symlink        tar            tarmode        
translate      unlock         volume         vuid           wdel           
logon          listconnect    showconnect    ..             !              
smb: \> 
smb: \>