LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Convert python script to shell script (https://www.linuxquestions.org/questions/programming-9/convert-python-script-to-shell-script-829060/)

investmentbnker75 08-28-2010 05:49 PM

Convert python script to shell script
 
I have a python script that i want to convert to shell for a specific reason. The converted script should also be able to accept a command line arg such as hostname, which the current python script doesn't.

Thanks!

kbp 08-28-2010 07:15 PM

Hi,

If you need assistance converting the script then please show us what you have so far and members will be happy to help, if you just want someone to do the work for you maybe you should try http://www.rentacoder.com


cheers

investmentbnker75 08-29-2010 07:20 AM

Umm i dont have anything so far, thats why i posted the question. Please dont use my post as a place to advertise spam. Im only looking for a solution, no spam telling me about another site.

I have reported you, just an FYI

ghostdog74 08-29-2010 07:33 AM

put your Python code in code tags.

grail 08-29-2010 08:11 AM

Before you go jumping on your high horse about reporting people, you might want to look at the words of the site name that implies we will
not do the work for you and that you need to put in some effort. The idea being that no one here is being payed to do your work, so if you need help you will
have to show us what you have tried and where you are getting stuck.

If you are saying you have no idea where to start then you can try searching for bash tutorials (assuming that is what you want as you have also not said
which shell) or you can try this one as point of reference.

estabroo 08-29-2010 08:27 AM

That doesn't look like it's the full script, cause as far as I know python doesn't have snmp built into the core and that is making calls to snmp functions.

with regards to converting it to a bash script, I'd use something like snmpget to replace the oid calls, the rest should be pretty straight forward if else possibly with some sed/awk to get just the parts you want out of the snmp output

investmentbnker75 08-30-2010 08:38 AM

Grail,

First of all, i don't own a horse. Second of all, if you feel you need to be "paid" to offer assistance to this post, then you shouldn't be responding to it. The only people that should respond to it are those who don't mind helping someone out who isn't strong in shell scripting. I happen to be a person who can see the end result and can learn how to do it myself. And if i came across a post like this and knew how to help the person posting it, i would be more than happy to help them and not be worried about getting "paid" for it.

I also don't think people should be posting references to other websites where they are probably the ones who will be making money. It appears as if someone trying to redirect people from this site to other sites to get "paid". So, if you don't want to help, please move on.

Have a nice day

investmentbnker75 08-30-2010 08:41 AM

Estabroo,

As far as i know, its the full script. Unfortunately, i don't know python so i don't know if it calls SNMP or not. To me it looks like its just executing a snmpwalk or get and is using if statements to return output to the command line.

ntubski 08-30-2010 11:18 AM

The code you posted doesn't output anything, also I think it has errors (I had to guess the indentation though).

Code:

#!/usr/bin/python

cisco_envmon_states = {
    1: "normal",
    2: "warning",
    3: "critical",
    4: "shutdown",
    5: "notPresent",
    6: "notFunctioning"
    }

def inventory_switch_temp(checkname, info):
    return [ (line[0], None) for line in info if len(line) == 4 ]

def check_switch_temp(item, params, info):
    for line in info:
        if line[0] == item:
            temp = int(line[1])
            perfdata = [("temp", temp)]
            state = int(line[3])
            statename = cisco_envmon_states.get(state, "(invalid)")
            if state == 1:
                return (0, "OK - %s degrees" % temp, perfdata)
            elif state == 2:
                return (0, "WARN - state is %s, %s degrees" % (statename, temp), perfdata)
            elif state in [ 5, 6 ]:
                return (3, "UNKNOWN - state is %s" % statename)
            else:
                return (2, "CRIT - state is %s, %d degrees (max is %s)" % (statename, temp, line[2]), perfdata)
            return (3, "UNKNOWN - Item %s not found in SNMP data" % item)

check_info['switch_temp'] = (check_switch_temp, "%s", 1, inventory_switch_temp)
snmp_info['switch_temp'] = ( "1.3.6.1.4.1.9.9.13.1.3.1", [ 2, 3, 4, 6 ] ) # CISCO-SMI
snmp_scan_functions['switch_temp'] = \
    lambda oid: "cisco" in oid(".1.3.6.1.2.1.1.1.0").lower() and \
    not not oid("1.3.6.1.4.1.9.9.13.1.3.1.3.1")

Code:

~/tmp$ ./switch_temp.py
Traceback (most recent call last):
  File "switch_temp.py", line 32, in <module>
    check_info['switch_temp'] = (check_switch_temp, "%s", 1, inventory_switch_temp)
NameError: name 'check_info' is not defined


dugan 08-30-2010 11:33 AM

You'll need to a) format the code listing, because indentation matters in Python, and b) be more specific about the help that you need, because converting the whole script for you is out of the question. For example, do you need a tutorial to quickly get up to speed on Python? For another example, is there some specific functionality (e.g. accepting command-line arguments) that you don't know how to express in Bash?

investmentbnker75 08-30-2010 12:14 PM

No the reason it needs to be converted to shell script is the server it runs on cant read python scripts. Its the univesities server so i cant alter it.

estabroo 08-31-2010 09:26 PM

Are you sure there wasn't another script, maybe one that imports this one (import switch_temp)?

investmentbnker75 09-01-2010 09:21 AM

Dugan, thanks for your response but your link to python wouldnt help in this case because it must be shell as noted in my original post.

estabroo, i will try and find out the answer to your question about there being another script, im not sure though

ntubski, im not sure what this was that you posted, could you clarify please?

Thanks all for trying to assist on this!

~/tmp$ ./switch_temp.py
Traceback (most recent call last):
File "switch_temp.py", line 32, in <module>
check_info['switch_temp'] = (check_switch_temp, "%s", 1, inventory_switch_temp)
NameError: name 'check_info' is not defined

ntubski 09-05-2010 01:38 AM

Quote:

Originally Posted by investmentbnker75 (Post 4084877)
ntubski, im not sure what this was that you posted, could you clarify please?

I attempted to run the code you posted, but it resulted in an error. Since your python code doesn't work, I can't provide a working shell translation.


All times are GMT -5. The time now is 05:22 AM.