如何使用C中的OpenSSH库执行任意命令

发布于 2025-01-30 05:06:33 字数 913 浏览 3 评论 0原文

我是初学者C程序员,我得到了一个旧的项目,请使用OpenSSH库( https://github.com/ openssh/openssh-portable ),其中我需要通过SSH连接并在设备上执行一组命令。我能够验证:

if ((res = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
    (res = sshpkt_put_cstring(ssh, login)) != 0 ||
    (res = sshpkt_put_cstring(ssh, service)) != 0 ||
    (res = sshpkt_put_cstring(ssh, ssh_method_name)) != 0 ||
    (res = sshpkt_put_u8(ssh, 0)) != 0 ||
    (res = sshpkt_put_cstring(ssh, password)) != 0 ||
    (res = sshpkt_add_padding(ssh, 64)) != 0 ||
    (res = sshpkt_send(ssh)) != 0)

但是出现了问题,如何执行所需的命令?我尝试了这样的事情,但它不起作用:

 if ((res = sshpkt_start(ssh, SSH2_MSG_SERVICE_REQUEST)) != 0 ||
    (res = sshpkt_put_cstring(ssh, "mkdir test123")) != 0 ||
    (res = sshpkt_send(ssh)) != 0)
{

您能告诉我如何正确运行命令吗?

I am a beginner C programmer, I got an old project use openssh library(https://github.com/openssh/openssh-portable) in which I need to connect via SSH and execute a set of commands on the device. I was able to auth:

if ((res = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
    (res = sshpkt_put_cstring(ssh, login)) != 0 ||
    (res = sshpkt_put_cstring(ssh, service)) != 0 ||
    (res = sshpkt_put_cstring(ssh, ssh_method_name)) != 0 ||
    (res = sshpkt_put_u8(ssh, 0)) != 0 ||
    (res = sshpkt_put_cstring(ssh, password)) != 0 ||
    (res = sshpkt_add_padding(ssh, 64)) != 0 ||
    (res = sshpkt_send(ssh)) != 0)

But the question arose, how do I execute the commands I need? I tried something like this, but it doesn't work:

 if ((res = sshpkt_start(ssh, SSH2_MSG_SERVICE_REQUEST)) != 0 ||
    (res = sshpkt_put_cstring(ssh, "mkdir test123")) != 0 ||
    (res = sshpkt_send(ssh)) != 0)
{

Can you please tell me how to run the command correctly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文