SMB上传下载

Zss 发表于:

Tool.smb_down(‘test’,’test’,’192.168.3.222′,’445′,r’C:\Auto-Test\Data\Smb\smb-client-1\test.exe’,’Smb/smb-server-1/test.exe’)
Tool.smb_up(‘test’,’test’,’192.168.3.222′,’445′,r’C:\Auto-Test\Data\Smb\smb-client-2\test.exe’,’Smb/smb-server-2/test.exe’)

down 账号密码 将Smb/smb-server-1/test.exe的文件下载到C:\Auto-Test\Data\Smb\smb-client-1\test.exe

ip 账号密码 将C:\Auto-Test\Data\Smb\smb-client-2\test.exe上传到Smb/smb-server-2/test.exe

from smb.SMBConnection import SMBConnection

class Tool():
    def __init__(self):
        pass

    @staticmethod
    def smb_up(username,password,ip,port,src_file,dst_file):
        # 新建连接对象
        try:
            conn = SMBConnection(username,password,'autotest',ip, domain=ip,use_ntlm_v2=True, is_direct_tcp = True)
            # 返回值为布尔型,表示连接成功与否
            result = conn.connect(ip,int(port))
            if result == True:
                print('SMB连接成功!!!')
            else:
                print('SMB连接失败!!!')
            f = open(src_file,'rb')
            conn.storeFile(dst_file.split('/',1)[0],dst_file.split('/',1)[1],f)
            f.close()
        except Exception as e:
            print('ERROR:%s'%e)

    @staticmethod
    def smb_down(username,password,ip,port,src_file,dst_file):
        # 新建连接对象
        try:
            conn = SMBConnection(username,password,'autotest',ip, domain=ip,use_ntlm_v2=True, is_direct_tcp = True)
            # 返回值为布尔型,表示连接成功与否
            result = conn.connect(ip,int(port))
            if result == True:
                print('SMB连接成功!!!')
            else:
                print('SMB连接失败!!!')
            with open(src_file,'wb') as f:
                conn.retrieveFile(dst_file.split('/',1)[0],dst_file.split('/',1)[1],f)
            f.close()
        except Exception as e:
            print('ERROR:%s'%e)

if __name__ == '__main__':
    Tool.smb_down('test','test','192.168.3.222','445',r'C:\Auto-Test\Data\Smb\smb-client-1\test.exe','Smb/smb-server-1/test.exe')
    Tool.smb_up('test','test','192.168.3.222','445',r'C:\Auto-Test\Data\Smb\smb-client-2\test.exe','Smb/smb-server-2/test.exe')