0

webclient を使用して Cacti にログインする方法を知っている人はいますか?
PowerShell を使用して Web サイト (Realm : Local) にログインしようとしています。
ただし、ログインページで立ち往生しました。
ここに画像の説明を入力

このページの HTML コード:

<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>   

これが私のpowershellコードです:

$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")
4

1 に答える 1

1

Web クライアントの解決策について具体的に尋ねられたので、それはあなたが求めたものではないことを理解していますが、これは私が個人的に経験したことなので、私があなたに与えることができるものです. Internet Explorer の Com オブジェクトを使用すると、ページを読み込み、必要なオブジェクトを選択し、それらの値を設定できます。送信ボタンの HTML は表示されませんが、名前を見つけて、ここにあるコードを更新できると確信しています。ボタン名を LoginBtn のままにしたのは、それが私のスクリプト用だったからです。実際のログイン ページの URL が「pokey.cacti.com/login」であるとは思えないため、明らかに URL も更新する必要があります。

$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()

うまくいけば、それが回避策として機能します。明らかに、WebClient 名前空間を調べる必要があるのは、それが私のニーズのいくつかに対するより良い代替手段である可能性があるためです.

わかりました、それであなたは写真を撮りたかったのです。ここでは、Win7 以上を使用していると仮定しています。そうでない場合はお知らせください。とにかく、BITS モジュールをロードして画像をダウンロードすることを前提としています。

Import-Module BitsTransfer
$display = $url.Split('/')[-1]
Start-BITSTransfer $url 'C:\temp\test.jpg' -Prio High -Display $display
于 2014-03-17T16:18:05.303 に答える