-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
TaceyWong
committed
Nov 4, 2021
1 parent
2420e20
commit c1c6259
Showing
8 changed files
with
224 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module github.com/g-lib/wifi-password | ||
|
||
go 1.16 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |