Skip to content

Commit

Permalink
completed(only test linux)
Browse files Browse the repository at this point in the history
  • Loading branch information
TaceyWong committed Nov 4, 2021
1 parent 2420e20 commit c1c6259
Show file tree
Hide file tree
Showing 8 changed files with 224 additions and 2 deletions.
47 changes: 45 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,45 @@
# wifi-password
https://github.com/kevva/wifi-password
# WIFI Password

> Get current wifi password

## Install

```shell
$ go get github.com/g-lib/wifi-password
```

## Usage

```go
package main

import (
"fmt"
"github.com/g-lib/wifi-password"
)


func main(){
ssid,_ :=wifipw.WIFISSID()
password,_ := wifipw.WIFIPassword(ssid)
fmt.Println(ssid,password)
}
```

OR

```go
package main

import (
"fmt"
"github.com/g-lib/wifi-password"
)


func main(){
ssid,password,_ := wifipw.GetWIFIPassword()
fmt.Println(ssid,password)
}
```
26 changes: 26 additions & 0 deletions cmd/wifipw.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package main

import (
"flag"
"fmt"
"os"

wifipw "github.com/g-lib/wifi-password"
)

func usage() {
fmt.Fprintf(os.Stderr, `wifipw is a command to get the password of current wifi.
usage: [sudo] wifipw
`)
flag.PrintDefaults()
fmt.Fprintf(os.Stderr, `
`)
os.Exit(0)
}

func main() {
flag.Usage = usage
flag.Parse()
ssid, password, _ := wifipw.GetWIFIPassword()
fmt.Printf("SSID:%s\nPassword:%s\n", ssid, password)
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/g-lib/wifi-password

go 1.16
15 changes: 15 additions & 0 deletions wifipw.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package wifipw

func GetWIFIPassword() (ssid, password string, err error) {
if s, err := WIFISSID(); err != nil {
return "", "", err
} else {
ssid = s
}
if pw, err := WIFIPassword(ssid); err != nil {
return ssid, "", err
} else {
password = pw
}
return
}
35 changes: 35 additions & 0 deletions wifipw_darwin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package wifipw

import (
"bytes"
"fmt"
"os/exec"
)

func WIFIPassword(ssid string) (string, error) {
output, err := exec.Command("sh", "-c",
"security find-generic-password -D AirPort network password -wa "+ssid).CombinedOutput()
if err != nil {
return "", err
}
return string(output), nil
}

func WIFISSID() (name string, err error) {
var stdout bytes.Buffer
cmdStr := "/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -I"
cmd := exec.Command("sh", "-c", cmdStr)
cmd.Stdout = &stdout
if err := cmd.Run(); err != nil {
return "", errors.New(stderr.String())
}
stdoutStr := stdout.String()
if strings.Contains(stdout, "AirPort: Off") {
return "", errors.New("Wi-Fi is turned off")
}
ret := regexp.MustCompile(`^\s*SSID: (.+)\s*$`).FindStringSubmatch(stdout.String())
if len(ret) < 1 {
return "", errors.New("Could not get SSID")
}
return ret[1], nil
}
46 changes: 46 additions & 0 deletions wifipw_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package wifipw

import (
"bytes"
"errors"
"fmt"
"os/exec"
"regexp"
"strings"
)

func WIFIPassword(ssid string) (password string, err error) {
paths := []string{
fmt.Sprintf("/etc/NetworkManager/system-connections/%s", ssid),
fmt.Sprintf("/etc/NetworkManager/system-connections/%s.nmconnection", ssid),
}
outputStr := ""
for _, p := range paths {
output, err := exec.Command("sudo", "cat", p).CombinedOutput()
if err == nil {
outputStr = string(output)
break
}
}
if outputStr == "" {
return "", errors.New("could not get password")
}
ret := regexp.MustCompile(`(?:psk|password)=(.+)`).FindStringSubmatch(outputStr)
if len(ret) < 1 {
return "", errors.New("could not get SSID")
}
return strings.TrimSpace(ret[1]), nil
}

func WIFISSID() (name string, err error) {
var stdout bytes.Buffer
var stderr bytes.Buffer
cmd := exec.Command("sh", "-c", "iwgetid -r")
cmd.Stdout = &stdout
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
return "", errors.New(stderr.String())
}

return strings.TrimSpace(stdout.String()), nil
}
20 changes: 20 additions & 0 deletions wifipw_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package wifipw_test

import (
"testing"

wifipw "github.com/g-lib/wifi-password"
)

func TestWIFISSID(t *testing.T) {
t.Log(wifipw.WIFISSID())
}

func TestWIFIPassword(t *testing.T) {
ssid, _ := wifipw.WIFISSID()
t.Log(wifipw.WIFIPassword(ssid))
}

func TestGetWIFIPassword(t *testing.T) {
t.Log(wifipw.GetWIFIPassword())
}
34 changes: 34 additions & 0 deletions wifipw_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package wifipw

import (
"bytes"
"fmt"
"os/exec"
)

func WIFIPassword(ssid string) (string,error) {
output, err := exec.Command("cmd", "/C", "netsh","wlan",
"show","profile","name="+ssid,"key=clear").CombinedOutput()
if err != nil{
return "",err
}
ret := regexp.MustCompile(`^\s*Key Content\s*: (.+)\s*$`).FindStringSubmatch(string(output))
if len(ret) < 1 {
return "", errors.New("Could not get password")
}
return ret[1], nil
}

func WIFISSID() (string, error) {
var stdout bytes.Buffer
cmdStr := "netsh wlan show interface"
cmd,err := exec.Command("cmd", "/C", "netsh","wlan","show","interface")..CombinedOutput()
if err != nil {
return "", err
}
ret := regexp.MustCompile(`^\s*SSID: (.+)\s*$`).FindStringSubmatch(string(cmd))
if len(ret) < 1 {
return "", errors.New("Could not get SSID")
}
return ret[1], nil
}

0 comments on commit c1c6259

Please sign in to comment.