powershellwebclientcacti

Powershell - How to use webclient login a webpage (Cacti)


Does anyone know how to use webclient to login Cacti?
I am trying to use PowerShell to log into a website (Realm : Local).
However I stuck on login page.
enter image description here

Html code for this page :

<tr>
    <td colspan="2">Please enter your user name and password below:</td>
</tr>
<tr style="height: 10px;"><td></td></tr>
<tr>
    <td>User Name:</td>
    <td><input name="login_username" style="width: 295px;" type="text" size="40" value=""></td>
</tr>
<tr>
    <td>Password:</td>
    <td><input name="login_password" style="width: 295px;" type="password" size="40"></td>
</tr>
<tr>
    <td>Realm:</td>
    <td>
        <select name="realm" style="width: 295px;"><option value="local">Local</option><option value="ldap" selected="">LDAP</option>
        </select>
    </td>
</tr>
    <tr style="height: 10px;"><td></td></tr>
<tr>
    <td><input type="submit" value="Login"></td>
</tr>   

Here is my powershell code :

$username="MyAccount"
$passowrd="MyPassowrd"
$realm="local"

$webclient = new-object System.Net.WebClient
$webclient.Credentials = new-object System.Net.NetworkCredential($username, $password, $realm)
$webclient.DownloadFile($url, "C:\temp\test.jpg")

Solution

  • I realize that it is not what you asked for since you specifically asked about a webclient resolution, but since this is what I personally have experience with it's what I can give you. Using an Internet Explorer Com Object you could load the page, select the objects you need, and set their values. I don't see the HTML for the submit button there, but I'm sure you can find the name for it and update the code I have here. I left the button name as LoginBtn since that what it was for my script. Obviously you are going to need to update the URL as well since I doubt your actual login page's URL is "pokey.cacti.com/login".

    $IE = New-Object -ComObject InternetExplorer.Application
    Do {Start-Sleep 1} Until (!($IE.Busy))
    $IE.Navigate('https://pokey.cacti.com/login')
    Do {Start-Sleep 1} Until (!($IE.Busy))
    $Doc = $IE.Document
    $UserNameField = $Doc.getElementById('login_username')
    $UserNameField.value = $username
    $PassField = $Doc.getElementById('login_password')
    $PassField.value = $password
    $RealmDropdown = $Doc.getElementById('realm')
    $RealmDropdown.value = 'Local'
    $LoginButton = $Doc.getElementById('LoginBtn')
    $LoginButton.setActive()
    $LoginButton.click()
    

    Hopefully that works as a work-around for you. Obviously I need to go look into the WebClient namespace as it may be a better alternative for some of my needs, but so far the com object has worked for me, and hopefully will for you too.

    Ok, so you wanted to get a picture. I'm making an assumption here that you're on Win7 or higher, so if that isn't the case let me know. Anyway, that assumed load up the BITS module to download the picture.

    Import-Module BitsTransfer
    $display = $url.Split('/')[-1]
    Start-BITSTransfer $url 'C:\temp\test.jpg' -Prio High -Display $display