1

I have an SNMP pass script that I am running and, for some reason that I do not yet understand, additional uses of echo in the script causes the script to not execute. My precise issue right now is that I need to take a substring from a variable, REQ, and assign it to another variable. Below is a method that works in theory, but does not work in my situation due to the use of echo.

DGROUP_NODE=`echo $REQ | cut -d. -f3`

Is there another way to do this without disrupting whatever is reading the script? I have noticed that printf also tends to have the same issue, though less often. (This issue also prevents me from using echo or printf to debug my code...)

NOTES: When echo causes issues executing the script, no errors are returned (regarding the script). I only get No Such Instance currently exists at this OID which tells me that the script was not run, resulting in my OID tree not being built.

If I run the script outside of SNMP it works as expected.

Ctrl S
  • 1,053
  • 12
  • 31
  • `RET` != `REQ`, which is it? AND you'll almost always want to dbl-quote variable usage with `echo`, so `echo "$REQ"` may solve your problem. Good luck. – shellter Jun 27 '18 at 15:33
  • Should be `REQ`. I've tried adding quotes already, but the issue is with `echo` itself. The script works fine when run directly from terminal. – Ctrl S Jun 27 '18 at 16:20

3 Answers3

1

If, for example, I wanted just the PEN:

TREE=.1.3.6.1.4.1.8072.2.255
CHOP=.1.3.6.1.4.1.
PEN=${TREE##*$CHOP} # remove $CHOP from beginning (## = longest match from beginning)
PEN=${PEN%%.*} # keep everything before first period (%% = longest match from end)

Output:

8072

More about this method here: extract part of a string

Ctrl S
  • 1,053
  • 12
  • 31
0

Use set +x then run again. I am sure the problem is that $RET is empty.

jas-
  • 1,801
  • 1
  • 18
  • 30
  • `set +x` in terminal before running? – Ctrl S Jun 27 '18 at 13:11
  • When I do like so: `$ set +x` `$ snmpwalk -v2c -cpublic localhost .1.3.6.1.4.1.(PEN)` I get the same issue: `MIB::module = No Such Instance currently exists at this OID` – Ctrl S Jun 27 '18 at 13:13
  • 1
    You should look into pipes and redirection in bash... that is going to stderr. – jas- Jun 27 '18 at 13:19
  • `$REQ` can't be empty because it is used in a `case` immediately after. The issue only occurs when adding additional `echo` statements AND running the script via SNMP. If I run the script outside of SNMP it works as expected. – Ctrl S Jun 27 '18 at 13:21
  • Okay, will look into that. – Ctrl S Jun 27 '18 at 13:24
  • Even with redirection, I have the same issue. Running the script with SNMP, `echo` prevents it from working. Running the script standalone, it runs and `echo` outputs as expected. – Ctrl S Jun 27 '18 at 13:44
0

Enclose echo with $ from your code like this:

DGROUP_NODE=$(echo $REQ | cut -d. -f3)
pegasuspect
  • 991
  • 4
  • 15
  • The code needs to work. The visual absence of `echo` is not sufficient. – Ctrl S Jun 27 '18 at 14:13
  • I updated the code, sorry i had to test it. You are right. – pegasuspect Jun 27 '18 at 14:45
  • Error: `.snmp/pass_script_backup/passScript: 41: .snmp/pass_script_backup/passScript: 3: not found` "41" is the line in the script, and "3" is the retrieved value from $REQ. It seems to be looking for a variable called "3" – Ctrl S Jun 27 '18 at 14:55
  • `registered debug token ucd-snmp/pass, 1 pcilib: Cannot open /proc/bus/pci pcilib: Cannot find any working access method. pcilib: pci_init failed Turning on AgentX master support. Error opening specified endpoint "" Server Exiting with code 1` – Ctrl S Jun 27 '18 at 16:14
  • Check this for the issue please: https://www.raspberrypi.org/forums/viewtopic.php?t=129258#p988886 – pegasuspect Jun 27 '18 at 16:26
  • Already been down the PCI "issue" road lol. I don't believe the absence of PCI is really causing any true errors. Thanks though! – Ctrl S Jun 27 '18 at 16:29
  • Ok, what is you pass script looks like around line 41? It would be a whole lot better if you could share the whole file itself. – pegasuspect Jun 27 '18 at 16:36
  • A sample of my code structure has been added to the question. – Ctrl S Jun 27 '18 at 17:29
  • and line 41 is equivalent to which line on the sample? – pegasuspect Jun 27 '18 at 18:18
  • The one in your answer. – Ctrl S Jun 27 '18 at 18:51
  • Can you show me what this command tells you on the console? `ls -alF .snmp/pass_script_backup/passScript`? – pegasuspect Jun 27 '18 at 19:03
  • -rw-r--r-- 1 pi pi 60371 Jun 27 15:05 .snmp/pass_script_backup/passScript – Ctrl S Jun 27 '18 at 19:07
  • ok can you try this now `chmod 775 .snmp/pass_script_backup/passScript` – pegasuspect Jun 27 '18 at 19:10
  • if it doesn't solve your issue also try this one: `chown $(id -un):$(id -Gn | cut -d' ' -f1) .snmp/pass_script_backup/passScript` – pegasuspect Jun 27 '18 at 19:11
  • What leads you to believe that permissions are the issue? – Ctrl S Jun 27 '18 at 19:12
  • Because the error talks about cannot open, or working access. I would give it a try. – pegasuspect Jun 27 '18 at 19:13
  • If you had read the link that you sent me, you'd see that it is because the Pi does not have PCI hardware. No amount of permissions will create hardware. Also, that error is completely unrelated to the issue. The PCI error is caused by SNMP not finding PCI on the Pi. My issue is regarding shell syntax... – Ctrl S Jun 27 '18 at 19:14
  • You are certainly right. That is all I know to offer for help. I hope you can find the answer. Best of luck! – pegasuspect Jun 27 '18 at 19:16
  • Alright, thanks anyway. I have found an alternative solution for the time being. – Ctrl S Jun 27 '18 at 19:18