简体   繁体   English

从ac程序在远程主机上执行命令

[英]Execute commands on remote host from a c program

I am trying to automate a process of logging on the firewall server, exporting some logs , scp the logs to local host then delete it from the remote server (firewall server) 我试图自动化登录防火墙服务器,导出一些日志,将日志scp到本地主机然后从远程服务器(防火墙服务器)删除它的过程

I also enabled host based authentication so that both machines would communicate without entering the password of either machine during ssh-ing and transfering. 我还启用了基于主机的身份验证,以便两台计算机在sshing和传输过程中无需输入任何一台计算机的密码即可进行通信。

I started by using system calls thinking it would work but it does not seem that it works as anticipated. 我开始使用系统调用认为它可以工作,但似乎它没有按预期工作。 What I did was something like this 我做的是这样的

system(ssh user@firewallserver)
system(cd /directory)
system(fwm logexport)
system(scp log user@localhost:/home)
system(rm log)

people who tried this in the past will know that this does not work, but I do not know a solution or maybe a different way of accomplishing the task. 过去尝试过此操作的人会知道这不起作用,但我不知道解决方案,也可能是完成任务的不同方法。

Thanks! 谢谢!

Each system command opens new process so that your ssh session is gone in second call. 每个system命令都会打开新进程,以便您的ssh会话在第二次调用中消失。 Try to do it in one command if you really want to use system : 如果你真的想使用system尝试在一个命令中执行:

ssh server <some commands on server side>

scp <path on server> <local path>

Edit: Try 编辑:试试

ssh server 'cd /directory && touch test123'

on the command line. 在命令行上。 This should create the file test123 on the remote server in directory /directory . 这应该在目录/directory的远程服务器上创建文件test123 Then put this command in your system(...) call. 然后将此命令放入system(...)调用中。 In this way you can execute commands on the remote host. 通过这种方式,您可以在远程主机上执行命令。 For every new system call you must do again ssh server <commands> ... 对于每个新的system调用,您必须再次执行ssh server <commands> ...

If you don't need it in a C program you can put these commands directly in a bash script: 如果在C程序中不需要它,可以将这些命令直接放在bash脚本中:

#!/bin/bash
ssh server 'cd /directory && touch test123'
scp server:/directory/test123 /home/me/log123

Bash is definitely the way to go! Bash肯定是要走的路! You can just stick these commands straight into a script and it should work if you've set up your authentication properly. 您可以直接将这些命令粘贴到脚本中,如果您正确设置了身份验证,它应该可以正常工作。

The script would look something like this: 脚本看起来像这样:

#!/bin/bash
ssh user@firewallserver
cd /directory
fwm logexport
scp log user@localhost:/home #Possible mistake
rm log

Now I've looked through your code there could be a misunderstanding. 现在我查看了你的代码,可能会有一个误解。 I take it that you're supposed to move log back to the original machine that you ssh -ed into firewallserver from. 我认为你应该将log移回你从ssh -ed进入firewallserver的原始机器。

If that's the case you can't use localhost in your scp call, because that's the firewallserver . 如果是这种情况,你不能在你的scp调用中使用localhost ,因为那是firewallserver You need to scp log user@<your local IP> to get the log back to the machine you logged in from. 您需要scp log user@<your local IP>以将日志返回到您登录的计算机。

Perhaps I've misunderstood, if that's the case then the script I posted above should work. 也许我误会了,如果是这样的话,那么我上面发布的剧本应该有效。 However, if I'm correct then just swap the scp line and that should work. 但是,如果我是正确的,那么只需交换scp线即可。

This is how this could be done effectively, in a bash script do the following 这是如何有效地完成的,在bash脚本中执行以下操作

#!/bin/bash
ssh user@host 'bash -l' < ~/dowork.sh

the "dowork.sh" script is what you want to execute on the remote machine, you have to break those up to two scripts and redirect the dowork.sh to the ssh when executing the command “dowork.sh”脚本是您要在远程计算机上执行的脚本,您必须将这些脚本分解为两个脚本并在执行命令时将dowork.sh重定向到ssh

to verify that you got a shell into the correct machine do this in "dowork.sh" 验证你有一个shell进入正确的机器在“dowork.sh”中执行此操作

#!/bin/bash
echo $PATH

and this will give you the path variable from the remote machine. 这将为您提供远程机器的路径变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM