This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

Reset POE Port on Unifi Switch - Connecting to a device via SSH Key
#1
Hi

I'd like to automate the power cycle of a POE port on a Unifi Switch.   The Unifi docs say this can be achieved by connecting to the device via a SSH key and then issuing some telnet commands.   I can run these commands ok from the terminal window of my desktop, however am not to sure how to initial a ssh connection from logic machine / CBUS SHAC.

Here the sequence of commands that run ok from my desktop Mac to power cycle port 15.

Code:
ssh -i /Users/james/.ssh/id_rsa admin@192.168.1.100#
telnet localhost 23
enable
configure
interface 0/15
poe opmode shutdown
poe opmode auto
exit
exit
exit
telnet localhost 23
exit


Here's the beginnings of my LM script to issue the telnet commands.  Can anyone advise how to modify the script to SSH to the switch with an SSH key (the key is in the id_rsa file in the above example)

Code:
function resetPOESwitchPort(switchport)
 
local socket = require("socket").tcp()
local response, err = socket:connect('192.168.1.100', 23)

if response then

  -- SSH connection established so issue telnet commands
  socket:send('telnet localhost 23\r\n')

  response = socket:receive(5)
  log(response)
  socket:send('telnet localhost 23\r\n')
   
  response = socket:receive(5)
  log(response)
  socket:send('enable\r\n')
   
  response = socket:receive(5)
  log(response)
  socket:send('configure\r\n')
   
  response = socket:receive(5)
  log(response)
  socket:send('interface 0/' .. switchport .. '\r\n')
   
  response = socket:receive(5)
  log(response)
  socket:send('poe opmode shutdown\r\n')
   
  response = socket:receive(5)
  log(response)
  socket:send('poe opmode auto\r\n')
   
  response = socket:receive(5)
  log(response)
  socket:send('exit\r\n')
   
  response = socket:receive(5)
  log(response)
  socket:send('exit\r\n')
   
  response = socket:receive(5)
  log(response)
  socket:send('exit\r\n')
   
  response = socket:receive(5)
  log(response)
  socket:send('telnet localhost 23\r\n')
   
  response = socket:receive(5)
  log(response)
  socket:send('exit\r\n')

  response = socket:receive()
  log(response)
  socket:close()
 
else
 
  -- No connnection response
    log ('Unable to connect to switch')
  tcp:close()
 
end

end


Any advice would be greatly appreciated 

Kind Regards
James
Reply


Messages In This Thread
Reset POE Port on Unifi Switch - Connecting to a device via SSH Key - by jamesng - 02.02.2021, 13:02

Forum Jump: