Code by Scott שאול בן ישוע
Send Telnet Command.groovy 2.89 KB
Newer Older
Sha'ul ben Yeshua's avatar
Sha'ul ben Yeshua committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*
   A simple driver that will run a command via telnet
   Useful with Rpi's

   Copyright 2019 Sha'ul ben Yeshua / Scott Grayban

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

metadata {
Sha'ul ben Yeshua's avatar
Sha'ul ben Yeshua committed
21
22
23
24
25
26
27
	definition (
	name: "Send Telnet Command",
	namespace: "sgrayban",
	author: "Scott Grayban",
	importUrl: "http://sgrayban.borgnet.online:8081/hubitat-public-repo/Send%20Telnet%20Command.groovy"
	)

Sha'ul ben Yeshua's avatar
Sha'ul ben Yeshua committed
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
    {        
        capability "Initialize"
        capability "Telnet"
        capability "Switch"
        
        command "Reboot Device"
	}
	
	attribute "Telnet", "String"

	preferences {
		input "ip", "text", title: "Target System IP", description: "", required: true, displayDuringSetup: true
		input "port", "text", title: "Target System Telnet Port", description: "", required: true, displayDuringSetup: true
		input "username", "text", title: "Username", description: "", required: true, displayDuringSetup: true
		input "password", "text", title: "Password", description: "", required: true, displayDuringSetup: true
        input name: "logEnable", type: "bool", title: "Enable debug logging", defaultValue: true
    }
}

def initialize(){
    log.debug "Connecting to telnet - IP = ${ip}, Port = ${port.toInteger()}, Username = ${username}, Password = ${password}"
    telnetConnect([terminalType: 'VT100'], ip, port.toInteger(), username, password)
}

def installed(){
    log.info "Installed"
    log.info "Please enter your login credentials."
}

def updated(){
    log.info "Preferences updated"
Sha'ul ben Yeshua's avatar
Sha'ul ben Yeshua committed
59
    runIn(5,sendEvent(name: "switch", value: "off"))
Sha'ul ben Yeshua's avatar
Sha'ul ben Yeshua committed
60
61
62
63
64
65
66
67
68
}

def on() {
    initialize()
	if (logEnable) log.info "Send Reboot"
	sendEvent(name: "switch", value: "on")
    def msg = "sudo reboot"
    if (logEnable) log.info "Sending msg = ${msg}"
	sendHubCommand(new hubitat.device.HubAction("""$msg\r\n""", hubitat.device.Protocol.TELNET))
Sha'ul ben Yeshua's avatar
Sha'ul ben Yeshua committed
69
    runIn(5,sendEvent(name: "switch", value: "off"))
Sha'ul ben Yeshua's avatar
Sha'ul ben Yeshua committed
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
}
def reboot(){
    on()
}

def parse(String msg) {
    if (logEnable) log.debug "Telnet Response = ${msg}"
    if (msg == "permitted by applicable law.") {
        sendEvent(name: "Telnet", value: "Connected");
    }
}

def telnetStatus(String status){
	if (logEnable) log.info "telnetStatus- error: ${status}"
	if (status == "receive error: Stream is closed"){
		
		if (logEnable) log.error "Telnet connection dropped..."
        sendEvent(name: "Telnet", value: "Disconnected")
		initialize()
	} else {
		sendEvent(name: "Telnet", value: "Connected")
	}
}
Sha'ul ben Yeshua's avatar
Sha'ul ben Yeshua committed
93