LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-28-2010, 05:49 PM   #1
investmentbnker75
Member
 
Registered: Oct 2007
Location: Eastern Seaboard
Distribution: CentOS
Posts: 162

Rep: Reputation: 15
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!

Last edited by investmentbnker75; 09-15-2010 at 05:22 AM.
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 08-28-2010, 07:15 PM   #2
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653
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
 
Old 08-29-2010, 07:20 AM   #3
investmentbnker75
Member
 
Registered: Oct 2007
Location: Eastern Seaboard
Distribution: CentOS
Posts: 162

Original Poster
Rep: Reputation: 15
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

Last edited by investmentbnker75; 08-29-2010 at 07:21 AM. Reason: additional sentence
 
0 members found this post helpful.
Old 08-29-2010, 07:33 AM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
put your Python code in code tags.
 
Old 08-29-2010, 08:11 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,037

Rep: Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203
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.
 
3 members found this post helpful.
Old 08-29-2010, 08:27 AM   #6
estabroo
Senior Member
 
Registered: Jun 2008
Distribution: debian, ubuntu, sidux
Posts: 1,127
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
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
 
Old 08-30-2010, 08:38 AM   #7
investmentbnker75
Member
 
Registered: Oct 2007
Location: Eastern Seaboard
Distribution: CentOS
Posts: 162

Original Poster
Rep: Reputation: 15
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

Last edited by investmentbnker75; 08-30-2010 at 08:42 AM.
 
Old 08-30-2010, 08:41 AM   #8
investmentbnker75
Member
 
Registered: Oct 2007
Location: Eastern Seaboard
Distribution: CentOS
Posts: 162

Original Poster
Rep: Reputation: 15
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.
 
Old 08-30-2010, 11:18 AM   #9
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,823

Rep: Reputation: 2105Reputation: 2105Reputation: 2105Reputation: 2105Reputation: 2105Reputation: 2105Reputation: 2105Reputation: 2105Reputation: 2105Reputation: 2105Reputation: 2105
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
 
Old 08-30-2010, 11:33 AM   #10
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,352

Rep: Reputation: 5386Reputation: 5386Reputation: 5386Reputation: 5386Reputation: 5386Reputation: 5386Reputation: 5386Reputation: 5386Reputation: 5386Reputation: 5386Reputation: 5386
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?

Last edited by dugan; 08-30-2010 at 11:42 AM.
 
1 members found this post helpful.
Old 08-30-2010, 12:14 PM   #11
investmentbnker75
Member
 
Registered: Oct 2007
Location: Eastern Seaboard
Distribution: CentOS
Posts: 162

Original Poster
Rep: Reputation: 15
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.
 
Old 08-31-2010, 09:26 PM   #12
estabroo
Senior Member
 
Registered: Jun 2008
Distribution: debian, ubuntu, sidux
Posts: 1,127
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
Are you sure there wasn't another script, maybe one that imports this one (import switch_temp)?
 
Old 09-01-2010, 09:21 AM   #13
investmentbnker75
Member
 
Registered: Oct 2007
Location: Eastern Seaboard
Distribution: CentOS
Posts: 162

Original Poster
Rep: Reputation: 15
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
 
Old 09-05-2010, 01:38 AM   #14
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,823

Rep: Reputation: 2105Reputation: 2105Reputation: 2105Reputation: 2105Reputation: 2105Reputation: 2105Reputation: 2105Reputation: 2105Reputation: 2105Reputation: 2105Reputation: 2105
Quote:
Originally Posted by investmentbnker75 View Post
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Converting a Shell script to a Python script Aquarius_Girl Programming 4 01-29-2010 12:27 AM
shell script and python? aedmlinux Linux - Laptop and Netbook 3 06-01-2009 10:38 AM
Convert python script to windows executable ankit_mcgill Linux - Newbie 4 10-05-2008 01:21 AM
convert shell script to c code 3saul Linux - Software 1 01-02-2006 09:40 AM
Convert from shell script to binary? Anon123 Linux - General 4 06-26-2004 05:53 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:54 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration