Discussion:
[OpenWrt-Users] How to execute python script in rpc shell script
Phani Siriki
2018-02-14 18:11:16 UTC
Permalink
Hi All

I want to execute my custom python scripts in rpc shell script. However,
they are not executed. Could someone help me on this?

*Example Python script:*
*===================*
***@OpenWrt:~# cat hello.py
from subprocess import call

call(["touch", "/root/ap1", "down"])

print "{'status':'True'}"

*Shell script:*
*==========*
***@OpenWrt:~# cat /usr/libexec/rpcd/foo
#!/bin/sh
. /usr/share/libubox/jshn.sh
case "$1" in
list)
echo '{ "bar": { "arg1": true, "arg2": 32, "arg3": "str" },
"toto": { } }'
;;
call)
case "$2" in
bar)
# read the arguments
read input;

# optionally log the call
logger -t "foo" "call" "$2" "$input"

*python /root/hello.py*

# return json object or an array
echo '{ "hello": "world" }'
;;
toto)
# return json object or an array
echo '[ "item1", "item2", "item3" ]'
;;
esac
;;
esac
***@OpenWrt:~#
***@OpenWrt:~# ubus call -S foo bar '{"arg1": true }'
{"hello":"world"}
***@OpenWrt:~#
***@OpenWrt:~# ls ==========> Files are not created.
Math foo hello.py
***@OpenWrt:~#

Best Regards
Phani
Saverio Proto
2018-02-14 20:43:46 UTC
Permalink
Add something like:

#!/usr/bin/env python

at the first line of your python script

bash script has #!/bin/sh, so it is correctly detected it is shell script
but for the python code you did not specify any shebang

Saverio
Post by Phani Siriki
Hi All
I want to execute my custom python scripts in rpc shell script. However,
they are not executed. Could someone help me on this?
===================
from subprocess import call
call(["touch", "/root/ap1", "down"])
print "{'status':'True'}"
==========
#!/bin/sh
. /usr/share/libubox/jshn.sh
case "$1" in
list)
echo '{ "bar": { "arg1": true, "arg2": 32, "arg3": "str" },
"toto": { } }'
;;
call)
case "$2" in
bar)
# read the arguments
read input;
# optionally log the call
logger -t "foo" "call" "$2" "$input"
python /root/hello.py
# return json object or an array
echo '{ "hello": "world" }'
;;
toto)
# return json object or an array
echo '[ "item1", "item2", "item3" ]'
;;
esac
;;
esac
{"hello":"world"}
Math foo hello.py
Best Regards
Phani
_______________________________________________
openwrt-users mailing list
https://lists.openwrt.org/cgi-bin/mailman/listinfo/openwrt-users
Lars Kruse
2018-02-14 20:59:27 UTC
Permalink
Hello Saverio,


Am Wed, 14 Feb 2018 21:43:46 +0100
Post by Saverio Proto
#!/usr/bin/env python
at the first line of your python script
in this specific case it is not relevant, since he is calling the interpreter
with the script filename as an argument ("python /root/hello.py"). But it would
be necessary if he just called the script without specifying the interpreter
(e.g. "/root/hello.py").

Anyway: a shebang is surely always a good thing for scripts.

Cheers,
Lars
Phani Siriki
2018-02-14 21:27:50 UTC
Permalink
Hi Saverio

I tried as suggested. It didn't work :(

Best Regards
Phani
Post by Saverio Proto
#!/usr/bin/env python
at the first line of your python script
bash script has #!/bin/sh, so it is correctly detected it is shell script
but for the python code you did not specify any shebang
Saverio
Post by Phani Siriki
Hi All
I want to execute my custom python scripts in rpc shell script. However,
they are not executed. Could someone help me on this?
===================
from subprocess import call
call(["touch", "/root/ap1", "down"])
print "{'status':'True'}"
==========
#!/bin/sh
. /usr/share/libubox/jshn.sh
case "$1" in
list)
echo '{ "bar": { "arg1": true, "arg2": 32, "arg3": "str"
},
Post by Phani Siriki
"toto": { } }'
;;
call)
case "$2" in
bar)
# read the arguments
read input;
# optionally log the call
logger -t "foo" "call" "$2" "$input"
python /root/hello.py
# return json object or an array
echo '{ "hello": "world" }'
;;
toto)
# return json object or an array
echo '[ "item1", "item2", "item3" ]'
;;
esac
;;
esac
{"hello":"world"}
Math foo hello.py
Best Regards
Phani
_______________________________________________
openwrt-users mailing list
https://lists.openwrt.org/cgi-bin/mailman/listinfo/openwrt-users
_______________________________________________
openwrt-users mailing list
https://lists.openwrt.org/cgi-bin/mailman/listinfo/openwrt-users
Greg Oliver
2018-02-15 06:27:04 UTC
Permalink
Post by Phani Siriki
Hi Saverio
I tried as suggested. It didn't work :(
Best Regards
Phani
Post by Saverio Proto
#!/usr/bin/env python
at the first line of your python script
bash script has #!/bin/sh, so it is correctly detected it is shell script
but for the python code you did not specify any shebang
Saverio
Post by Phani Siriki
Hi All
I want to execute my custom python scripts in rpc shell script. However,
they are not executed. Could someone help me on this?
===================
from subprocess import call
call(["touch", "/root/ap1", "down"])
print "{'status':'True'}"
==========
#!/bin/sh
. /usr/share/libubox/jshn.sh
case "$1" in
list)
"str" },
Post by Phani Siriki
"toto": { } }'
;;
call)
case "$2" in
bar)
# read the arguments
read input;
# optionally log the call
logger -t "foo" "call" "$2" "$input"
python /root/hello.py
# return json object or an array
echo '{ "hello": "world" }'
;;
toto)
# return json object or an array
echo '[ "item1", "item2", "item3" ]'
;;
esac
;;
esac
{"hello":"world"}
Math foo hello.py
Best Regards
Phani
It will probably work if you specify the full path to the python
interpreter (which python) in your script...
Phani Siriki
2018-02-15 15:21:09 UTC
Permalink
Hi Greg

I tried as suggested, but it didn't work :(


***@OpenWrt:~# which python
/usr/bin/python
***@OpenWrt:~#
***@OpenWrt:~# ls -l /usr/bin/python
lrwxrwxrwx 1 root root 23 Feb 14 2018 /usr/bin/python ->
/mnt/usb/usr/bin/python

***@OpenWrt:~# cat /usr/libexec/rpcd/foo
#!/bin/sh
#!/usr/bin/python
. /usr/share/libubox/jshn.sh
case "$1" in
list)
echo '{ "bar": { "arg1": true, "arg2": 32, "arg3": "str" },
"toto": { } }'
;;
call)
case "$2" in
bar)
# read the arguments
read input;

# optionally log the call
logger -t "foo" "call" "$2" "$input"

*/usr/bin/python /root/hello.py*

# return json object or an array
echo '{ "hello": "world" }'
;;
toto)
# return json object or an array
echo '[ "item1", "item2", "item3" ]'
;;
esac
;;
esac
Post by Greg Oliver
Post by Phani Siriki
Hi Saverio
I tried as suggested. It didn't work :(
Best Regards
Phani
Post by Saverio Proto
#!/usr/bin/env python
at the first line of your python script
bash script has #!/bin/sh, so it is correctly detected it is shell script
but for the python code you did not specify any shebang
Saverio
Post by Phani Siriki
Hi All
I want to execute my custom python scripts in rpc shell script.
However,
Post by Phani Siriki
they are not executed. Could someone help me on this?
===================
from subprocess import call
call(["touch", "/root/ap1", "down"])
print "{'status':'True'}"
==========
#!/bin/sh
. /usr/share/libubox/jshn.sh
case "$1" in
list)
"str" },
Post by Phani Siriki
"toto": { } }'
;;
call)
case "$2" in
bar)
# read the arguments
read input;
# optionally log the call
logger -t "foo" "call" "$2" "$input"
python /root/hello.py
# return json object or an array
echo '{ "hello": "world" }'
;;
toto)
# return json object or an array
echo '[ "item1", "item2", "item3" ]'
;;
esac
;;
esac
{"hello":"world"}
Math foo hello.py
Best Regards
Phani
It will probably work if you specify the full path to the python
interpreter (which python) in your script...
_______________________________________________
openwrt-users mailing list
https://lists.openwrt.org/cgi-bin/mailman/listinfo/openwrt-users
Lars Kruse
2018-02-15 21:11:42 UTC
Permalink
Hello Phani,


Am Thu, 15 Feb 2018 09:21:09 -0600
Post by Phani Siriki
I tried as suggested, but it didn't work :(
Just in case you are running out of ideas:
did you try the suggestions that I put down in my first response to this thread
(Wed, 14 Feb 2018 21:56:08 +0100)?
What did you discover along this path?

Cheers,
Lars
Phani Siriki
2018-02-17 18:22:02 UTC
Permalink
Hi Lars

Sorry for the late reply.

Thank you for the awesome suggestion.

I redirected stderr to file and observed below error.

shell script:
========
...
/usr/bin/python /root/hello.py > /root/file-name 2>&1
...

***@OpenWrt:~# cat file-name
/usr/bin/python: can't load library 'libpython2.7.so.1.0'

***@OpenWrt:~# which python
/usr/bin/python

***@OpenWrt:~# ls -l /usr/bin/python
lrwxrwxrwx 1 root root 23 Feb 14 2018 /usr/bin/python ->
/mnt/usb/usr/bin/python

***@OpenWrt:~# ldd python
libpython2.7.so.1.0 => /mnt/usb/usr/lib/libpython2.7.so.1.0
(0x77864000)
libpthread.so.0 => /mnt/usb/lib/libpthread.so.0 (0x7783e000)
libdl.so.0 => /lib/libdl.so.0 (0x7782a000)
libutil.so.0 => /lib/libutil.so.0 (0x77819000)
libz.so.1 => /mnt/usb/usr/lib/libz.so.1 (0x777fb000)
libm.so.0 => /lib/libm.so.0 (0x777d5000)
libgcc_s.so.1 => /lib/libgcc_s.so.1 (0x777b1000)
libc.so.0 => /lib/libc.so.0 (0x77744000)
ld-uClibc.so.0 => /lib/ld-uClibc.so.0 (0x779fa000)

Could you please help me how to resolve this?

Best Regards
Phani
Post by Lars Kruse
Hello Phani,
Am Thu, 15 Feb 2018 09:21:09 -0600
Post by Phani Siriki
I tried as suggested, but it didn't work :(
did you try the suggestions that I put down in my first response to this thread
(Wed, 14 Feb 2018 21:56:08 +0100)?
What did you discover along this path?
Cheers,
Lars
_______________________________________________
openwrt-users mailing list
https://lists.openwrt.org/cgi-bin/mailman/listinfo/openwrt-users
Lars Kruse
2018-02-17 18:55:27 UTC
Permalink
Hello Phani,


Am Sat, 17 Feb 2018 12:22:02 -0600
Post by Phani Siriki
/usr/bin/python: can't load library 'libpython2.7.so.1.0'
so this discussion revolved around the same topic as your other thread
("Pip command found error even though pip is installed").
Thus you will need to fix your python installation.

The usual steps:
* remove and install again
* find out, how your setup differs from the setup of others

Cheers,
Lars
y***@gmail.com
2018-02-17 19:16:30 UTC
Permalink
Sure Lars. I will check it.

Best regards
Phani

Sent from my iPhone
Post by Lars Kruse
Hello Phani,
Am Sat, 17 Feb 2018 12:22:02 -0600
Post by Phani Siriki
/usr/bin/python: can't load library 'libpython2.7.so.1.0'
so this discussion revolved around the same topic as your other thread
("Pip command found error even though pip is installed").
Thus you will need to fix your python installation.
* remove and install again
* find out, how your setup differs from the setup of others
Cheers,
Lars
_______________________________________________
openwrt-users mailing list
https://lists.openwrt.org/cgi-bin/mailman/listinfo/openwrt-users
Phani Siriki
2018-04-04 20:02:13 UTC
Permalink
Hi Lars

Thanks for your input. I am able to execute python script after
creating below soft links.

ln -s /mnt/usb/usr/lib/libz.so.1 /usr/lib/libz.so.1
ln -s /mnt/usb/lib/libpthread.so.0 /usr/lib/libpthread.so.0
ln -s /mnt/usb/usr/lib/libpython2.7.so.1.0 /usr/lib/libpython2.7.so.1.0

However, I am unable to import "ctypes" module. I am getting below error.

Traceback (most recent call last):
File "/root/change_wifi_params.py", line 43, in <module>
import ctypes
File "/mnt/usb/usr/lib/python2.7/ctypes/__init__.py", line 10, in <module>
from _ctypes import Union, Structure, Array
ImportError: File not found

sys.path:
=======

/mnt/usb/usr/lib/python2.7/site-packages/pip-1.5.6-py2.7.egg
/mnt/usb/usr/lib/python27.zip
/mnt/usb/usr/lib/python2.7
/mnt/usb/usr/lib/python2.7/plat-linux2
/mnt/usb/usr/lib/python2.7/lib-tk
/mnt/usb/usr/lib/python2.7/lib-old
/mnt/usb/usr/lib/python2.7/lib-dynload
/mnt/usb/usr/lib/python2.7/site-packages
/mnt/usb/usr/lib/python2.7/site-packages/setuptools-7.0-py2.7.egg

***@OpenWrt:~#
***@OpenWrt:~# ls /mnt/usb/usr/lib/python2.7/lib-dynload/_ctypes.so
/mnt/usb/usr/lib/python2.7/lib-dynload/_ctypes.so
***@OpenWrt:~#

I am able to import the module through normal execution. However it is
not working in rpcd shell script. Could you please give me some
inputs?

***@OpenWrt:~# python
Python 2.7.9 (default, Sep 9 2015, 07:59:02)
[GCC 4.8.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
Post by y***@gmail.com
Post by Lars Kruse
import ctypes
***@OpenWrt:~#

Best Regards
Phani
Post by y***@gmail.com
Sure Lars. I will check it.
Best regards
Phani
Sent from my iPhone
Post by Lars Kruse
Hello Phani,
Am Sat, 17 Feb 2018 12:22:02 -0600
/usr/bin/python: can't load library 'libpython2.7.so.1.0'
so this discussion revolved around the same topic as your other thread
("Pip command found error even though pip is installed").
Thus you will need to fix your python installation.
* remove and install again
* find out, how your setup differs from the setup of others
Cheers,
Lars
_______________________________________________
openwrt-users mailing list
https://lists.openwrt.org/cgi-bin/mailman/listinfo/openwrt-users
Lars Kruse
2018-04-06 02:47:20 UTC
Permalink
Hello Phani,


Am Wed, 4 Apr 2018 15:02:13 -0500
Post by Phani Siriki
I am able to import the module through normal execution. However it is
not working in rpcd shell script. Could you please give me some
inputs?
you could try to compare the search path under both conditions.

Probably the following would be sufficient?

open("/tmp/pythonpath.out", "w").write("\n".join(sys.path))

(to be placed before the critical import statement)


Cheers,
Lars
Phani Siriki
2018-04-06 15:31:15 UTC
Permalink
Hi Lars

Thanks for your input. The path is same for both conditions. But I
cannot import ctypes while executing through ubus.

Execute shell script:
===============
***@OpenWrt:~# sh foo call tx_power

{ "hello": "yyyyyyyyyyyyyyyyyyy" }
***@OpenWrt:~#
***@OpenWrt:~#
***@OpenWrt:~#
***@OpenWrt:~# cat /tmp/pythonpath.out
/root
/mnt/usb/usr/lib/python2.7/site-packages/pip-1.5.6-py2.7.egg
/mnt/usb/usr/lib/python27.zip
/mnt/usb/usr/lib/python2.7
/mnt/usb/usr/lib/python2.7/plat-linux2
/mnt/usb/usr/lib/python2.7/lib-tk
/mnt/usb/usr/lib/python2.7/lib-old
/mnt/usb/usr/lib/python2.7/lib-dynload
/mnt/usb/usr/lib/python2.7/site-packages
/mnt/usb/usr/lib/python2.7/site-packages/setuptools-7.0-***@OpenWrt:~#
***@OpenWrt:~#

Execute shell script via ubus:
=====================

***@OpenWrt:~# rm /tmp/pythonpath.out
***@OpenWrt:~#
***@OpenWrt:~#
***@OpenWrt:~# ubus call foo tx_power
{
"hello": "yyyyyyyyyyyyyyyyyyy"
}
***@OpenWrt:~#
***@OpenWrt:~#
***@OpenWrt:~# cat /tmp/pythonpath.out
/root
/mnt/usb/usr/lib/python2.7/site-packages/pip-1.5.6-py2.7.egg
/mnt/usb/usr/lib/python27.zip
/mnt/usb/usr/lib/python2.7
/mnt/usb/usr/lib/python2.7/plat-linux2
/mnt/usb/usr/lib/python2.7/lib-tk
/mnt/usb/usr/lib/python2.7/lib-old
/mnt/usb/usr/lib/python2.7/lib-dynload
/mnt/usb/usr/lib/python2.7/site-packages
/mnt/usb/usr/lib/python2.7/site-packages/setuptools-7.0-***@OpenWrt:~#
***@OpenWrt:~#

Best Regards
Phani
Post by Lars Kruse
Hello Phani,
Am Wed, 4 Apr 2018 15:02:13 -0500
Post by Phani Siriki
I am able to import the module through normal execution. However it is
not working in rpcd shell script. Could you please give me some
inputs?
you could try to compare the search path under both conditions.
Probably the following would be sufficient?
open("/tmp/pythonpath.out", "w").write("\n".join(sys.path))
(to be placed before the critical import statement)
Cheers,
Lars
_______________________________________________
openwrt-users mailing list
https://lists.openwrt.org/cgi-bin/mailman/listinfo/openwrt-users
Lars Kruse
2018-04-06 21:20:19 UTC
Permalink
Hello Phani,


let us step back to this issue ...


Am Wed, 4 Apr 2018 15:02:13 -0500
I am able to execute python script after creating below soft links.
ln -s /mnt/usb/usr/lib/libz.so.1 /usr/lib/libz.so.1
ln -s /mnt/usb/lib/libpthread.so.0 /usr/lib/libpthread.so.0
ln -s /mnt/usb/usr/lib/libpython2.7.so.1.0 /usr/lib/libpython2.7.so.1.0
The fact that you need to create symlinks manually feels wrong to me.

I am not used to installing packages to non-root locations, thus I cannot tell,
whether this is normal or an indication of a problem. Can you tell?

I would recommend to verify, that your setup in general is correctly configured.
Did you follow a reasonable documentation? Which one?
Did you deviate? Did you notice problems, that you had to work around?

Maybe someone else has an idea what is going wrong here?

Cheers,
Lars
Phani Siriki
2018-04-06 21:39:07 UTC
Permalink
Hi Lars

I have followed below site to install packages on USB.

http://nixorids.blogspot.com/2013/03/installing-packages-into-usb-on-tl.html

Packages installed in USB are working fine. However, when I am
executing python script in a ubus shell script, it is not working :(

Best Regards
Phani
Post by Lars Kruse
Hello Phani,
let us step back to this issue ...
Am Wed, 4 Apr 2018 15:02:13 -0500
I am able to execute python script after creating below soft links.
ln -s /mnt/usb/usr/lib/libz.so.1 /usr/lib/libz.so.1
ln -s /mnt/usb/lib/libpthread.so.0 /usr/lib/libpthread.so.0
ln -s /mnt/usb/usr/lib/libpython2.7.so.1.0 /usr/lib/libpython2.7.so.1.0
The fact that you need to create symlinks manually feels wrong to me.
I am not used to installing packages to non-root locations, thus I cannot tell,
whether this is normal or an indication of a problem. Can you tell?
I would recommend to verify, that your setup in general is correctly configured.
Did you follow a reasonable documentation? Which one?
Did you deviate? Did you notice problems, that you had to work around?
Maybe someone else has an idea what is going wrong here?
Cheers,
Lars
_______________________________________________
openwrt-users mailing list
https://lists.openwrt.org/cgi-bin/mailman/listinfo/openwrt-users
Damiano Verzulli
2018-04-06 22:20:47 UTC
Permalink
| [...]
I am able to execute python script after creating below soft links.
ln -s /mnt/usb/usr/lib/libz.so.1 /usr/lib/libz.so.1
ln -s /mnt/usb/lib/libpthread.so.0 /usr/lib/libpthread.so.0
ln -s /mnt/usb/usr/lib/libpython2.7.so.1.0 /usr/lib/libpython2.7.so.1.0
The fact that you need to create symlinks manually feels wrong to me.
I am not used to installing packages to non-root locations, thus I cannot tell,
whether this is normal or an indication of a problem. Can you tell?
I guess that the three symbolic links above are required 'cause:

- some packages have been installed inside the USB storage (/mnt/usb) and,
as such, some important libraries (libz.so.1, libpthread.so.0,
libpython2.7.so.1.0) are stored under such folder;

- some other application (Python 2.7, I guess) is "dinamically lynked" to
such libraries. This means that to succesfully run python 2.7, those
libraries needs to be "retrieved and read" (as they are part of python
itself!). If they are NOT found, the application (python 2.7) cannot start;

- the set of folder used by Linux (and OpenWRT) to search for dynamic
libraries is quite fixed and, by default:
- does _NOT_ include /mnt/usr/lib nor /mnt/usb/usr/lib
- _DOES_ include /usr/lib

So, by defining above symbolic links, Phani is simply offering to OpenWRT a
chance to find the dinamic libraries in "default folder" (/usr/lib) even if
they are really stored inside /mnt/usb/usr/lib.


Anyway, the proper way to solve such a problem is a bit different.

As mentioned in the guide pointed by Phani in his last message:

http://nixorids.blogspot.com/2013/03/installing-packages-into-usb-on-tl.html

some environment variable should be "updated" (when relying on USB
storage). In detail:

export USB=/mnt/usb
export PATH=$PATH:$USB/usr/bin:$USB/usr/sbin # This PATH is dependent on
existing $PATH
export LD_LIBRARY_PATH=$USB/lib:$USB/usr/lib

- The first one simply define the mount-point of the USB storage;

- The second one simply tell that binaries stored inside the USB should be
directly accessible, without specifying a full-path;

- The first, and MOST IMPORTANT, line defines _TWO_ folders (/mnt/usb/lib
AND /mnt/usb/usr/lib) to be used for searching of dynamic libraries, IN
ADDITION to standard ones.

Actually, the LD_LIBRARY_PATH environment variables has exactly such a purpose.

Hence, instead of creating the three symbolic links (and, maybe, missing
several others), it's much better to define such a LD_LIBRARY_PATH.

It's important to note that such a LD_LIBRARY_PATH should be defined
_BEFORE_ launching the program requiring related libraries.


Exactly for this reason, the same already mentioned guide refers the
/etc/profile file (where the three environment variables should be defined)
and the command:

# source /etc/profile

that, when launched, "import" such variables inside the current environment.



So, in the end:

1 - remove the symbolic links;
2 - create the /etc/profile taking care to define the three variables;
3 - before launching "python", ensure to "source /etc/profile" [it's
required only once]

In this way, not only the three libraries above, but also any other library
stored inside /mnt/usb should be accessible with not problem.

Hope this help!

Cheers,
DV
--
Damiano Verzulli
e-mail: ***@verzulli.it
---
possible?ok:while(!possible){open_mindedness++}
---
"Technical people tend to fall into two categories: Specialists
and Generalists. The Specialist learns more and more about a
narrower and narrower field, until he eventually, in the limit,
knows everything about nothing. The Generalist learns less and
less about a wider and wider field, until eventually he knows
nothing about everything." - William Stucke - AfrISPA
http://elists.isoc.org/mailman/private/pubsoft/2007-December/001935.html
Phani Siriki
2018-04-06 23:38:34 UTC
Permalink
Hi DV

Thank you for your inputs.

I am unable to import the ctypes library while executing python script
in rpc shell script.

When I run the same script in bash it works fine. ( Even without three
symbolic links.) I have created all the env variables and sourced
profile before running the script.

Best Regards
Phani
Post by Damiano Verzulli
| [...]
I am able to execute python script after creating below soft links.
ln -s /mnt/usb/usr/lib/libz.so.1 /usr/lib/libz.so.1
ln -s /mnt/usb/lib/libpthread.so.0 /usr/lib/libpthread.so.0
ln -s /mnt/usb/usr/lib/libpython2.7.so.1.0 /usr/lib/libpython2.7.so.1.0
The fact that you need to create symlinks manually feels wrong to me.
I am not used to installing packages to non-root locations, thus I cannot tell,
whether this is normal or an indication of a problem. Can you tell?
- some packages have been installed inside the USB storage (/mnt/usb) and,
as such, some important libraries (libz.so.1, libpthread.so.0,
libpython2.7.so.1.0) are stored under such folder;
- some other application (Python 2.7, I guess) is "dinamically lynked" to
such libraries. This means that to succesfully run python 2.7, those
libraries needs to be "retrieved and read" (as they are part of python
itself!). If they are NOT found, the application (python 2.7) cannot start;
- the set of folder used by Linux (and OpenWRT) to search for dynamic
- does _NOT_ include /mnt/usr/lib nor /mnt/usb/usr/lib
- _DOES_ include /usr/lib
So, by defining above symbolic links, Phani is simply offering to OpenWRT a
chance to find the dinamic libraries in "default folder" (/usr/lib) even if
they are really stored inside /mnt/usb/usr/lib.
Anyway, the proper way to solve such a problem is a bit different.
http://nixorids.blogspot.com/2013/03/installing-packages-into-usb-on-tl.html
some environment variable should be "updated" (when relying on USB
export USB=/mnt/usb
export PATH=$PATH:$USB/usr/bin:$USB/usr/sbin # This PATH is dependent on
existing $PATH
export LD_LIBRARY_PATH=$USB/lib:$USB/usr/lib
- The first one simply define the mount-point of the USB storage;
- The second one simply tell that binaries stored inside the USB should be
directly accessible, without specifying a full-path;
- The first, and MOST IMPORTANT, line defines _TWO_ folders (/mnt/usb/lib
AND /mnt/usb/usr/lib) to be used for searching of dynamic libraries, IN
ADDITION to standard ones.
Actually, the LD_LIBRARY_PATH environment variables has exactly such a purpose.
Hence, instead of creating the three symbolic links (and, maybe, missing
several others), it's much better to define such a LD_LIBRARY_PATH.
It's important to note that such a LD_LIBRARY_PATH should be defined
_BEFORE_ launching the program requiring related libraries.
Exactly for this reason, the same already mentioned guide refers the
/etc/profile file (where the three environment variables should be defined)
# source /etc/profile
that, when launched, "import" such variables inside the current environment.
1 - remove the symbolic links;
2 - create the /etc/profile taking care to define the three variables;
3 - before launching "python", ensure to "source /etc/profile" [it's
required only once]
In this way, not only the three libraries above, but also any other library
stored inside /mnt/usb should be accessible with not problem.
Hope this help!
Cheers,
DV
--
Damiano Verzulli
---
possible?ok:while(!possible){open_mindedness++}
---
"Technical people tend to fall into two categories: Specialists
and Generalists. The Specialist learns more and more about a
narrower and narrower field, until he eventually, in the limit,
knows everything about nothing. The Generalist learns less and
less about a wider and wider field, until eventually he knows
nothing about everything." - William Stucke - AfrISPA
http://elists.isoc.org/mailman/private/pubsoft/2007-December/001935.html
_______________________________________________
openwrt-users mailing list
https://lists.openwrt.org/cgi-bin/mailman/listinfo/openwrt-users
Damiano Verzulli
2018-04-07 00:05:18 UTC
Permalink
Post by Phani Siriki
...
I am unable to import the ctypes library while executing python script
in rpc shell script.
Please, provide details: cut&paste of terminal session, detailed command launched and error messages, screenshot, ...whatever.

Without details, I can't get a clear figure of your scenario.
Post by Phani Siriki
When I run the same script in bash it works fine.
Are you sure it's a full BASH Shell?
Post by Phani Siriki
Even without three
symbolic links.
I have created all the env variables and sourced
profile before running the script.
Again: details regarding the script!

Bye,
DV
--
Sent via my new (unlocked) Xiaomi Redmi Note 4
Phani Siriki
2018-04-07 01:25:08 UTC
Permalink
Hi DV

Please find the details below.

I am trying to change tx power of wireless interface using netlink
sockets. I am using libnl python library to achieve this. Please find
the script attached.

When this python program is executed in ash shell (My Openwrt has this
shell) it works fine.

***@OpenWrt:~# iwinfo
wlan0 ESSID: unknown
Access Point: 00:00:00:00:00:00
Mode: Mesh Point Channel: 11 (2.462 GHz)
Tx-Power: 10 dBm Link Quality: unknown/70
Signal: unknown Noise: -95 dBm
Bit Rate: unknown
Encryption: unknown
Type: nl80211 HW Mode(s): 802.11bgn
Hardware: unknown [Generic MAC80211]
TX power offset: unknown
Frequency offset: unknown
Supports VAPs: yes PHY name: phy0

***@OpenWrt:~# python change_wifi_tx_power.py wlan0 5
Tx Power 5
Sent 44 bytes to the kernel.

***@OpenWrt:~# iwinfo
wlan0 ESSID: unknown
Access Point: 00:00:00:00:00:00
Mode: Mesh Point Channel: 11 (2.462 GHz)
Tx-Power: 5 dBm Link Quality: unknown/70
Signal: unknown Noise: -95 dBm
Bit Rate: unknown
Encryption: unknown
Type: nl80211 HW Mode(s): 802.11bgn
Hardware: unknown [Generic MAC80211]
TX power offset: unknown
Frequency offset: unknown
Supports VAPs: yes PHY name: phy0

I am trying to change the tx power parameter without login to the
router. So, I thought of using ubus over http to configure the
parameter. So, inorder to execue my python code, I wrote a rpc shell
script ( foo ) and made it available via ubus.

***@OpenWrt:~# cat /usr/libexec/rpcd/foo
#!/bin/sh
#!/usr/bin/python

. /usr/share/libubox/jshn.sh

case "$1" in
list)
echo '{ "bar": { "arg1": true, "arg2": 32, "arg3":
"str" }, "toto": {"arg1": true }, "tx_power": {"interface":"value",
"tx_power":10} }'
;;
call)
case "$2" in
bar)
# read the arguments
read input;

json_load "$input"

# optionally log the call
logger -t "foo" "call" "$2" "$input"

# return json object or an array
echo '{ "hello":
"aaaaaaaaaaaaaaaaaaaaaaaaaaa" }'
;;
toto)
# return json object or an array
echo '[ "item1", "item2", "item3" ]'
;;

tx_power)
# read the arguments
read input;

json_load "$input"

json_get_var $interface "interface"
json_get_var $tx_power "tx_power"

/usr/bin/python
/root/change_wifi_tx_power.py $interface $tx_power > /root/file 2>&1

echo '{ "hello": "yyyyyyyyyyyyyyyyyyy" }'
;;
esac
;;
esac
***@OpenWrt:~#

***@OpenWrt:~# ubus list -v | grep -A3 foo
'foo' @d222a4a9
"bar":{"arg1":"Boolean","arg2":"Integer","arg3":"String"}
"toto":{"arg1":"Boolean"}
"tx_power":{"interface":"String","tx_power":"Integer"}


When I tried to change the parameter using ubus, I am seeing the import errors.

***@OpenWrt:~# ubus call foo tx_power '{"interface":"wlan0", "tx_power":10}'
{
"hello": "yyyyyyyyyyyyyyyyyyy"
}

Error file:
=======
***@OpenWrt:~# cat file
Traceback (most recent call last):
File "/root/change_wifi_tx_power.py", line 41, in <module>
from libnl.attr import nla_data, nla_get_string, nla_get_u32,
nla_parse, nla_put_u32, nla_get_u64
File "/mnt/usb/usr/lib/python2.7/site-packages/libnl/attr.py", line
15, in <module>
from libnl.linux_private.netlink import NLA_ALIGN, NLA_F_NESTED,
NLA_HDRLEN, NLA_TYPE_MASK, nlattr, NLMSG_ALIGN
File "/mnt/usb/usr/lib/python2.7/site-packages/libnl/linux_private/netlink.py",
line 10, in <module>
from libnl.misc import (bytearray_ptr, c_int, c_uint, c_uint16,
c_uint32, c_ushort, SIZEOF_INT, SIZEOF_U16, SIZEOF_U32,
File "/mnt/usb/usr/lib/python2.7/site-packages/libnl/misc.py", line
3, in <module>
import ctypes
File "/mnt/usb/usr/lib/python2.7/ctypes/__init__.py", line 10, in <module>
from _ctypes import Union, Structure, Array
ImportError: File not found
***@OpenWrt:~#

***@OpenWrt:~# df -h
Filesystem Size Used Available Use% Mounted on
rootfs 4.5M 332.0K 4.2M 7% /
/dev/root 2.3M 2.3M 0 100% /rom
tmpfs 61.6M 64.0K 61.5M 0% /tmp
/dev/mtdblock3 4.5M 332.0K 4.2M 7% /overlay
overlayfs:/overlay 4.5M 332.0K 4.2M 7% /
tmpfs 512.0K 0 512.0K 0% /dev
/dev/sda1 29.2G 68.6M 27.6G 0% /mnt/usb

***@OpenWrt:~# which python
/usr/bin/python
***@OpenWrt:~# ls -l /usr/bin/python
lrwxrwxrwx 1 root root 23 Apr 4 17:42
/usr/bin/python -> /mnt/usb/usr/bin/python

***@OpenWrt:~# ldd python
libpython2.7.so.1.0 => /mnt/usb/usr/lib/libpython2.7.so.1.0 (0x77270000)
libpthread.so.0 => /mnt/usb/lib/libpthread.so.0 (0x7724a000)
libdl.so.0 => /lib/libdl.so.0 (0x77236000)
libutil.so.0 => /lib/libutil.so.0 (0x77225000)
libz.so.1 => /mnt/usb/usr/lib/libz.so.1 (0x77207000)
libm.so.0 => /lib/libm.so.0 (0x771e1000)
libgcc_s.so.1 => /lib/libgcc_s.so.1 (0x771bd000)
libc.so.0 => /lib/libc.so.0 (0x77150000)
ld-uClibc.so.0 => /lib/ld-uClibc.so.0 (0x77406000)

***@OpenWrt:~# env
SSH_CLIENT=192.168.1.121 49290 22
USER=root
LD_LIBRARY_PATH=/mnt/usb/lib:/mnt/usb/usr/lib
SHLVL=1
HOME=/root
SSH_TTY=/dev/pts/0
PS1=\u@\h:\w\$
LOGNAME=root
TERM=xterm-256color
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/mnt/usb/usr/bin:/mnt/usb/usr/sbin
SHELL=/bin/ash
PWD=/root
USB=/mnt/usb
SSH_CONNECTION=192.168.1.121 49290 192.168.1.1 22

***@OpenWrt:~# ls /mnt/usb/usr/lib/python2.7/lib-dynload/_ctypes.so
/mnt/usb/usr/lib/python2.7/lib-dynload/_ctypes.so

Best Regards
Phani
Post by Damiano Verzulli
Post by Phani Siriki
...
I am unable to import the ctypes library while executing python script
in rpc shell script.
Please, provide details: cut&paste of terminal session, detailed command launched and error messages, screenshot, ...whatever.
Without details, I can't get a clear figure of your scenario.
Post by Phani Siriki
When I run the same script in bash it works fine.
Are you sure it's a full BASH Shell?
Post by Phani Siriki
Even without three
symbolic links.
I have created all the env variables and sourced
profile before running the script.
Again: details regarding the script!
Bye,
DV
--
Sent via my new (unlocked) Xiaomi Redmi Note 4
_______________________________________________
openwrt-users mailing list
https://lists.openwrt.org/cgi-bin/mailman/listinfo/openwrt-users
Lars Kruse
2018-04-07 03:30:26 UTC
Permalink
Hello Phani,


thanks to Damiano's summary of proper usage of the external installation root,
it should now be a solvable riddle ...


Am Fri, 6 Apr 2018 20:25:08 -0500
Post by Phani Siriki
[..]
json_get_var $interface "interface"
json_get_var $tx_power "tx_power"
/usr/bin/python
/root/change_wifi_tx_power.py $interface $tx_power > /root/file 2>&1
You need to make sure, that the environment of this python process contains the
environment variables, that Damiano mentioned (specifically LD_LIBRARY_PATH).
Thus you need to source the /etc/profile file before the python-call above.

For your understanding: every process receives an "environment" (set of
textual variables with values) from its parent when it is started.
In your case, then shell script "foo" creates the python process. Thus the
shell script needs to prepare the environment, that the python process should
use. The set of variables that should be inherited by the child process, can be
configured via "export" (in a shell).
Thus it is not relevant, how your shell is configured, when you are interacting
with the script via ubus calls (e.g. "ubus call foo ..."). The only relevant
environment is the one, that is passed from the "foo" shell script to the
python process.

Cheers,
Lars
Phani Siriki
2018-04-07 15:31:21 UTC
Permalink
Hi Lars

Thank you for your clear explanation. After sourcing the profile file,
I am able to execute the python script without any issues.

Best Regards
Phani
Post by Lars Kruse
Hello Phani,
thanks to Damiano's summary of proper usage of the external installation root,
it should now be a solvable riddle ...
Am Fri, 6 Apr 2018 20:25:08 -0500
Post by Phani Siriki
[..]
json_get_var $interface "interface"
json_get_var $tx_power "tx_power"
/usr/bin/python
/root/change_wifi_tx_power.py $interface $tx_power > /root/file 2>&1
You need to make sure, that the environment of this python process contains the
environment variables, that Damiano mentioned (specifically LD_LIBRARY_PATH).
Thus you need to source the /etc/profile file before the python-call above.
For your understanding: every process receives an "environment" (set of
textual variables with values) from its parent when it is started.
In your case, then shell script "foo" creates the python process. Thus the
shell script needs to prepare the environment, that the python process should
use. The set of variables that should be inherited by the child process, can be
configured via "export" (in a shell).
Thus it is not relevant, how your shell is configured, when you are interacting
with the script via ubus calls (e.g. "ubus call foo ..."). The only relevant
environment is the one, that is passed from the "foo" shell script to the
python process.
Cheers,
Lars
_______________________________________________
openwrt-users mailing list
https://lists.openwrt.org/cgi-bin/mailman/listinfo/openwrt-users
Lars Kruse
2018-02-14 20:56:08 UTC
Permalink
Hello Phani,


Am Wed, 14 Feb 2018 12:11:16 -0600
Post by Phani Siriki
I want to execute my custom python scripts in rpc shell script. However,
they are not executed. Could someone help me on this?
I am not familiar with the details of the rpcd insterface you are using - but
probably your problem is generic, anyway.

You should probably walk along the usual debugging path:
- try to find out, why no messages from stderr are printed (failure to call the
python script or failure within the python script) - maybe redirect stderr
to stdout in your call of the python interpreter
- maybe start the python script with some "print" statements (instead of "call")
in order to find out, if the python script is running at all
- if everything fails: use strace or similar tools in order to find out, if the
python script file is accessed at all
- ...

Happy hunting!
Lars


PS: please do not cross post (here: at least openwrt-user and openwrt-devel
mailinglists) - see [1]

[1] http://catb.org/~esr/faqs/smart-questions.html#forum
Loading...