diff --git a/README.md b/README.md index ac2c647..a44788d 100644 --- a/README.md +++ b/README.md @@ -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) +} +``` \ No newline at end of file diff --git a/cmd/wifipw.go b/cmd/wifipw.go new file mode 100644 index 0000000..223a254 --- /dev/null +++ b/cmd/wifipw.go @@ -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) +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..1e44154 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module github.com/g-lib/wifi-password + +go 1.16 diff --git a/wifipw.go b/wifipw.go new file mode 100644 index 0000000..86ce4fc --- /dev/null +++ b/wifipw.go @@ -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 +} diff --git a/wifipw_darwin.go b/wifipw_darwin.go new file mode 100644 index 0000000..c43baa9 --- /dev/null +++ b/wifipw_darwin.go @@ -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 +} diff --git a/wifipw_linux.go b/wifipw_linux.go new file mode 100644 index 0000000..f38d6fa --- /dev/null +++ b/wifipw_linux.go @@ -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 +} diff --git a/wifipw_test.go b/wifipw_test.go new file mode 100644 index 0000000..22db0c0 --- /dev/null +++ b/wifipw_test.go @@ -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()) +} diff --git a/wifipw_windows.go b/wifipw_windows.go new file mode 100644 index 0000000..53d15be --- /dev/null +++ b/wifipw_windows.go @@ -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 +}