ApplyGateWay bug fix

This commit is contained in:
cuu 2019-01-08 22:29:20 +08:00
parent 6c0bbb1101
commit 6ccb04ce06
2 changed files with 21 additions and 4 deletions

View File

@ -271,15 +271,16 @@ func (self *GateWayPage) ApplyGateWay( gateway string ) bool {
out := UI.System("sudo ifconfig usb0 | grep inet | tr -s \" \"| cut -d \" \" -f3") out := UI.System("sudo ifconfig usb0 | grep inet | tr -s \" \"| cut -d \" \" -f3")
if len(out) > 7 { if len(out) > 7 {
if strings.Contains(out,"error") == false { if strings.Contains(out,"error") == false {
out = strings.Trim(out,"\r\n ")
parts := strings.Split(out,".") parts := strings.Split(out,".")
if len(parts) == 4 { // IPv4 if len(parts) == 4 { // IPv4
tmp,err := strconv.Atoi(parts[3]) tmp,err := strconv.Atoi(parts[3])
if err == nil { if err == nil {
tmp = tmp +1 if tmp == 0 {
if tmp > 255 { tmp = tmp +1
tmp = 255 }else if tmp > 0 {
tmp = tmp -1
} }
parts[3] = strconv.Itoa(tmp) parts[3] = strconv.Itoa(tmp)
ipaddress := strings.Join(parts,".") ipaddress := strings.Join(parts,".")
UI.System( fmt.Sprintf("sudo route add default gw %s",ipaddress) ) UI.System( fmt.Sprintf("sudo route add default gw %s",ipaddress) )

View File

@ -259,3 +259,19 @@ func System(cmd string) string {
return ret return ret
} }
func SystemTrim(cmd string) string {
ret := ""
out,err := exec.Command("bash","-c",cmd).Output()
if err != nil {
if _, ok := err.(*exec.ExitError); ok {
//exit code !=0 ,but it can be ignored
}else{
fmt.Println(err)
}
}else {
ret = string(out)
}
return strings.Trim(ret,"\r\n")
}