[Resolved] Extract hosts.csv with script python3



  • Hello,
    How i do to download hosts.csv in script python3
    I try something like that

    import requests
    import csv
    import shutil
    
    Login = "fog"
    Pass = "bar"
    
    site ="http://IP/fog/management/index.php"
    headers = {'User-Agent': 'Mozilla/5.0'}
    payload = {'uname': 'fog', 'upass' : 'bar', 'ulang' : 'English'}
    payload2 = {'fogguiuser' : 'fog', 'fogguipass' : 'bar', 'nojson' : ''}
    
    
    s = requests.Session()
    r = s.post(site + "?node=home", headers=headers, data=payload)
    t = s.get("http://IP/fog/management/index.php?node=host&sub=export")
    page = t.text
    print ("[+] Connexion OK")
    
    r = s.post(site + "/export.php?type=host", headers=headers, data=payload2)
    with open('test.csv', 'wb') as f:
        print ("[+] Récupération du csv")
        t = s.get("http://IP/fog/management/index.php?node=host&sub=export", stream = True)
        print (t.text)
        print ("[+] Enregistrement terminé")
        f.write(t.content)
    f.close()
    

    Don’t work :"(
    Have you an idea ?
    thxs



  • my script work \o/

    #-*- coding: UTF-8 -*-
    import requests
    
    
    headers = {'User-Agent': 'Mozilla/5.0'}
    payload = {'uname': 'fog', 'upass' : 'bar', 'ulang' : 'English', 'login': ''}
    
    headers2 = {'User-Agent': 'Mozilla/5.0', 'Referer': 'http://IP/fog/management/index.php?node=host&sub=export'}
    payload2 = {'fogguiuser' : 'fog', 'fogguipass' : 'bar', 'nojson' : ''}
    
    
    s = requests.Session()
    r = s.post("http://IP/fog/management/index.php?node=home", headers=headers, data=payload)
    print ("[+] Connexion OK")
    s.get("http://IP/fog/management/index.php?node=host&sub=export")
    u = s.post("http://IP/fog/management/export.php?type=host", headers=headers2, data=payload2, stream = True)
    
    mon_fichier = open("C:\\my\\Path\\hosts.txt", "w")
    mon_fichier.write(u.text)
    mon_fichier.close()
    
    print ("[+] Enregistrement terminé")
    

    thxs for your help.


  • Developer

    @Crok Three things I notice:

    • t = s.get("http://IP/fog/management/index.php?node=host&sub=export") is still needed after the login
    • The second s.post() call needs to go to .../export.php?type=host
    • f.write(t.content) should actually be f.write(r.content)


  • Thxs for your response.
    But i’m so bad :"(

    import requests
    import csv
    import shutil
    
    Login = "fog"
    Pass = "bar"
    
    site ="http://IP/fog/management/"
    headers = {'User-Agent': 'Mozilla/5.0'}
    payload = {'uname': 'fog', 'upass' : 'bar', 'ulang' : 'English', 'login': ''}
    payload2 = {'fogguiuser' : 'fog', 'fogguipass' : 'bar', 'nojson' : ''}
    
    
    s = requests.Session()
    r = s.post("http://IP/fog/management/index.php?node=home", headers=headers, data=payload)
    print ("[+] Connexion OK")
    
    
    r = s.post("http://IP/fog/management/index.php?node=host&sub=export", headers=headers, data=payload2)
    with open('host_export.csv', 'wb') as f:
        print ("[+] Récupération du csv")
        f.write(t.content)
        print ("[+] Enregistrement terminé")
    f.close()
    

    Have you another advice ?


  • Developer

    @Crok Three things need adjustment in your script.

    1. Add empty login param to the initial site logon request
    payload = {'uname': 'fog', 'upass': 'password', 'ulang': 'English', 'login': ''}
    
    1. Use different URL for the later export POST request and write CSV data without another GET request
    r = s.post("http://IP/fog/management/export.php?type=host", headers=headers, data=payload2)
    with open('test.csv', 'wb') as f:
        f.write(r.content)
        print ("[+] Récupération du csv")
    f.close()
    

    Good luck!


Log in to reply
 

324
Online

6.2k
Users

13.6k
Topics

127.9k
Posts