Skip to content

Commit 71d0450

Browse files
committed
Version 0.1.1
1 parent 83757d6 commit 71d0450

File tree

15 files changed

+363
-14
lines changed

15 files changed

+363
-14
lines changed

Output.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Startup

SSHCommand/RunSSH.go

Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
package SSHCommand
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"github.com/labstack/gommon/log"
7+
"golang.org/x/crypto/ssh"
8+
"io"
9+
"os"
10+
)
11+
12+
type ScutInfo struct {
13+
Username, Password string
14+
AcIP, AcName string
15+
}
16+
17+
type SSHClass struct {
18+
client *ssh.Client
19+
session *ssh.Session
20+
err error
21+
status bool // 记录上一操作是否成功
22+
out io.Writer // 用于保存输出SSH运行结果
23+
ScutInfo // 保存信息
24+
}
25+
26+
func (s *SSHClass) SetOut(out io.Writer) {
27+
// 先于 NewSession 被调用
28+
s.out = out
29+
}
30+
31+
func (s *SSHClass) NewSession() {
32+
// 每条命令都需要一个新 Session
33+
if s.status != true {
34+
log.Errorf("Dial Fails: %s", s.err.Error())
35+
return
36+
}
37+
s.session, s.err = s.client.NewSession()
38+
s.Assert()
39+
40+
// 设置输出
41+
s.session.Stdout = s.out
42+
}
43+
44+
func (s *SSHClass) Close() {
45+
if s.session != nil {
46+
s.session.Close()
47+
s.session = nil
48+
s.status = true
49+
}
50+
}
51+
52+
func (s *SSHClass) Assert() {
53+
if s.err != nil {
54+
log.Error(s.err.Error())
55+
s.status = false
56+
return
57+
}
58+
s.status = true
59+
}
60+
61+
func (s *SSHClass) RunCommand(cmd string) {
62+
s.NewSession()
63+
defer s.Close()
64+
65+
if s.status != true {
66+
log.Errorf("New Session Fails: %s", s.err.Error())
67+
return
68+
}
69+
err := s.session.Run(cmd)
70+
if err != nil {
71+
log.Error(err.Error())
72+
}
73+
}
74+
75+
type accountInfo struct {
76+
Username, Password string
77+
AcIP, AcName string
78+
}
79+
80+
func (s *SSHClass) preLoadInfo() {
81+
// 遇加载信息
82+
infoFile, err := os.ReadFile("./Account.json")
83+
if err != nil {
84+
log.Info(err.Error())
85+
return
86+
}
87+
accInfo := new(accountInfo)
88+
json.Unmarshal(infoFile, accInfo)
89+
90+
s.Username = accInfo.Username
91+
s.Password = accInfo.Password
92+
s.AcIP = accInfo.AcIP
93+
s.AcName = accInfo.AcName
94+
}
95+
96+
func (s *SSHClass) saveInfo() {
97+
infoFile, err := os.Create("./Account.json")
98+
if err != nil {
99+
log.Error(err.Error())
100+
}
101+
102+
accInfo := new(accountInfo)
103+
accInfo.Username = s.Username
104+
accInfo.Password = s.Password
105+
accInfo.AcIP = s.AcIP
106+
accInfo.AcName = s.AcName
107+
108+
b, err := json.Marshal(accInfo)
109+
if err != nil {
110+
log.Error(err.Error())
111+
}
112+
infoFile.Write(b)
113+
infoFile.Close()
114+
}
115+
116+
func (s *SSHClass) RunSetScutInfo(username, password string) {
117+
log.Info("RunSetScutInfo is called")
118+
s.preLoadInfo()
119+
120+
s.Username = username
121+
s.Password = password
122+
s.HelpSetInfo()
123+
}
124+
125+
func (s *SSHClass) RunSetAcInfo(acIP, acName string) {
126+
log.Info("RunSetAcInfo is called")
127+
s.preLoadInfo()
128+
129+
s.AcIP = acIP
130+
s.AcName = acName
131+
s.HelpSetInfo()
132+
}
133+
134+
func (s *SSHClass) RunWiredLogin() {
135+
cmd := fmt.Sprintf("chmod +x scutlogin.sh && ./scutlogin.sh")
136+
log.Info("RunWiredLogin is called")
137+
s.RunCommand(cmd)
138+
}
139+
140+
func (s *SSHClass) RunWirelessLogin() {
141+
cmd := fmt.Sprintf("chmod +x wl_login.sh && ./wl_login.sh")
142+
log.Info("RunWirelessLogin is called")
143+
s.RunCommand(cmd)
144+
}
145+
146+
func (s *SSHClass) HelpSetInfo() {
147+
// 辅助函数
148+
s.saveInfo()
149+
150+
cmd := fmt.Sprintf("echo %s > SCUT_info.txt", s.Username)
151+
cmd += fmt.Sprintf(" && echo %s >> SCUT_info.txt", s.Password)
152+
cmd += fmt.Sprintf(" && echo %s >> SCUT_info.txt", s.AcIP)
153+
cmd += fmt.Sprintf(" && echo %s >> SCUT_info.txt", s.AcName)
154+
s.RunCommand(cmd)
155+
}
156+
157+
func (s *SSHClass) RunSyncTime() {
158+
cmd := fmt.Sprintf("ntpd -n -q -p ntp.aliyun.com")
159+
160+
log.Info("RunSyncTime is called")
161+
s.RunCommand(cmd)
162+
}
163+
164+
func (s *SSHClass) RunAutoLogin() {
165+
log.Info("RunAutoLogin is called")
166+
167+
cmd := fmt.Sprintf("crontab -l | sed '/.*\\/root\\/scutlogin.sh.*/d' > temp_cron")
168+
cmd += fmt.Sprintf(" && echo \"5 6 * * 1-5 /root/scutlogin.sh\" >> temp_cron")
169+
cmd += fmt.Sprintf(" && crontab temp_cron\nrm temp_cron")
170+
171+
s.RunCommand(cmd)
172+
}
173+
174+
func (s *SSHClass) CancelAutoLogin() {
175+
log.Info("CancelAutoLogin is called")
176+
177+
cmd := fmt.Sprintf("crontab -l | sed '/.*\\/root\\/scutlogin.sh.*/d' > temp_cron")
178+
cmd += fmt.Sprintf(" && crontab temp_cron\nrm temp_cron")
179+
s.RunCommand(cmd)
180+
}
181+
182+
type routerConfig struct {
183+
Addr string `json:"addr"`
184+
Password string `json:"password"`
185+
Port string `json:"port"`
186+
}
187+
188+
func NewRunSSH() *SSHClass {
189+
buf, err := os.ReadFile("./config.json")
190+
if err != nil {
191+
log.Error(err.Error())
192+
f, _ := os.Create("./config.json")
193+
f.Close()
194+
return nil
195+
}
196+
197+
rConfig := new(routerConfig)
198+
err = json.Unmarshal(buf, rConfig)
199+
if err != nil {
200+
log.Error(err.Error())
201+
return nil
202+
}
203+
204+
config := ssh.ClientConfig{
205+
Config: ssh.Config{},
206+
User: "root",
207+
Auth: []ssh.AuthMethod{ssh.Password(rConfig.Password)},
208+
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
209+
}
210+
SSHObj := new(SSHClass)
211+
addrPort := fmt.Sprintf("%s:%s", rConfig.Addr, rConfig.Port)
212+
SSHObj.client, SSHObj.err = ssh.Dial("tcp", addrPort, &config)
213+
SSHObj.Assert()
214+
215+
return SSHObj
216+
}

ScutLog.log

Whitespace-only changes.

config.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"addr":"192.168.1.101",
3+
"password": "KrxkKrxk",
4+
"port": "22"
5+
}

frontend/src/components/Demo.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
<MyAvatar/>
7070
</Header>
7171
<Content class="layout-content">
72-
<h2>自由定制,真正属于你的校园网路由器</h2>
72+
<h2>自由定制,打造您的专属校园网路由</h2>
7373
<img src="../assets/Scut-Router.png"/>
7474
</Content>
7575
<Footer class="layout-footer">

frontend/src/components/Login.vue

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
</div>
2020
</template>
2121
<script>
22+
import {RunAutoLogin, CancelAutoLogin, RunSetScutInfo} from "../../wailsjs/go/SSHCommand/SSHClass";
23+
2224
export default {
2325
data() {
2426
return {
@@ -27,11 +29,14 @@ export default {
2729
},
2830
methods: {
2931
handleSubmit(valid, { username, password }) {
30-
if (valid) {
31-
this.$Modal.info({
32-
title: '输入的内容如下:',
33-
content: `username: ${username} | password: ${password} | type: ${this.AccountType}`,
34-
})
32+
if(valid) {
33+
RunSetScutInfo(username, password)
34+
if(this.AccountType == "本科生") {
35+
RunAutoLogin()
36+
} else {
37+
CancelAutoLogin()
38+
}
39+
alert('完成')
3540
}
3641
},
3742
},

frontend/src/views/ACInfo.vue

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<script>
22
import MyMenu from '../components/Menu.vue'
3+
import {RunSetAcInfo} from "../../wailsjs/go/SSHCommand/SSHClass";
34
45
const ACList = [] // 复合型const变量只确保地址不变而非值不变,故可修改
56
for (let i = 1; i < 20; i += 1) {
@@ -20,7 +21,9 @@ export default {
2021
},
2122
methods: {
2223
OnSubmit() {
23-
alert(`Wlan_ac_name: ${this.AcName}\nWlan_ac_ip: ${this.AcIp}`)
24+
RunSetAcInfo(this.AcIp, this.AcName)
25+
alert('完成')
26+
// alert(`Wlan_ac_name: ${this.AcName}\nWlan_ac_ip: ${this.AcIp}`)
2427
}
2528
},
2629
name: 'ACInfo'

frontend/src/views/SyncTime.vue

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import MyMenu from '../components/Menu.vue'
33
import Success from "../components/Success.vue"
44
import Fail from "../components/Fail.vue"
5+
import {RunSyncTime} from "../../wailsjs/go/SSHCommand/SSHClass";
56
67
export default {
78
components: {
@@ -18,8 +19,9 @@ export default {
1819
},
1920
methods: {
2021
TryConnect() {
22+
RunSyncTime()
2123
this.SyncStatus = true
22-
this.FailStatus = true
24+
// this.FailStatus = true
2325
}
2426
}
2527
}

frontend/src/views/WiredLogin.vue

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import MyMenu from '../components/Menu.vue'
33
import Success from "../components/Success.vue"
44
import Fail from "../components/Fail.vue"
5+
import {RunWiredLogin} from "../../wailsjs/go/SSHCommand/SSHClass";
56
67
export default {
78
components: {
@@ -18,8 +19,9 @@ export default {
1819
},
1920
methods: {
2021
TryConnect() {
22+
RunWiredLogin()
2123
this.ConnectStatus = true
22-
this.FailStatus = true
24+
// this.FailStatus = true
2325
}
2426
}
2527
}

frontend/src/views/WirelessLogin.vue

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import MyMenu from '../components/Menu.vue'
33
import Success from '../components/Success.vue'
44
import Fail from "../components/Fail.vue"
5+
import {RunWirelessLogin} from "../../wailsjs/go/SSHCommand/SSHClass";
56
67
export default {
78
components: {
@@ -18,8 +19,9 @@ export default {
1819
},
1920
methods: {
2021
TryConnect() {
22+
RunWirelessLogin()
2123
this.ConnectStatus = true
22-
this.FailStatus = true
24+
// this.FailStatus = true
2325
}
2426
}
2527
}

0 commit comments

Comments
 (0)