Skip to content

Commit

Permalink
feat: ssh: Add new module ssh via bind libssh2
Browse files Browse the repository at this point in the history
Signed-off-by: Jianhui Zhao <[email protected]>
  • Loading branch information
zhaojh329 committed Nov 20, 2023
1 parent 8d52dc2 commit 52c466d
Show file tree
Hide file tree
Showing 8 changed files with 1,212 additions and 0 deletions.
22 changes: 22 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ set(LUA_INSTALL_PREFIX lib/lua/5.3)
option(ECO_SSL_SUPPORT "ssl" ON)
option(ECO_UBUS_SUPPORT "ubus" ON)
option(ECO_MQTT_SUPPORT "mqtt" ON)
option(ECO_SSH_SUPPORT "ssh" ON)

add_executable(eco eco.c)
target_link_libraries(eco PRIVATE ${LIBEV_LIBRARY} ${LUA53_LIBRARIES})
Expand Down Expand Up @@ -151,6 +152,27 @@ if (ECO_MQTT_SUPPORT)
endif()
endif()

if (ECO_SSH_SUPPORT)
pkg_search_module(LIBSSH2 libssh2)
if (LIBSSH2_FOUND)
add_library(ssh MODULE ssh.c)
target_link_libraries(ssh PRIVATE ${LIBSSH2_LIBRARIES})
set_target_properties(ssh PROPERTIES OUTPUT_NAME ssh PREFIX "")

install(
TARGETS ssh
DESTINATION ${LUA_INSTALL_PREFIX}/eco/core
)

install(
FILES ssh.lua
DESTINATION ${LUA_INSTALL_PREFIX}/eco
)
else()
message(WARNING "Not found libssh2. Skip build eco.ssh")
endif()
endif()

install(
TARGETS eco
DESTINATION bin
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Lua-eco also provides some modules for you to build applications quickly:
* `netlink`: Provides operations for inter-process communication (IPC) between both the kernel and userspace processes.
* `nl80211`: Show/manipulate wireless devices and their configuration.
* `termios`: Bind unix API for terminal/serial I/O.
* `ssh`: Bind libssh2.

Would you like to try it? Kinda interesting.

Expand Down
1 change: 1 addition & 0 deletions README_ZH.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ Lua-eco 还提供了一些有用的模块,方便您快速构建应用程序:
* `netlink`: 为内核和用户空间进程之间的进程间通信(IPC)提供操作。
* `nl80211`: 显示/操作无线设备及其配置。
* `termios`: 绑定 unix 接口用于操作终端和串口。
* `ssh`: 绑定 libssh2.

想试试吗?很有趣的!

Expand Down
29 changes: 29 additions & 0 deletions examples/ssh/exec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env eco

local ssh = require 'eco.ssh'

local ipaddr = '127.0.0.1'
local port = 22
local username = 'root'
local password = '1'

local session, err = ssh.new(ipaddr, port, username, password)
if not session then
print('new session fail:', err)
return
end

local data, exit_code, exit_signal = session:exec('uptime')
if not data then
print('exec fail:', exit_code)
return
end

if exit_signal then
print('Got signal:', exit_signal)
else
print('Exit:', exit_code)
print('Output:', data)
end

session:free()
34 changes: 34 additions & 0 deletions examples/ssh/scp_recv.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/env eco

local ssh = require 'eco.ssh'

local ipaddr = '127.0.0.1'
local port = 22
local username = 'root'
local password = '1'

local session, err = ssh.new(ipaddr, port, username, password)
if not session then
print('new session fail:', err)
return
end

-- receive as a string returned
local data, err = session:scp_recv('/etc/os-release')
if not data then
print('recv fail:', err)
return
end

print(data)

-- receive to a local file
local n, err = session:scp_recv('/etc/os-release', '/tmp/os-release')
if not n then
print('recv fail:', err)
return
end

print('Got', n, 'bytes, stored into "/tmp/os-release"')

session:free()
30 changes: 30 additions & 0 deletions examples/ssh/scp_send.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/env eco

local ssh = require 'eco.ssh'

local ipaddr = '127.0.0.1'
local port = 22
local username = 'root'
local password = '1'

local session, err = ssh.new(ipaddr, port, username, password)
if not session then
print('new session fail:', err)
return
end

-- send a string
local ok, err = session:scp_send('12345\n', '/tmp/test1')
if not ok then
print('send fail:', err)
return
end

-- send a file
local ok, err = session:scp_sendfile('test', '/tmp/test2')
if not ok then
print('send fail:', err)
return
end

session:free()
Loading

0 comments on commit 52c466d

Please sign in to comment.