How to set Proxy Address using QProcessEnvironment on Linux? - c++

I am stuck with a simple issue in Qt. I want set proxy address using Qt. The command to set proxy address
export http_proxy=http://wwgw.abcd.com:8080
works fine if passed by a terminal manually. but If the same command is run using QProcess, it fails without setting proxy. Even, I tried with QProcessEnvironment as
QProcess process_setupProxyServerUrl;
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
QString cmd = "http://wwgw.abcd.com:8080";
env.insert("HTTP_PROXY", cmd);
process_setupProxyServerUrl.setProcessEnvironment(env);
But this also fails in setting up proxy address. QProcessEnvironment is new for me. So may be i might be using it in wrong way.
In my application, I need to change the proxy address according to users choice (at run time).
Any way using Qt would be helpfull. Please provide some suggestions/ideas to resolve this issue.

Try something like that
QProcess process_setupProxyServerUrl;
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
env.insert("HTTP_PROXY", "http://wwgw.abcd.com:8080");
process_setupProxyServerUrl.setProcessEnvironment(env);
Why did you use export ? This is just an executable, not the environment key

Related

Using Python and the os library, how do I tell which environment i am in

I have written a script, just tested on my local, moved to dev, did not work because of url/environment variable. I need to add the correct location of the file that is being created.
how do i tell which env i am in using the os library in python?
```/mnt/www/html/**warkdev**/docroot - for dev```
/mnt/www/html/**warkstg**/docroot - for staging
```/mnt/www/html/**wark**/docroot - for prod```
I am testing out
os.getcwd
, but that is giving the entire url, and i don't want to start slicing up, How can i get the envir vars info via os so i can tell if I am in stg, dev, prod?
Thank you in advance.
For those with the same question in the future, use print(os.environ). look for the key/pair that you need then
someVar = os.environ['SITE_NAME']

Execute /bin/login from QProcess

I'm working on embedded system running linux (without X11 server). I want my Qt application to be started with root privileges then from within spawn new QProcess which should be "/bin/login user_name" then provide password.
The problem is the "Password:" that usually prints on console (I assume it's stdout) doesn't show up, and QProcess.write() doesn't seem to work at all. If I run this QProcess as normal user I get an error message at redirected stderr (via setStandardErrorFile). I think I have general problem with understanding /bin/login.
I tried to
echo "userPassword" > /proc/LOGIN_PID/fd/0
from different console, but it also doesn't work.
The function I wrote in c++ goes like this:
void BashWrapper::bashEcho(QStringList t_echo){
QString bash = "/bin/login";
QStringList args(t_echo[0]);
static QProcess *newProc = new QProcess();
newProc->setStandardOutputFile("/dev/pts/2");
newProc->setStandardErrorFile("/dev/pts/2");
newProc->start(bash,args);
if(newProc->waitForStarted()){
qDebug() << newProc->state();
}
newProc->write(t_echo[1].toUtf8());
if(!newProc->waitForFinished(10000)){
qDebug() << "timeout";
}
else{
qDebug() << "finished";
}
}
t_echo contains ("user_name", "userPassword")
What I want to do is spawn new program (let's say /bin/myApp) as user_name. I thought that login as that user and setting .bash_profile to run /bin/myApp would be easy, painless and... secure?
If using /bin/login from application is impossible is there any simple way to this?
Best Regards,
MichaƂ
Ok, I found the answer here:
https://www.thegeekstuff.com/2010/10/expect-examples/ There is an example script that executes su command, simply changing it to /bin/login allowed me to pass the password via some addtional scripts.

Start, Stop, Enable, Disable a Systemd Service from C++ [duplicate]

I have a .service for a process that i don't want to start at boot-time, but to call it somehow from another already running application, at a given time.
The other option would be to put a D-Bus (i'm using glib dbus in my apps ) service file in /usr/share/dbus-1/services and somehow call it from my application. Also, i don't manage to do this either.
Let's say that my dbus service file from /usr/share/dbus-1/services is com.callThis.service
and my main service file from /lib/systemd/system is com.startThis.service
If i run a simple introspect from command line:
/home/root # dbus-send --session --type=method_call --print-reply \
--dest=com.callThis /com/callThis org.freedesktop.DBus.Introspectable.Introspect
the D-Bus service file will get called and it will start what is in the Exec ( com.starThis ). The problem is that i want to achieve this from C/C++ code using D-Bus glib.
A combination of g_dbus_connection_send_message with g_dbus_message_new_method_call or g_dbus_message_new_signal should be what you are looking for.
I had trouble to do the same thing. The discover of : G_BUS_NAME_WATCHER_FLAGS_AUTO_START solve it.
g_bus_watch_name(G_BUS_TYPE_SYSTEM, "com.mydbus.listen",
G_BUS_NAME_WATCHER_FLAGS_AUTO_START, xOnNameAppeared, xOnNameVanished,
this, nullptr);

Changing the Network adapter name Programmatically in windows with Qt

I know that it is possible to change the network adapter in windows using the netsh command in console. but I need to do this in Qt using C++. I have tried to use QProcess to call netsh but command prompt needs admin right. also there is space in my adapter name that make it also not easy to run. Is there any library in Qt capable of changing Network adapter name without using QProcess and calling windows commands in it?
Update 1:
When I run the program with admin right and set the \" for having " inside of my string I can change the adapter name, using QProcess and netsh command.
QProcess p;
QString pi_network = "original adapter name";
p.start("netsh interface set interface name = \""+pi_network+"\" newname = \"MyAdapter\"");
I will post it as an answer, because it might help someone (as MrEricSir said in the comments).
you can use netsh command, but be aware that you should run your program with administrator right in windows.
QProcess p;
QString pi_network = "original adapter name";
p.start("netsh interface set interface name = \""+pi_network+"\" newname = \"new network adapter name\"");
p.waitForFinished();
p.close();

find list of all accessible hostnames

I have an application where a user can dynamically configure TCP connections between remote processes. I'd like to make sure the user input is valid, by providing them with a QComboBox that is pre-populated with all the valid hostnames on their network. Is there a way to find the list of hostnames using Qt?
If possible I'd like to do this on both windows and linux.
This can be achieved using Qt classes, but you'll also need to use system tools to gather the hostname information, and those tools are different between linux and windows. That said, with a simple preprocessor switch, we can use QProcess to call the right one, and pull the hostnames out of the result using a QRegExp:
// find valid hostnames
QStringList hostnames;
QRegExp hostnameRx("\\\\\\\\(.*)");
QProcess cmd(this);
#ifdef _WIN32
cmd.start("cmd.exe");
cmd.write("net view\r\n");
cmd.write("exit\r\n");
#else
cmd.start("smbtree", QStringList() << "--no-pass");
#endif // _WIN32
cmd.waitForFinished();
while (!cmd.atEnd())
{
QString line = cmd.readLine();
hostnameRx.indexIn(line);
if (!hostnameRx.cap(1).trimmed().isEmpty())
{
hostnames << hostnameRx.cap(1).trimmed();
}
}
The regex strips the begining '\\' returned by both net view and smbtree, because QTcpSocket connections take hostnames without it.
Obviously, the QStringListcan be used to populate a QComboBox:
QComboBox* box = new QComboBox(this);
box->insertItems(0, hostnames);
NOTE: net view and smbtree are only going to show computers with accessible shares. You can try nmap for a more complete list of live hosts, but you're going to need to run as root and you'll still probably hit a lot of firewall issues.