Making a post request in golang

huangapple go评论86阅读模式
英文:

Making a post request in golang

问题

我正在尝试使用golang自动化以下操作:

  • 生成密码(已完成)
  • 向所有设备发送推送通知(已使用pushbullet完成)
  • 更改192.168.0.1上的WiFi路由器密码(需要完成)

192.168.0.1上的WiFi路由器页面
WiFi路由器页面

这是使用Fiddler捕获的原始数据(手动更改时):

POST http://192.168.0.1/goform/form2WlanBasicSetup.cgi HTTP/1.1
Host: 192.168.0.1
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Referer: http://192.168.0.1/d_wlan_basic.asp
Cookie: curShow=
X-Skyzip-Mode: high
Connection: keep-alive
Content-Type: application/x-www-form-urlencoded
Content-Length: 233

domain=1&hiddenSSID=0&ssid=Home&band=9&chan=0&chanwid=1&txRate=0&method_cur=0&method=6&authType=2&length=1&format=1&defaultTxKeyId=1&key1=&pskFormat=0&pskValue=3ADI0NSXEAYOI0M&checkWPS2=1&save=Apply&basicrates=496&operrates=4080

我的golang代码如下:

func RouterPass(pass string) {
	routerUrl := "http://192.168.0.1"
	resource := "/goform/form2WlanBasicSetup.cgi"
	data := url.Values{}
	data.Set("domain", "1")
	data.Add("ssid", "Home")
	data.Add("band", "9")
	data.Add("chan", "0")
	data.Add("chanwid", "1")
	data.Add("txRate", "0")
	data.Add("method_cur", "0")
	data.Add("method", "6")
	data.Add("authType", "2")
	data.Add("length", "1")
	data.Add("format", "1")
	data.Add("defaultTxKeyId", "1")
	data.Add("pskFormat", "0")
	data.Add("pskValue", pass)
	data.Add("checkWPS2", "1")
	data.Add("save", "Apply")
	data.Add("basicrates", "496")
	data.Add("operrates", "4080")

	u, _ := url.ParseRequestURI(routerUrl)
	u.Path = resource
	u.RawQuery = data.Encode()
	urlStr := fmt.Sprintf("%v", u) 

	client := &http.Client{}
	r, _ := http.NewRequest("POST", urlStr, nil)
	// r.Header.Add("Authorization", "auth_token=\"XXXXXXX\"")
	r.Header.Add("Content-Type", "application/x-www-form-urlencoded")
	r.Header.Add("Content-Length", strconv.Itoa(len(data.Encode())))

	resp, _ := client.Do(r)
    fmt.Println(pass)
	fmt.Println(resp.Status)
}

但是我无法更改密码。我做错了什么?

英文:

I'm trying to automate following using golang

  • <strike>generate password</strike> (done)
  • <strike>push notification to all devices</strike> (done using pushbullet)
  • change wifi router password at 192.168.0.1 (needs to be done)

Wifi Router page at 192.168.0.1
Wifi Router page

Here is a raw data captured using <em>Fiddler</em>. (When manually changed)

POST http://192.168.0.1/goform/form2WlanBasicSetup.cgi HTTP/1.1
Host: 192.168.0.1
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Referer: http://192.168.0.1/d_wlan_basic.asp
Cookie: curShow=
X-Skyzip-Mode: high
Connection: keep-alive
Content-Type: application/x-www-form-urlencoded
Content-Length: 233

domain=1&amp;hiddenSSID=0&amp;ssid=Home&amp;band=9&amp;chan=0&amp;chanwid=1&amp;txRate=0&amp;method_cur=0&amp;method=6&amp;authType=2&amp;length=1&amp;format=1&amp;defaultTxKeyId=1&amp;key1=&amp;pskFormat=0&amp;pskValue=3ADI0NSXEAYOI0M&amp;checkWPS2=1&amp;save=Apply&amp;basicrates=496&amp;operrates=4080

and my <em>golang</em> code as follows

func RouterPass(pass string) {
	routerUrl := &quot;http://192.168.0.1&quot;
	resource := &quot;/goform/form2WlanBasicSetup.cgi&quot;
	data := url.Values{}
	data.Set(&quot;domain&quot;, &quot;1&quot;)
	data.Add(&quot;ssid&quot;, &quot;Home&quot;)
	data.Add(&quot;band&quot;, &quot;9&quot;)
	data.Add(&quot;chan&quot;, &quot;0&quot;)
	data.Add(&quot;chanwid&quot;, &quot;1&quot;)
	data.Add(&quot;txRate&quot;, &quot;0&quot;)
	data.Add(&quot;method_cur&quot;, &quot;0&quot;)
	data.Add(&quot;method&quot;, &quot;6&quot;)
	data.Add(&quot;authType&quot;, &quot;2&quot;)
	data.Add(&quot;length&quot;, &quot;1&quot;)
	data.Add(&quot;format&quot;, &quot;1&quot;)
	data.Add(&quot;defaultTxKeyId&quot;, &quot;1&quot;)
	data.Add(&quot;pskFormat&quot;, &quot;0&quot;)
	data.Add(&quot;pskValue&quot;, pass)
	data.Add(&quot;checkWPS2&quot;, &quot;1&quot;)
	data.Add(&quot;save&quot;, &quot;Apply&quot;)
	data.Add(&quot;basicrates&quot;, &quot;496&quot;)
	data.Add(&quot;operrates&quot;, &quot;4080&quot;)

	u, _ := url.ParseRequestURI(routerUrl)
	u.Path = resource
	u.RawQuery = data.Encode()
	urlStr := fmt.Sprintf(&quot;%v&quot;, u) 

	client := &amp;http.Client{}
	r, _ := http.NewRequest(&quot;POST&quot;, urlStr, nil)
	// r.Header.Add(&quot;Authorization&quot;, &quot;auth_token=\&quot;XXXXXXX\&quot;&quot;)
	r.Header.Add(&quot;Content-Type&quot;, &quot;application/x-www-form-urlencoded&quot;)
	r.Header.Add(&quot;Content-Length&quot;, strconv.Itoa(len(data.Encode())))

	resp, _ := client.Do(r)
    fmt.Println(pass)
	fmt.Println(resp.Status)
}

But I'm not able to change password.
What am I doing wrong?

答案1

得分: 4

希望这能帮助其他人。

代码部分如下:

func ChangePassword(password string) {
    hc := http.Client{}
    routerURL := "http://192.168.0.1/goform/form2WlanBasicSetup.cgi"
    form := url.Values{}
    form.Add("domain", "1")
    form.Add("hiddenSSID", "0")
    form.Add("ssid", "Linux-PC")
    form.Add("band", "9")
    form.Add("chan", "0")
    form.Add("chanwid", "1")
    form.Add("txRate", "0")
    form.Add("method_cur", "0")
    form.Add("method", "6")
    form.Add("authType", "2")
    form.Add("length", "1")
    form.Add("format", "1")
    form.Add("defaultTxKeyId", "1")
    form.Add("key1", "")
    form.Add("pskFormat", "0")
    form.Add("pskValue", password)
    form.Add("checkWPS2", "1")
    form.Add("save", "Apply")
    form.Add("basicrates", "496")
    form.Add("operrates", "4080")
    req, err := http.NewRequest("POST", routerURL, strings.NewReader(form.Encode()))
    if err != nil {
        panic(err)
    }
    req.PostForm = form
    req.Header.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0")
    req.Header.Add("Referer", "http://192.168.0.1/d_wlan_basic.asp")
    req.Header.Add("Content-Type", "application/x-www-form-urlencoded")

    fmt.Println(form)
    resp, err := hc.Do(req)
    if err != nil {
        panic(err)
    }
    fmt.Println(resp.Status)
    fmt.Println(password)
}
英文:

Hopefully, this will help others.

Code that worked

func ChangePassword(password string) {
hc := http.Client{}
routerURL := &quot;http://192.168.0.1/goform/form2WlanBasicSetup.cgi&quot;
form := url.Values{}
form.Add(&quot;domain&quot;, &quot;1&quot;)
form.Add(&quot;hiddenSSID&quot;, &quot;0&quot;)
form.Add(&quot;ssid&quot;, &quot;Linux-PC&quot;)
form.Add(&quot;band&quot;, &quot;9&quot;)
form.Add(&quot;chan&quot;, &quot;0&quot;)
form.Add(&quot;chanwid&quot;, &quot;1&quot;)
form.Add(&quot;txRate&quot;, &quot;0&quot;)
form.Add(&quot;method_cur&quot;, &quot;0&quot;)
form.Add(&quot;method&quot;, &quot;6&quot;)
form.Add(&quot;authType&quot;, &quot;2&quot;)
form.Add(&quot;length&quot;, &quot;1&quot;)
form.Add(&quot;format&quot;, &quot;1&quot;)
form.Add(&quot;defaultTxKeyId&quot;, &quot;1&quot;)
form.Add(&quot;key1&quot;, &quot;&quot;)
form.Add(&quot;pskFormat&quot;, &quot;0&quot;)
form.Add(&quot;pskValue&quot;, password)
form.Add(&quot;checkWPS2&quot;, &quot;1&quot;)
form.Add(&quot;save&quot;, &quot;Apply&quot;)
form.Add(&quot;basicrates&quot;, &quot;496&quot;)
form.Add(&quot;operrates&quot;, &quot;4080&quot;)
req, err := http.NewRequest(&quot;POST&quot;, routerURL, strings.NewReader(form.Encode()))
if err != nil {
panic(err)
}
req.PostForm = form
req.Header.Add(&quot;User-Agent&quot;, &quot;Mozilla/5.0 (Windows NT 6.3; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0&quot;)
req.Header.Add(&quot;Referer&quot;, &quot;http://192.168.0.1/d_wlan_basic.asp&quot;)
req.Header.Add(&quot;Content-Type&quot;, &quot;application/x-www-form-urlencoded&quot;)
fmt.Println(form)
resp, err := hc.Do(req)
if err != nil {
panic(err)
}
fmt.Println(resp.Status)
fmt.Println(password)
}

huangapple
  • 本文由 发表于 2016年1月6日 08:09:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/34623523.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定