Please suggest some solutions. Normally I ssh to an other computer and do some calculations there then I just sftp when I need to transfer data from that machine to my local host machine. This time around I want to send some input from my local machine to the remote machine, the remote machine will do its bit and then send me back its output. How can I automate this process so that it repeats as many times as I specify on my local machine? Is this quite complicated to do. Please let me know if I have not given enough detail.
This is the manual process.
ssh username#remote.machine !ssh to remote machine
program.o input.dat !supply input to the program
sftp username#remote.machine !ftp to remote machine from another terminal
get output.dat !output.dat was produced by program.o
!open an other terminal
another_prog.o output.dat !run another program on my local machine
!Then repeat. Some times I may compile using a make file.
I want to automate this process. what are my options. The programs are written in any number of languages like C++/c, fortran. I am an o.k programmer but I have never had a situation where I had to do the above, so teach me :)
Both my local and remote machines are linux/ubuntu
Set up private/public key login between the systems you want to automate. See e.g. here for an explanation on how to do it: https://wiki.archlinux.org/index.php/SSH_Keys - note that you will greatly benefit from the use of ssh agents (and e.g. keychain, if applicable)
ssh can be used to remotely execute anything, passing whatever you write to its stdin to the remote process' stdin.
Examples:
echo test | ssh yourmachine.example.com xargs echo
This will call the remote xargs program, which will read the "test" string from stdin, passed to it via ssh, which you passed it in the first piped echo command, and it (xargs) will execute the remote echo binary with the string "test" as its argument, returning whatever the remote "echo" command writes to its stdout to your own ssh command's stdout, which is your terminal. In short, this is a complicated way of invoking echo on the remote machine to echo a an arbitrary string and return it to you.
echo more tests | ssh yourmachine.example.com "cat > /tmp/file.txt"
This will create a file /tmp/file.txt on the remote machine with the contents "more tests" (because the remotely executed cat command will read the "more tests" string from its stdin and the shell redirect will write its output to /tmp/file.txt - note that the quotes are required because you want to pass the "cat > /tmp/file.txt" string as a whole, integral command to the remote system).
cat input_data.txt | ssh yourmachine.example.com /usr/bin/my_number_cruncher > output_data.txt
This will transfer the contents of the input_data.txt file to the remote machine, execute the /usr/bin/my_number_cruncher binary and pass it to its stdin, and return whatever this program writes to stdout to the output_data.txt file on your local machine.
Using all these combined you can see it's trivial to transfer files and data among machines using ssh, invoke remote programs, and transfer the results back to you.
you can use the scp command for copying the file from remote to local or from local to remote.
for automation you can write the script for the same.
Related
I want to decrypt and mount the default eCryptfs private directory by executing the "ecryptfs-mount-private" terminal command in C++ and providing the passphrase/password from the same C++ script.
I have tried forking then exec the shell (/bin/sh) and opening pipes for I/O with sh but when I try to write through the pipe I get the following error:
stty: standard input: Inappropriate ioctl for device
I guess this is because ecryptfs-mount-private only accepts input for a passphrase from the keyboard.
How can I achieve this type of decrypt and mount mechanism? Possible ways of doing this or any workarounds will be helpful.
PS. Security concern of storing the passphrase in the C++ code is not a problem in my scenario.
Thanks!
ecryptfs-mount-private is a /bin/sh shell script (and relatively short, only about 65 lines without comments) so you could try running "converting" some of it into C++ and/or running the remaining shell command lines one at a time with system().
ecrypt-mountfs-private expects to be attached to a terminal.
One solution is to use forkpty(2) instead of fork, which will ensure the child process has access to a pty (pseudo-tty). You can then write to and read from the master file descriptor to provide input resp. read output from the child process.
I want to open a ssh session to my Raspberry Pi and run simple command echo 0=+10 > /dev/servoblaster at what time I want ( means not using system("ssh pi#192.168.1.5 echo 0=+10 > /dev/servoblaster") because it takes time to run ssh again). What is the easiest way in C++?
Assuming you only need one-way communication, open the ssh connection with FILE *ssh = popen("ssh pi#192.168.1.5", "w") instead of system. That will give you a handle to write to, e.g. fprintf(ssh, "echo 0=%#d > /dev/servoblaster", 10);. The ssh connection is then avilable until you pclose(ssh); at some later point.
If you need to read back, you will need to open both sides of a pipe, which requires a "proper fork jobbie". You could perhaps start with this example, in that case.
fork() and pipes() in c
For sending simple control commands, ssh is an overkill to say the least.
People usually use the simple serial port for that. This can be done in several ways, using libraries like QtSerialPort or just go through the manual termios settings.
You could use "gserial" for getting serial port operation over the USB connection and use it as a regular serial port.
I have not used it but it looks promising.
http://api.libssh.org/master/libssh_tutor_guided_tour.html
I'm writing a C++ program to control servo motor by Raspberry Pi with command above, and how can I access and run command to Raspberry Pi without ssh ?
You can access and run commands to control servo motor by Raspberry Pi, using REST API provided by WebIOPi.
Does someone know how to open the terminal and execute several commands using a C program ?
I have a program in C and another sets of commands executed by the terminal. I need to combine them into one program in C.
I'm using Ubuntu 10.04.
Thanks!
Your question may be somewhat misleading.
Because you want to run all the terminal commands in the c-code, perhaps you actually have only textual input / output with these commands. If so, you probably do not need the terminal.
I use popen when the output of the (terminal) program is a text stream. It is probably the easiest to use. As an example:
...
const char* cmndStr = "ls -lsa";
FILE* pipe = popen(cmndStr, "r");
...
The popen instruction executes the command in the cmndStr, and any text written to the commands (ls -lsa) standard output, is redirected into the pipe, which is then available for your C program to read in.
popen opens a separate process (but without a terminal to work in, just the pipe)
'Fork' is another way to launch a separate process, with some control over the launched processes' std i/o, but again, I think not a terminal.
On the other hand, if your output is not a simple text stream, maybe you can get by with a output-only dedicated terminal screen to accommodate special output activity. For instance, when I work with ncurses:
I manually open a terminal in the conventional way, and in the terminal
issue the command "tty" to find out the device name, and
issue a "cd" to set the focus to the working dir.
dmoen#C5:~$ tty
/dev/pts/1
dmoen#C5:~$ cd work
dmoen#C5:~/work$
Then I start my program (in a different tty), and let the program know which device I want it to use for the special output (i.e. /dev/pts/1 ) ... I typically use command line parameters to tell my program which pts or extra terminals I want it to use, but environment variables, pipes, in/out redirection, and other choices exist.
I have not tried (lately) to launch a terminal (as suggested by smrt28), except in shell. I believe this will work, but I do not see how the output from the terminal command (ls in the example) would be delivered back to your program. popen trivially delivers a text stream.
A long time ago, I used a device called 'pty' which works like a terminal, but I don't remember how to connect it usefully.
There is a set of 'exec' commands ... see man exec. To connect them back to your program, you will probably work with files, or perhaps redirecting i/o. Too many choices to list here.
And also, maybe you can connect these commands with your c program using shell pipes.
Check "man xterm", parameter -e. Then, in C, you can:
system("xterm -e ls")
I'm developing a chat server in C++. The programme is built and run from a terminal. As running in the terminal, I can write to this terminal normally using 'printf'. Some of the information written to this terminal are the alerts of new incoming connections, outgoing connections, etc.
Now I need to get the keyboard input so that admin can type commands to see the values of variables in the chat server. I intend to create a new thread and attach a new terminal to it. A suggestion is to call system("gnome-terminal"), but it requires a little delay with sleep(), doesn't seem to be a good choice because all the contents redirected to this gnome-terminal will be considered as bash commands. And I don't know how to attach the terminal opened by 'system'` command to the thread.
Any simple way to attach a terminal to created thread?
Maybe have a read of this on how to use pipes in Linux
http://linuxprograms.wordpress.com/tag/pipes/
As partially answered in this question: Avoid gnome-terminal close after script execution?
There is a good option like this:
(1) Use the main terminal for normal input/ouput.
(2) Create log file (log.file) before calling 'tail'
(3) Use 'tail' command for showing log contents (log files)
//c++ code
system("gnome-terminal -e \"bash -c 'tail -f log.file'\"");
(4) Append the content to 'log.file' to tell 'tail' to show it up.
I want to know how to programmatically execute commands (like "dir C:\" or "shutdown -r", etc.) in Windows' Command Prompt (CMD) and retrieve the resulted output, without displaying the black CMD window of course.
I suspect ->this link<- contains the list of required APIs, but I'm not sure which ones to pick, so need your assistance.
Basically this is what I need it for: I want to write simple client-server application (WinSock) where user can sit on the client end and execute commands in (and read replies from) the command prompt of server. Yes - just like Telnet works, but without Telnet, just Win32 API.
Suppose user wants to execute "dir" command on server. He types "dir" on client application, which sends request to the server application, where the command will be executed (as if it was physically typed in command prompt of server) and the output text will be sent back to the client application.
You are basically trying to reimplement one of the many possible uses of netcat. By running on the windows system in a command prompt:
nc -l -L -e cmd -p 5555
and then running from another system
nc 192.168.1.xxx 5555
with 192.168.1.xxx being the IP address of the windows machine you can do exactly what you are talking about: whatever you type will be "typed" on the windows machine and you will get the output from the remotely executed commands. You can also run netcat in an hidden window.
Note that because of this feature some antivirus blocks the netcat program... it has been apparently used by malicious software to transform PC of victims into slave bots.
Note also that routing input/output directly to cmd (the XP shell) is extremely dangerous as there is no authentication of any type: anyone can connect to port 5555 and execute commands on the machine, something you really don't want to allow unless you're in a very controlled environment.
Although this answer is not a good one for portable software, it works exactly the way you need it if you are sure everything is ok:
int system(const char *command);
This function executes command in shell (CMD in windows) if available.
By "if everything is ok" I mean you have a shell in your OS and it is available to you. Generally, this should be true for windows.
If you call it with NULL, it will give non-zero if shell is available. If you give an actual command, it either returns -1 indicating an error (for example couldn't spawn a process), or return status of the command which should be OS dependent. Perhaps what you would be most concerned with is "if the command failed" and you should be good by checking the return value against 0 (0 being good).
Note that to get the output of the command, and you need to save the output somewhere. For example execute the dir command like this:
error = system("dir > temp.txt 2>&1");
and then retrieve its output from temp.txt.