Skip to content
This repository was archived by the owner on Feb 3, 2025. It is now read-only.

Commit f9655f8

Browse files
committed
🎉 Change Hostname Automatically
1 parent 8d1b515 commit f9655f8

1 file changed

Lines changed: 49 additions & 0 deletions

File tree

main.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"math"
99
"net"
1010
"os"
11+
"os/exec"
1112
"os/signal"
1213
"time"
1314

@@ -500,6 +501,54 @@ func main() {
500501
}
501502
}()
502503

504+
//Hostnameを取得する
505+
cmd := exec.Command("hostname")
506+
out, err := cmd.Output()
507+
if err != nil {
508+
panic(err)
509+
}
510+
fmt.Println(string(out))
511+
512+
//もし初期値のraspberrypiだったら
513+
if string(out) == "raspberrypi\n" {
514+
//UNIX時間の下5桁を取得する
515+
unixtime := time.Now().UnixNano()
516+
log.Println("Unixtime is " + fmt.Sprintf("%d", unixtime))
517+
unixtime = unixtime % 100000
518+
519+
//Hostnameをracoon-XXXXXに変更する
520+
hostname := "racoon-" + fmt.Sprintf("%05d", unixtime)
521+
522+
log.Println("Change Hostname To " + hostname)
523+
//Change Hostname
524+
//hostnamectl set-hostname raspberrypi コマンド実行
525+
cmd = exec.Command("hostnamectl", "set-hostname", hostname)
526+
cmd.Run()
527+
528+
//再起動
529+
log.Println("=====Reboot=====")
530+
531+
buzzer := rpio.Pin(12)
532+
buzzer.Mode(rpio.Pwm)
533+
buzzer.Freq(1175 * 64)
534+
buzzer.DutyCycle(16, 32)
535+
time.Sleep(500 * time.Millisecond)
536+
buzzer.DutyCycle(0, 32)
537+
buzzer.Freq(1396 * 64)
538+
buzzer.DutyCycle(16, 32)
539+
time.Sleep(500 * time.Millisecond)
540+
buzzer.DutyCycle(0, 32)
541+
buzzer.Freq(1760 * 64)
542+
buzzer.DutyCycle(16, 32)
543+
time.Sleep(500 * time.Millisecond)
544+
buzzer.DutyCycle(0, 32)
545+
546+
//reboot コマンド実行
547+
cmd = exec.Command("reboot")
548+
cmd.Run()
549+
550+
}
551+
503552
//MyIDで指定したロボットIDを取得
504553
var MyID uint32 = uint32(diptoid)
505554

0 commit comments

Comments
 (0)