英文:
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&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
and my <em>golang</em> code as follows
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)
}
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 := "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)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论