why os module in python under performing than shell system?

Multi tool use
Multi tool use


why os module in python under performing than shell system?



I have written a python script in which I use the os package module to send snmp related commands through net snmp
The following is the code snippet which is doing that
snmpsend.py


import os
CMD='snmpset -v 2c -c public 192.168.3.3 .1.3.9.5.2.45672.5.3.11.67.0 s "ON" '
os.system(CMD)



The error obtained is "Timeout:No Response from 192.168.3.3"
The response from the SNMP agent is a timeout response
Then a bash script is used to send the same command



snmpsend.sh


#!/usr/bin/sh
snmpset -v 2c -c public 192.168.3.3 .1.3.9.5.2.45672.5.3.11.67.0 s "ON"



The error obtained is "Configuration error"
Which is the error message programmed at the agent side when it receives this command.
So same command is invoking different behaviour while using shell and python.
I am using Ubuntu 16.04 version as the OS.
The object IDs used and the IP addresses used as not real to protect proprietary information.



my core part is the snmpset command and that remains the name in both the code snippets.
But the python is causing some issue and shell is working fine ,so removing the python usage is the way forward as a final conclusion by a veteran.



Please guide me to find the root cause in this case where python os.system have performed less than the shell version. Did any one face this before?





your command lines are different.
– Jean-François Fabre
Jul 2 at 14:01





It's unlikely you'll be able to find anyone that can help with the question as is. As an absolute minimum you should post any error output and return codes from os.system. You should post a minmal, complete, and verifiable example. Otherwise, your problem could be anywhere, including a C&P mistake with the URL.
– Dunes
Jul 2 at 14:01


os.system





Using the deprecated os.system interface creates an extra shell process, however I would not have thought that it would make a noticeable difference. Have you benchmarked this? Are you timing the whole python script vs the shell script, or just part of it?
– cdarke
Jul 2 at 14:01



os.system





use subprocess.check_output with an argument list.
– Jean-François Fabre
Jul 2 at 14:01


subprocess.check_output





One is starting Python, and then having Python start a shell interpreter, and having the shell interpreter start sh -c 'yourcommand'. The other starts a shell interpreter directly, and has that directly-invoked shell interpreter invoke the command itself. Starting up more processes obviously has more overhead.
– Charles Duffy
Jul 2 at 15:09



sh -c 'yourcommand'









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

OiSa tkKqUB Q,vF2qWEirsLs3IlW
r,SrQdteJ1r,JpgTtoY27TgAR

Popular posts from this blog

PHP contact form sending but not receiving emails

Do graphics cards have individual ID by which single devices can be distinguished?

Create weekly swift ios local notifications