赞
踩
openwrt本身带有libssh和libssh2两种三方库。我们需要修改编译使其参与编译
./scripts/feeds update -a
./scripts/feeds install -a
执行make menuconfig操作,勾选你想要的三方库
这里需要注意的主要就是makefile里面记得添加ssh2的依赖,要不编译或者运行会出问题,
其他想做的操作,替换新建文件的逻辑就可以
#include "rk_stick.h" int main(int argc, char **argv) { LIBSSH2_SESSION *session = NULL; const char *hostname = "192.168.180.8"; const char *username = "toybrick"; const char *password = "toybrick"; int rc = -1, sock; // 初始化libssh2库 rc = libssh2_init(0); if (rc != 0) { fprintf(stderr, "Failed to initialize libssh2\n"); return -1; } // 创建套接字 sock = socket(AF_INET, SOCK_STREAM, 0); if (sock == -1) { fprintf(stderr, "Failed to create socket\n"); return -1; } // 连接到远程主机 struct sockaddr_in sin; sin.sin_family = AF_INET; sin.sin_port = htons(22); sin.sin_addr.s_addr = inet_addr(hostname); if (connect(sock, (struct sockaddr*)(&sin), sizeof(struct sockaddr_in)) != 0) { fprintf(stderr, "Failed to connect to remote host\n"); close(sock); return -1; } // 创建SSH会话 session = libssh2_session_init(); if (!session) { fprintf(stderr, "Failed to create SSH session\n"); return -1; } // 连接到SSH服务器 rc = libssh2_session_startup(session, sock); if (rc) { fprintf(stderr, "Failed to establish SSH session\n"); libssh2_session_free(session); return -1; } // 认证 rc = libssh2_userauth_password(session, username, password); if (rc) { fprintf(stderr, "Authentication failed\n"); libssh2_session_free(session); return -1; } // 执行远程命令 LIBSSH2_CHANNEL *channel = libssh2_channel_open_session(session); if (channel) { libssh2_channel_exec(channel, "touch /tmp/test.config"); libssh2_channel_close(channel); libssh2_channel_free(channel); } // 关闭会话 libssh2_session_disconnect(session, "Bye bye, thanks for playing"); libssh2_session_free(session); // 关闭libssh2库 libssh2_exit(); return 0; }
在服务器的tmp目录下查看是否有test.config文件
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。