Repeat system() command in C++? [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
What is the easiest way to repeat a simple system command on multiple CMD's in C++? For instance, how could I repeat this code on multiple terminal windows from my C++ code?
system( ("ping "+ ip +" -t -l 32").c_str() );

From a networking perspective, I don't think that pinging a single system from multiple terminals will be as effective as pinging that system from multiple remote systems.
Anyways, in regards to the easiest way to ping a system from multiple processes... just use the shell directly. Something like:
target=s4
for remotehost in s1 s2 s3; do (ssh -n $remotehost ping $target -t -l 32 & ) ; done
The "remotehost" doesn't have to really be a remote host either. You can just use "localhost" multiple times (instead of multiple names of remote hosts).
Alternatively if you really want to use C++ from a single host:
#include <cstdlib>
#include <string>
int main()
{
const std::string ip = "foo";
for (int i = 0; i < 3; ++i)
{
std::system(("ping " + ip + " -t -l 32 & ").c_str());
}
}
Note the usage of the ampersand (&) character in the input string to the system function. That instructs the shell to run the given task in the background. That way system returns immediately and the ping command basically runs at the same time as the two other instances of the command.
Hope this helps answer your question.

Related

check mariadb connection with c++ (without dependencies) [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 months ago.
Improve this question
I got myself a programm where a user types in their mariadb credentials (username/password/port).
I now want the programm to check if this connection is working or if something is wrong.
Right now I am running processes with CreateProcess but since statements like mysql -u root -pwrongpassword will still run through without any errors, this doesn't work.
I want such a statement, or just a generic connection check, to return false when those credentials turn out as wrong.
Important here is that it has to work without any existing software on the target system (except mariadb if necessary for your solution).
What would be a solid practice for that task?
Reinventing the wheel (as suggested by Sam Varshavchik) is not a good idea: It's not just opening a socket, writing and reading data. Depending on the authentication options you have to support SSL/TLS and the various authentication methods (native password, ed25519, sha256_caching_password, sha256_password, gssapi/kerberos) which is quite complex.
Since you mentioned that MariaDB is installed on your target system, you can use the mariadb client library (MariaDB Connector/C), which is also used by mysql command line client. It is installed together with the server package.
#include <mysql.h>
int check_connection(const char *user, const char *password, int port)
{
int rc= 0;
MYSQL *mysql= mysql_init(NULL);
if (mysql_real_connect(mysql, "localhost", user, password, NULL, port, NULL, 0))
rc= 1;
mysql_close(mysql);
return rc;
}
Now you have to link your application against libmariadb.lib (dynamic linking) or mariadbclient.lib (static linking).

Understand shell script interpreter with custom shell [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 months ago.
Improve this question
I try to understood how shell script interpreter working.
for example i wrote custom shell with c++ :
#include <iostream>
#include <string>
using namespace std ;
int main()
{
string input;
while (1)
{
cout << "prompt:> ";
cin >> input;
if(input=="exit")
return 0;
else if(input=="test")
cout << "You executed test command\n";
else
cout << "Unknown command.\n";
}
}
now i wrote a script like this :
#!/PATH/TO/COMPILED/SHELL
test
wrong_command1
wrong_command2
exit
Actually this script not working and i want to understand what part of my thinking is wrong .
Note: I executed this script on /bin/bash shell .
can i say ,my c++ code is: interactive shell
How interpreters work on shell scripts ? #!/PATH/TO/COMPILED/SHELL
How can fix code or script to activate interpreting feature ?
No idea what that means
If you compile your program to /tmp/a.out and have an executable file script with:
#!/tmp/a.out
test
wrong_command1
wrong_command2
exit
which you invoke on command line as ./script then the shell running the command line will invoke /tmp/a.out ./script. I.e. looks at the shebang, invokes that command and passes the script as its first argument. The rest is up to that command.
There is no interpreting feature in C++, you have to write it yourself, what you have is a good start except you need to read from the passed file argument, not stdin. Also std::getline might come handy.

Why file is not created in /etc/ directory [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Please find the code sample
void createFile(const std::string& FileName, const std::string& Content)
{
ofstream of(FileName.c_str());
of<<Content;
of.close();
}
const std::string testFile = "/etc/testFile";
const std::string EmptyContent = "";
createFile(testFile, EmptyContent);
File is not creating at /etc/ directory. I think this is related to permissions. What extra I have to add in the code to work.
There's nothing extra that you can add to this program to "make it work". If an arbitrary program can write to /etc, this would toss the traditional POSIX security model out the window.
In order to be able to write to /etc, your program must be executed as root.
It seems to be a permission issue. Try to run your program using sudo:
sudo yourprogram

Can I only write last 50 lines console output to a file automatically [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Is it possible to only write the last 50 lines of console output to a file automatically?
So that the file is always overwritten by the latest 50 lines.
You can use shell to modify and redirect the output of your program :
my_program | tail -n 50 > my_file
use simple redirection > if you want to truncate the file or double redirection >> if you want to append it.
Note :
This method only redirect stdout if you need to redirect stderr put 2>&1 after my_program
If you really want to do this in C++, you could store your console output line by line in a container while your program is running and write the last 50 lines to your file when needed.
Write a wrapper for your output function and each time you print a line to the console, add it to a std::queue. If that makes your queue larger than 50 elements, pop the oldest one (just call pop()).
class Logger {
static std::queue<string> lastFifty;
public static void log(const std::string& str) {
lastFifty.push(str);
if (lastFifty.size() > 50) {
lastFifty.pop();
}
std::cout << str;
}
public static void dumpToFile(std::ofstream& file) {
while (!lastFifty.empty()) {
file << lastFifty.pop();
}
}
}

How to use graphviz neato from within C++ program [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Contination of this question
#include <boost/graph/graphviz.hpp>
#include <boost/graph/grid_graph.hpp>
typedef boost::grid_graph<2> Grid;
int main()
{
boost::array<std::size_t, 2> lengths = { { 3, 5 } };
Grid grid(lengths);
std::ofstream gout;
gout.open("test.dot");
boost::write_graphviz(gout, grid);
}
I run
system('neato -Tpng overlap=false test.dot > test.png');
from a c++ program. It is not working.i.e png file is not created
When I run the same command from a console prompt, it does work as expected.
If redirection doesn't work on your system's shell, use the option:
system("neato -Tpng overlap=false test.dot -o test.png");
Also be aware of your working directory. Make sure your input is in the current working directory, and also check that you are looking for the output (test.png) in that same directory.
Alternatively, spell out the paths
system("neato -Tpng overlap=false /path/to/dir/test.dot -o /path/to/dir/test.png");
CAVEAT: of course, in C++ strings backslashes need to be escaped, if your paths contains them