Hi, I 'm doing a project and I need to create a telnet connection , telnet to send commands to a device, someone could help me about it. regards
the idea is to establish a telnet connection with a telnet server is already listening and once established the connection telnet command enivar
1. connect
2. sending root
3. control command wait 1sec
4. Sending poweroff
Help me please.....
@IBAction func btnenviar(sender: UIBarButtonItem) {
let addr = "192.168.42.1"
let port = 23
var str : String = ""
var inp :NSInputStream?
var out :NSOutputStream?
NSStream.getStreamsToHostWithName(addr, port: port, inputStream: &inp, outputStream: &out)
let inputStream = inp!
let outputStream = out!
inputStream.open()
outputStream.open()
//envio usuario root, no hace falta enviar password comprobado con un software de cliente telnet
str="root"
var writeData = [UInt8]((str + "\n").utf8)
outputStream.write(&writeData, maxLength: writeData.count)
//envio codigo de espera de 1 seg al dispositivo
str="sleep 1"
writeData = [UInt8]((str + "\n").utf8)
outputStream.write(&writeData, maxLength: writeData.count)
/
/
// envio comando de apagado.
str="poweroff"
writeData = [UInt8]((str + "\n").utf8)
outputStream.write(&writeData, maxLength: writeData.count)
outputStream.close()
}
It does not fail but does not work
Do you actually need to implement the telnet protocol? If so, that’s quite complex. OTOH, opening up a TCP connection and sending and receiving bytes is relatively straightforward.
My ‘go to’ sample code for this is RemoteCurrency.
As for the code you posted, I generally recommend against using the network synchronously (which is what you’re doing) but it should work. The most likely problem is the line endings. Internet protocols generally use CR LF (
\r\n in Swift) and you’re using just LF (
\n).
Share and Enjoy
—
Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware
let myEmail = "eskimo" + "1" + "@apple.com"WWDC runs Mon, 13 Jun through to Fri, 17 Jun. During that time all of DTS will be at the conference, helping folks out face-to-face. http://developer.apple.com/wwdc/