Confusing output with SIPp remote commands through C++'s system() on Ubuntu - c++

I'm having a bit of trouble with understanding how the system() command is functioning on Ubuntu.
I am creating a C++ program to remotely control SIPp clients (SIPp remotely controlling clients) and have pretty much completed the program, but I'm now running into an issue when I'm testing it.
I had already tested this on my centOS VM, and it was running perfectly, but now that I'm testing it on my Ubuntu VM, it's failing to even execute the commands.
On Ubuntu's terminal, I can run echo + >/dev/udp/127.0.0.1/8888 and the SIPp client will accept the command, However, when I run my program (Which does the same thing) I get a sh: 1: cannot create /dev/udp/127.0.0.1/8888: Directory nonexistent.
#include <stdlib.h>
int main(){
system("echo + >/dev/udp/127.0.0.1/8888");
return 0;//
}
The above code works perfectly fine on centOS and the SIPp client recieves and performs the task, but on Ubuntu it's giving me the Directory nonexistent error.
From what I can see, it's currently trying to read the > as me attempting me to output a file, but escaping the > with \> just produces a compiler error.
warning: unknown escape sequence: '\>' [enabled by default]
system("echo + \>");
^
If anyone can point me in the right direction, I would be one happy camper.

So after searching around forever I realized it was because access to /dev/udp was a bash only function, and system() was interpreted by bin/sh.
Changing my system() call to system("bash -c \"echo + >/dev/udp/localhost/8888\""); solved the problem.

Had a similar Problem and found a different solution, i had to do it in a bashscript and ubuntu threw the same error. This worked for me:
echo "+" | nc -4u -w1 127.0.0.1 8888
Got it from here: http://mikeberggren.com/post/53883822425/ncudp

Related

Why isn't gdb working for me

Background
I am currently trying to build an autonomous drone using ROS on my Rapsberry Pi which is running an Ubuntu MATE 16.04 LTS. Solving the Computer Vision problem of recognising red circles as of now.
Specific Problem
I am constantly getting the error I get in this question. To help me solve this, I have decided to use gdb. However, the command rosrun --prefix 'gdb run --args' zlab_drone vdstab does not seem to be working for me. zlab_drone is the name of the package and vdstab is the name of the executable I am trying to run. Since this is inside a ROS environment, I have grabbed the syntax from here, and used the suggestions in this question.
When I invoke this command, even with tui, I get a SIGSEGV and when I invoke list inside gdb itself, the program does not stay at a particular point and keeps listing a different line till it is out of range. This is quite a weird issue.
I managed to make it work without this issue earlier by using a different command, I reckon. I just cannot remember how I made it work last time.
Well, in the link you mentioned, it states clear that you should use either :
launch-prefix="xterm -e gdb --args" : run your node in a gdb in a separate xterm window, manually type run to start it
or :
launch-prefix="gdb -ex run --args" : run your node in gdb in the same xterm as your launch without having to type run to start it
So, it really looks like you missed an -ex as #ks1322 suggeseted in the comments or just type run to start the debug process.
I found out about this exclusive bug that relates to Raspberry Pi's solely. Basically the solution involves, as quoted by Peter Bennet:
There is a workaround. Start the program, then from another command
prompt or from an ssh remote login, use gdp -p xxxxx where xxxxx is
the process number. This works without crashing. If you need to debug
something that happens before you can get in from another command
prompt, add to the program a command that stops process at the
beginning of main, for example a call to gets, which will wait for you
to press enter before continuing.

Run a C++ executable in Matlab from SSH server

I'm trying to run an executable, generated from C++ code, in Matlab.
To do this I use the command
system('unset LD_LIBRARY_PATH; /home/Documents/ServerFolder/ExecutableFiles/program_23 argument');
where program_23 is the executable generated by the C++ code.
When I do that in Ubuntu, it works perfectly fine.
The problem arise when I do that from a SSH server (i.d. calling Matlab from a server, but running the exact same script that works in Ubuntu).
In particular, if I add
[status,cmdout] = system('unset LD_LIBRARY_PATH; /home/Documents/ServerFolder/ExecutableFiles/program_23 argument');
I can see that status is different from 0, meaning a failure of the system command.
Does anyone have any idea on how to make this work?
Thank you

system("history") not working

I've run into a snag, I'm trying to implement a linux shell program of sorts with C++ and many of my commands seem to work, however, when I try to get the history(list all recently executed commands) I get an error of "sh: 1: history: not found" the below line is all that runs in the area, what is the issue?
system("history"); //produces the error above ^
If I do
$ history
from the command line it's fine...why is it not fine in C++?
system executes a program using /bin/sh, but history is a bash builtin.
You might look at the contents of ~/.bash_history instead. (Note (by leemes) .bash_history is only updated after closing a previous bash session, as well as it is not updated by executing a command with system.)
Because it's a bash shell builtin not necessarily accessible through /bin/sh -c` (which may be the bourne shell).

Basic ping pong style C++ program, can't seem to get it to compile and run

To start off I must say I am an absolute n00b. I just started with C++ and I am trying to figure it out. To issue I am having is I found this source code on google for Ping Pong and I've saved it as "pong.cpp" from sublime text edit to my Desktop. I am under the impression that all C++ programs have to be run through the terminal? but I am not sure, again beginner problems. So in terminal I direct it to my desktop in from there I type in g++ pong.cpp. Then it says this:
pong.cpp:2:10: fatal error: 'allegro.h' file not found
#include <allegro.h>
1 error generated.
I am thinking this is because I found it on google and that I need a file to back up the processing on the program which is unavailable.
Does anyone know where I could get a proper Ping Pong C++ code that would suite what I need here? I just want to be able to run a basic pingpong style game through my terminal to get more familiarized with C++
You have to tell gcc where allegro.h is installed. Run this command in terminal and post the output.
find / -name 'allegro.h' 2>/dev/null

Try to execute command line codes from c++ linux

I tried the following code, to communicate with the command line from c++ code.
#include<iostream>
#include<cv.h>
int main()
{
system("gnome-terminal");
system("cd");
}
The gnome-terminal command is executing fine. After I close the terminal, when am expecting the cd to execute, however, is not happening. Could you please help me and point out the reason? Thanks. I was expecting the function to make the cmd go down to the home directory
, but it did not. am working in linux
I tried it even by removing gnome. simple cd is not working. am I doing something rong>?
If I try ls, it seems to be working fine!
My main aim is to open a new terminal, and execute commands on that new terminal through the present program that opened the new terminal. Could you please tell me how I can achieve this??
If you want to run a program and wait for it to finish before executing next line, take a look at this page and example code here: http://www.thegeekstuff.com/2012/03/c-process-control-functions/
But if you want to run gnome-terminal and execute a command in newly created window, do this:
system("gnome-terminal -x sh -c 'cd /tmp ; ls -la'");
The system function creates a shell child process to execute the specified command.
cd is a shell command which changes the current working directory of that shell process only.
So the child's cd probably works fine, but it has no effect on your C++ program, which is a different process.
Instead, you probably want to look at the Linux system call chdir.
Thanks for your help!! This command worked perfectly fine from this link
https://superuser.com/questions/198015/open-gnome-terminal-programmatically-and-execute-commands-after-bashrc-was-execu
gnome-terminal -x sh -c 'command1; command2; exec bash'
and I entered the respective commands in the new window. But to change the working directory in the shell am working o, I haven't still figured that out.