pysmb

2012-04-20 23 views
7

का उदाहरण क्या आप मुझे कुछ सांबा सर्वर से कनेक्ट करने के लिए pysmb लाइब्रेरी का उपयोग करने का उदाहरण दे सकते हैं? मैं वहाँ वर्ग smb.SMBConnection.SMBConnection है (उपयोगकर्ता नाम, पासवर्ड, my_name, REMOTE_NAME, डोमेन = '', use_ntlm_v2 = सच) लेकिन मैं समझ नहीं है कि यह कैसे उपयोग करने के लिएpysmb

उत्तर

6

SMBConnection वर्ग पढ़ा है ब्लॉकिंग मोड में रिमोट सांबा सर्वर पर फ़ाइलों तक पहुंचने की अनुमति देगा।

दूरस्थ सर्वर पर किसी साझा फ़ोल्डर में फ़ाइलों की सूची प्राप्त करने के लिए,

conn = SMBConnection(userid, password, client_machine_name, remote_machine_name, use_ntlm_v2 = True) 
conn.connect(server_ip, 139) 
filelist = conn.listPath('shared_folder_name', '/') 

लौटे filelist SharedFile की कई घटनाओं के लिए किया जाएगा।

अधिक उदाहरण pysmb स्रोत पैकेज में tests/SMBConnectionTests फ़ोल्डर में पाए जा सकते हैं।

+1

धन्यवाद। Client_machine_name क्या हैं, और remote_machine_name चर भी दिखने वाले हैं? मैं किस पते का उपयोग करता हूं? क्या मैं रिमोट नाम में "smb: //" शामिल करता हूं? – hendrixski

6

मैं हाल ही में नेटवर्क शेयर गणना के लिए pysmb का उपयोग कर रहा हूं, और पाया कि अच्छे/पूर्ण उदाहरण ढूंढना इतना आसान नहीं है। मैं एक छोटे से स्क्रिप्ट है कि मैं pysmb साथ SMB शेयर की गणना के लिए लिखा था का उल्लेख चाहते हैं: https://github.com/n3if/scripts/tree/master/smb_enumerator

पूर्णता के लिए, भी, मैं यहाँ पोस्ट कोड का टुकड़ा है कि कनेक्शन और गणन सिद्ध:

try: 
    conn = SMBConnection(username,password,'name',system_name,domain,use_ntlm_v2=True, 
         sign_options=SMBConnection.SIGN_WHEN_SUPPORTED, 
         is_direct_tcp=True) 
    connected = conn.connect(system_name,445) 

    try: 
     Response = conn.listShares(timeout=30) # obtain a list of shares 
     print('Shares on: ' + system_name) 

     for i in range(len(Response)): # iterate through the list of shares 
      print(" Share[",i,"] =", Response[i].name) 

      try: 
       # list the files on each share 
       Response2 = conn.listPath(Response[i].name,'/',timeout=30) 
       print(' Files on: ' + system_name + '/' + " Share[",i,"] =", 
             Response[i].name) 

        for i in range(len(Response2)): 
         print(" File[",i,"] =", Response2[i].filename) 

      except: 
       print('### can not access the resource') 
    except: 
     print('### can not list shares')  
except: 
    print('### can not access the system') 
+1

क्या होगा यदि सांबा सर्वर में 'GUEST' लॉगिन है। उस मामले में उपयोगकर्ता नाम और पासवर्ड फ़ील्ड के लिए क्या प्रदान करना है? – user2033758

+1

मैं उपयोगकर्ता = GUEST और पासवर्ड = '' कहूंगा लेकिन मुझे इसे आजमा देना चाहिए। – neif

+2

आपने मुझे बचा लिया है। मैं PySmbClient का उपयोग कर रहा था लेकिन इसे काम पर नहीं मिला। धन्यवाद। – grantathon