How can I redirect program output using libssh2? - linux

I'm using libssh2 for a C++ program in a Linux environment and so far I'm able to start a program on a remote machine using libssh2_channel_exec. However I'd like to redirect the program's output to the local machine (i.e. the output shall travel over ssh).
I'd like to achieve the same goal of the following bash line:
$ ssh user#remote ls > local_file.txt
I cannot specify the > local_file.txt part the command parameter because the file must be written in the local machine and not in the remote one.
So, how can I redirect a remote program's output to the local machine?

you should use the libssh2_channel_read function to read the remote stdout:
http://www.libssh2.org/libssh2_channel_read.html

Related

Running commands on remote server transparently

I am developing a number of applications that need a bit more power than my local machine has. So I'd like to run them on a remote machine. This is all fairly straightforward and runs something like: 1) rsync the files in the current directory to the remote machine in some location, 2) ssh to remote machine run the command. In some cases, if the remote command generates a file, I'd need to pull that back locally also.
It feels to me like such a common set of tasks that should be a nice command that puts it all together. Say something like
## Run make on the the files in the current directory on big-server-box
rrun big-server-box make
## Do the same, but pull output.txt back afterward
rrun big-server-box -f output.txt make
## Open a shell, having synced files first
rrun big-server-box --shell
Is there any tool that achieves this?
There is already a mechanism for accomplishing what you’re looking for: ssh.
If you want your specific syntax, you could very easily write a wrapper script. The only things different from your syntax above is the -f flag to copy files back to your local machine (which would be easy to implement with scp) and the --shell flag, which can be omitted since that behavior is implied with ssh. Other than that, the syntax is identical: call ssh instead of rrun and you have what you’re looking for.

How to run shell script in another server in a script?

No where online can i find a way to run a shell script on a remote server from another script. This is for automation, so the script on the host machine will automatically trigger another script on a different server. The server that my script will ssh to will either have a password prompt or have RSA key pair set up
Thanks!
Just pass the command as an argument to ssh.
ssh someserver /path/to/some/script.bsh
Let's say you want to execute a script on node2 but you have your script on node1 file name of script is sp over location /home/user/sp. Simply
ssh node2 < /path-of-the-script-including-the-filename
Another way, using expect package.
Disclaimer: This you can use for testing environments since it has an
open password. but depends on your usecase
If your server does not have expect, you may add the package then. run the command. You can also put this command inside an .sh script.
expect -c 'spawn ssh user#10.11.12.13 "/path/to/my.sh"; expect "assword:"; send "Y0urp#ssw0rd\r"; interact'

linux or windows shell script to upload a file and copy it to another server

I am struggling with a problem. I have:
ServerA (is closer to me and much faster)
ServerB (is my website where I want the final file to stay)
so what I want to do with the shell script (either Windows batch or linux, I have cygwin installed) is, passing the filename as parameter:
1) upload with FTP a file to ServerA
2) login with ssh on serverB and wget the file from serverA
I managed to do 1 with a shell script, but I don't understand how to do step2 in the shell?
Thanks
I would recommend using scp to accomplish step 2. You can use the syntax:
scp path/to/file serverb#hostname:/path/to/destination.
You can read more about the syntax for scp here: http://www.hypexr.org/linux_scp_help.php
You could use TeraTerm which has a powerful scripting language to automate both tasks.

Running Multiple Remote Commands Consecutively Through Matlab system()

I'd like to execute multiple commands consecutively using a matlab system call. I would first like to ssh into a remote machine then run a program on that machine. After the program starts I would like to then enter another command into this program's console. Here is what I would like to do in code:
system('ssh othermachine')
system('command on other machine')
%%WAIT FOR PROGRAM TO START RUNNING ON OTHER MACHINE
system('run command on other machine')
The problem is that Matlab will hang on the first system call and won't proceed to the next system call until the process form the first is exited. Is there a way around this?
Thanks for your help!
Prologue: Your problem is general and not just related to matlab.
When you want to run remote commands via ssh, they have to be issued in the ssh call. In a (linux) shell, you'd have
$ ssh remotemachine command1
for a single command. Hence, using a matlab system call you would have
>> system('ssh remotemachine command1').
When you want multiple commands to be executed sequentially, in a shell you'd write
$ ssh remotemachine "command1; command2"
i.e., in matlab, you'd write something like
>> system('ssh remotemachine "command1; command2"').
In general, is way more elegant to group your commands in a shell script, say script.sh, and pipe it in the ssh call
$ cat script.sh | ssh remotemachine
which, in matlab shell, sounds like
>> system('cat script.sh | ssh remotemachine').
There are a number of flags you can add in order to specify which behavior you want (e.g. in terms of session detachment/background execution, output collection,... look e.g. here).

Saving grep results to a file on a remote box

I'm somewhat new in Linux. I have to read logs on a remote host and save certain lines, found with grep command to a file. The problem is that I don't have permissions to create a file on the host. Is there a workaround the issue? Thanks!
You can run something like the following:
ssh remotehost "grep certainline logs*" > file
to save the file locally.
Otherwise, you might be able to create a file in /tmp.
You don't mention but I'm going to assume you're using ssh to access the remote machine. So you can run the command on the remote machine and redirect the output on the local machine like so:
ssh remotehost 'grep pattern /var/log/mylog' > mylocalfile
Note that the redirection occurs outside the quoted command that is given to ssh to send to the remote host. If you were to put it inside the quotes then the redirection would occur on the remote side.

Resources