1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| import org.jsoup.Connection import org.jsoup.Jsoup import java.util.regex.Pattern
fun main(args: Array<String>) { val username = "admin" val password = "123456"
val gateway = "http://192.168.1.1" val indexUrl = "$gateway/cgi-bin/luci" val rebootUrl = "$gateway/cgi-bin/luci/admin/reboot"
val response = Jsoup.connect(indexUrl) .data("username", username) .data("psd", password) .method(Connection.Method.POST) .followRedirects(false) .execute()
when (response.statusCode()) { 200 -> println("用户名或密码错误")
302 -> { val cookies = response.cookies() val html = Jsoup.connect("$gateway${response.header("Location")}") .cookies(cookies) .get() .html() val matcher = Pattern.compile("token: '(.*?)'").matcher(html) if (matcher.find()) { Jsoup.connect(rebootUrl) .cookies(cookies) .data("token", matcher.group(1)) .post() } } } }
|