0

I have the following code to re-iterate through a IP address list to find the SNMP string:

INPUT:

import os
import subprocess
ip_list = ['10.10.10.10','10.10.10.11']
snmp = "snmpget -v1 -c public "

for x in ip_list:
     command = os.system(str(snmp + x))

OUTPUT: ... = STRING: "11/15/2017"

This code works fine, but I need to replace os.system with subprocess.call as os.system is very limited and can not be stored in a variable.

I tried to plug in subprocess.call in but i keep getting the error:

results = subprocess.call(os.system(str(snmp + r + static_oid + z)), shell=True)

TypeError: 'int' object is not iterable

Any help would be much appreciated, thanks!

user1130511
  • 243
  • 1
  • 7
  • 18

2 Answers2

2

erm, you're chaining os.system call with subprocess.call call. This is completely wrong. And the cryptic message root cause is that you're passing the return code of os.system which is an integer, when subprocess.call tries to iterate on the argument.

Switching to subprocess is a great idea because os.system is deprecated and is prone to security issues. That is, if you consider dropping shell=True.

And to drop shell=True, you have to pass a list of arguments, not composing your arg string yourself.

My proposal:

import os
import subprocess
ip_list = ['10.10.10.10','10.10.10.11']
snmp = ["snmpget","-v1","-c","public"]   # list of arguments

outputs = []
for x in ip_list:
     cp = subprocess.run(snmp + [x],check=False)
     outputs.append(cp.stdout.decode())

this runs your command in a safe, simple way, gets the output and puts each output line in a list of strings (trying to assign the output didn't work either with call or os.system since it returns the exit code, not the program output. Since your command can return a nonzero return code you cannot use check_output but you can use run is what you need (but you need python 3.5, else you'll have to use a Popen solution, described here: Running shell command and capturing the output):

 p = subprocess.Popen(snmp + [x])
 result = p.communicate()[0]
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
  • Hi, thanks for the quick reply. When i tried snmp + [x], i am getting the TypeError: cannot concatenate 'str' and 'list' objects. any ideas how i can string up snmp and variable x? – user1130511 Mar 04 '19 at 04:05
  • that's because you didn't change `snmp` to list. Check my answer again – Jean-François Fabre Mar 04 '19 at 04:14
  • i see, my bad. Updated snmp variable to: snmp = ["snmpget -v1 -c public "] as it is one argument but now i get this error. OSError: [Errno 2] No such file or directory – user1130511 Mar 04 '19 at 04:41
  • check my answer _again_. Paste from here. you're just putting your string in a list, which isn't correct. – Jean-François Fabre Mar 04 '19 at 04:54
  • hmm, i get the error message: subprocess.CalledProcessError: Command '['snmpget', '-v1', '-c', 'public', '10.10.10.10']' returned non-zero exit status 1 again thanks for the support! – user1130511 Mar 04 '19 at 05:12
  • you have to use `run` which doesn't crash when returncode is not 0, check my edit – Jean-François Fabre Mar 04 '19 at 07:18
  • thanks for pointing me in the right direction! i had to use Popen as i am not running version 3.5. for those who may need it in the future, i used: p = subprocess.Popen(snmp + [x]) result = p.communicate()[0] print result – user1130511 Mar 04 '19 at 14:54
-2

import os import subprocess ip_list = ['10.10.10.10','10.10.10.11'] snmp = ["snmpget","-v1","-c","public"] # list of arguments

outputs = [] for x in ip_list: cp = subprocess.run(snmp + [x],check=False) outputs.append(cp.stdout.decode())

Kefi
  • 67
  • 2
  • 7