How to send operating system commands (Linux on RPi)?

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

How to send operating system commands (Linux on RPi)?

Bo Berglund
I need to include the ability to switch on and off the WiFi network
connection of a Raspberry Pi3 running Raspbian Jessie into my
FreePascal program.

The commands I am looking at now are:
sudo ifup wlan0
sudo ifdown wlan0

How could this be accomplished in a FreePascal program running as user
Pi?

The Pi will be multihomed (there is an Ethernet network connection in
addition to the WiFi) and it has been preconfigured to connect to a
specific SSID in the conf file for WiFi.

I want my program to be able to switch on the device hosting the WiFi
Access Point using relays (already done), then command the RPi to
connect to the now active Access Point immediately.
Time is of importance here and with just switching on the WiFi device
I get connection delays varying from 20 seconds to several minutes.

So it seems like a specific command to tell the operating system to
connect to WiFi is needed....


--
Bo Berglund
Developer in Sweden

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: How to send operating system commands (Linux on RPi)?

Bo Berglund
On Sat, 02 Jul 2016 01:12:34 +0200, Bo Berglund
<[hidden email]> wrote:

>The commands I am looking at now are:
>sudo ifup wlan0
>sudo ifdown wlan0
>
>How could this be accomplished in a FreePascal program running as user
>Pi?

To be specific I would like to know how I can send any command to the
Linux operating system from my FreePascal program?

Just doing this seems not to cut it:

writeln('sudo ifup wlan0');

This just *displays* the text on the program console, but I want it to
go to the operating system as a command...

On Windows one has to start cmd with the wanted command as a
parameter, what is the corresponding method on Linux from within
FreePascal?


--
Bo Berglund
Developer in Sweden

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: How to send operating system commands (Linux on RPi)?

Sven Barth-2

Am 02.07.2016 07:55 schrieb "Bo Berglund" <[hidden email]>:
>
> On Sat, 02 Jul 2016 01:12:34 +0200, Bo Berglund
> <[hidden email]> wrote:
>
> >The commands I am looking at now are:
> >sudo ifup wlan0
> >sudo ifdown wlan0
> >
> >How could this be accomplished in a FreePascal program running as user
> >Pi?
>
> To be specific I would like to know how I can send any command to the
> Linux operating system from my FreePascal program?
>
> Just doing this seems not to cut it:
>
> writeln('sudo ifup wlan0');
>
> This just *displays* the text on the program console, but I want it to
> go to the operating system as a command...
>
> On Windows one has to start cmd with the wanted command as a
> parameter, what is the corresponding method on Linux from within
> FreePascal?

Either fpsystem() from unit BaseUnix (this is the FPC variant of the C system() call) or TProcess if you should need to provide password input on a GUI.

Regards,
Sven


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: How to send operating system commands (Linux on RPi)?

Sandro Cumerlato
In reply to this post by Bo Berglund

I find convenient to use TProcess, take a look at the wiki:

http://wiki.freepascal.org/Executing_External_Programs#TProcess


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: How to send operating system commands (Linux on RPi)?

Bo Berglund
In reply to this post by Sven Barth-2
On Sat, 2 Jul 2016 09:21:17 +0200, Sven Barth
<[hidden email]> wrote:

>Am 02.07.2016 07:55 schrieb "Bo Berglund" <[hidden email]>:
>> On Windows one has to start cmd with the wanted command as a
>> parameter, what is the corresponding method on Linux from within
>> FreePascal?
>
>Either fpsystem() from unit BaseUnix (this is the FPC variant of the C
>system() call) or TProcess if you should need to provide password input on
>a GUI.

Thanks,
this seems to work:

if arg = '1' then
   cmd := 'ifup wlan0'
else if arg = '0' then
   cmd := 'ifdown wlan0';

S := fpsystem('/usr/bin/sudo ' + cmd);

Now I just have to deal with the error when executing an ifdown
command when the wlan0 is already down. Strange that a command should
result in an error message if the result of the command is already
present at the start...

Reminds me of some similar errors on a CNC machine controller I
automated 15 years ago:
delete filename
-> ERROR: filename does not exist!

Such messages should not exist either, if one wants to delete a file
and it is not existing then the mission is accomplished from the start
and there should not be an error message...

In this case the Raspbian system generates:

sudo ifdown wlan0
ifdown: interface wlan0 not configured

and:

sudo ifup wlan0
ifup: interface wlan0 already configured

Well, luckily I saw it so I can handle these "errors"


--
Bo Berglund
Developer in Sweden

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal