I have written a C++ program where I would capture the packets on the interfaces. Based on the packet values, I need to install
iptables rules on the remote machine.
iptables on remote machine, I have done the following
- I have created SSH key pair so that I can login without password
Written a shell script to execute the
output=$(ssh $1 "sudo iptables -t nat -A OUTPUT -p tcp -d "$2" --dport "$3" -j DNAT --to-destination "$4":"$5"")
The parameters are passed to the shell script from the C++ program, and I am using
system()command to execute in C++.
As I want to automate the process, this is not useful as this prompts for remote root login password.
While searching in stack overflow, I found one solution which is
sudo host 'bash -s' < script.sh
This doesn't ask for remote host password but I do not understand how to automate the process i.e. to pass the
iptablesparameters to a script and then execute that script with the above command in C++.
I have come across
libssh library but that's not been updated for some years. So I am not sure whether I can use it or not.
EDIT 1: As the command
sudo host 'bash -s' < script.sh is not asking the password, I have passed the values to the above command like
sudo $1 'bash -s' < script.sh $2 $3 $4 $5
I have placed the above line in, say,
remote.sh. When I execute directly on terminal like
./remote.sh (all 5 values), its getting executed perfectly. I have verified in the remote machine.
But when I pass
./remote.sh as C++ command using
system(), then it is asking for root password again