novice20 17 Junior Poster

And...
import in script2 worked fine when mode_check() was defined before main.

Please let me know, why is it wrong to define a function before main, if it is so? and why did it work now?

novice20 17 Junior Poster

Thanks...

will abide by your suggestion of not using bold everywhere :)

I am not clear with this:
So we save this as average.py in python folder(or a python path folder so python can find it)
can u pls tell me more about this?

novice20 17 Junior Poster

Hi all..
U ppl have been of great help to me, guiding me throughout my project :)

well, importing a function from another module is not working for me :(
Pls let me know where am I going wrong. :(

scenario is as such..
My script1.py goes like this


def main():
            ###
            ###
            ....
            ###
            ###



 def mode_check(op_mode):
                #-- 
                ###
                return flag

if __name__== "__main__":
        main()

and when i try importing the functiom mode_check in script2.py

from script1 import *
##########
#########
##########
status=mode_check(mode)
########
############

gives me the following error:

NameError: global name 'mode_check' is not defined
novice20 17 Junior Poster

:( Else, can anybody tell me how to import a function from one python script into another?

novice20 17 Junior Poster

Hi all..
I need to do a condition check in a script. The same condition check needs to be repeated in another script. So, I am looking for a way, where i can set, say a flag in the first script ,upon meeting the condition, and be able to access that flag in another script, instead of repeating the check all over again.

Hope I am clear in explaining what I want to do. Can anyone pls give me suggestions?

novice20 17 Junior Poster

hey..got a way. Running top in background (top &) and issuing killall after some delay.:D

novice20 17 Junior Poster

Thanks Grib...

well, I dont know how to put the things in words.

The host I am running the 'top' command on is a proprietary unit of the company's client.

uname -a gives me just Linux XXXX, said to be having kernel version 0.3.4.

I don't know further details,but, top -n is not working :(

novice20 17 Junior Poster
~ # top -n 3
top: invalid option -- n

got this............

novice20 17 Junior Poster

hi all

using telnetlib, I am issuing a 'top' command on a remote host. Top seems to continue indefinitely. On a terminal, I can use CTRL+c to suspend the process...


What can be passed in telnet.write() equivalently to suspend the process , yet retain the output so far...


pls help me out....:S

novice20 17 Junior Poster

well..it was pretty simple....but couldn't get it.
achieved it with time.sleep() after logging and before issue of telnet.write("free\n")
command :)

novice20 17 Junior Poster

hi,

my problem is as such...

I am establishing a telnet session to an IP using telnetlib module. I read the free memory on the board using telnet.write("free\n").

after this i call a system command 'snmpwalk' to walk the MIBs of the IP above.


I again need to establish the session and read the free memory on that IP.


what I observe is, the system command(snmpwalk) is executed before the telnet.write.

what can be done to maintain the flow as mentioned above??

novice20 17 Junior Poster

Thanks all for the inputs. Happy new year :)

djidjadji's solution worked great:)

novice20 17 Junior Poster

Hi...

I have a set of strings as below:

4:14.4-17M,5:14.4-2e13M,6:14.4-4e9M,7:14.4-8e,22:28.4-35M,23:28.4-2e30M,24:28.4-4e26M,25:28.4-8e18M,26:28.4-16e2M,27:28.4-18e,28:14.16-36M,29:14.16-2e32M,30:14.16-4e28M,31:14.16-8e20M

I want everything after the ':' to be discarded. i.e., i just want a list of what preceeds ':' (4,5,6,7,22,.................)

How can this be done?

novice20 17 Junior Poster

I am clueless too... thanx for all the inputs :)

novice20 17 Junior Poster

I found a solution :)

a=str(obj).split(';')
                for vals in a:
                        print vals

gives me:

1+1 Protection
 East-West
 East-East
 Spiral Search
 ............
 ...........

any better solutions are welcome...

novice20 17 Junior Poster

and for..

1.print (str(obj))
   2.print(repr(obj))

is..

Null('')

anything useful???

novice20 17 Junior Poster

Output for

print dir(obj)

['_AbstractSimpleAsn1Item__hashedValue', '__add__', '__cmp__', '__doc__', '__getitem__', '__hash__', '__init__', '__len__', '__module__', '__mul__', '__nonzero__', '__radd__', '__repr__', '__rmul__', '__str__', '_subtypeSpec', '_tagSet', '_value', '_verifySubtypeSpec', 'clone', 'defaultValue', 'getSubtypeSpec', 'getTagSet', 'getTypeMap', 'isSameTypeWith', 'isSuperTypeOf', 'prettyIn', 'prettyOut', 'prettyPrint', 'prettyPrinter', 'subtype', 'subtypeSpec', 'tagSet']
novice20 17 Junior Poster

@ ultimatebuster
yeah.....i am going for a dictionary instead. thanx...

novice20 17 Junior Poster

@Grib........

value=varBinds[0][1].get()
AttributeError: OctetString instance has no attribute 'get'

novice20 17 Junior Poster

k......i can see that every element of string say, 'west' is treated as a seperate string

novice20 17 Junior Poster
<type 'str'>
22 W
<type 'str'>
23 e
<type 'str'>
24 s
<type 'str'>
25 t
<type 'str'>
26 ;
<type 'str'>
27
<type 'str'>
28 E
<type 'str'>
29 a
<type 'str'>
30 s
<type 'str'>
31 t
<type 'str'>
32 -
<type 'str'>
33 E
<type 'str'>
34 a
<type 'str'>
35 s
<type 'str'>
36 t
<type 'str'>
37 ;
<type 'str'>
38
<type 'str'>
39 S
<type 'str'>
40 p
<type 'str'>
41 i
<type 'str'>
42 r
<type 'str'>
43 a
<type 'str'>
44 l
<type 'str'>
45
<type 'str'>
46 S
<type 'str'>
47 e
<type 'str'>
48 a
<type 'str'>
49 r
<type 'str'>
50 c
<type 'str'>
51 h
<type 'str'>
52 ;
<type 'str'>
53
<type 'str'>
54 O
<type 'str'>
55 D
<type 'str'>
56 U
<type 'str'>
57
<type 'str'>
58 E
<type 'str'>
59 n
<type 'str'>
60 a
<type 'str'>
61 b
<type 'str'>
62 l
<type 'str'>
63 e
<type 'str'>
64 /
<type 'str'>
65 D
<type 'str'>
66 i
<type 'str'>
67 s
<type 'str'>
68 a
<type 'str'>
69 b
<type 'str'>
70 l
<type 'str'>
71 e
<type 'str'>
72 ;
<type 'str'>
73
<type 'str'>
74 M
<type 'str'>
75 a
<type 'str'>
76 n
<type 'str'>
77 u
<type 'str'>
78 a
<type 'str'>
79 l
<type 'str'>
80
<type 'str'>
81 F
<type 'str'>
82 r
<type 'str'>
83 e
<type 'str'>
84 q
<type 'str'>
85 u …
novice20 17 Junior Poster

@tonyjv:


print 'East-West ' in str(obj) gave me False

novice20 17 Junior Poster

@Grib....

Python gave me this:

<type 'instance'>
pysnmp.proto.rfc1902.OctetString

novice20 17 Junior Poster

thanks for the reply...but it's not helping me :(

novice20 17 Junior Poster

hi...

using pysnmp, an 'snmp get' varBinds gives me an 'octet string' of values like:

1+1 Protection; East-West; Spiral Search; ODU Enable/Disable

I need to loop through these strings and see whether a particular string say, 'East-West' is present. I am not finding way to do this.

can this octet string be converted to some other form?

novice20 17 Junior Poster

I got a solution on one of the forums...

class Materials:
Shaded, Shiny, Transparent, Matte = range(4)

>>> print Materials.Matte
3

If U have better ideas, do suggest pls....

novice20 17 Junior Poster

can anyone suggest a simple method to define an enum in python.

as some examples floating on net, i used

class Animal:
DOG=1
CAT=2
print Animal.DOG


but it doesn't seem to be working...

novice20 17 Junior Poster

ok..thanks richieking:)

novice20 17 Junior Poster

ok...it was that simple! I couldn't get it...now can i store the values in an array kind of structure? list is not helping me

novice20 17 Junior Poster

I know there must be something pretty simple, but it's blocking me, need help :)

novice20 17 Junior Poster

Grib......... thanks for the reply, but it still gives me:

1,2,3,4
5,6,7,8

i want to access 1 first followed by 2, followed by 3 and so on....

novice20 17 Junior Poster

hi,

I just came accross the csv module to deals with csv files in python.

suppose my csv file has two rows:

1,2,3,4
5,6,7,8

and i want to access each item at a time, i.e., 1 followed by 2 and so on....what can be done?

import csv
reader= csv.reader(open('filename.csv','r'),delimiter=' ')
for row in reader:
print row

gives me
[1,2,3,4]
[5,6,7,8]

I want to access a single value at a time

novice20 17 Junior Poster

hello.......can any body help me out??

novice20 17 Junior Poster

telnetlib is helping me to connect to remote host and do the user login.
but, when i try doing the super user login, it hangs

...............
..............
...............
User="abc"
Password="abc"
Superuser_id="yyy"
su_passwd="xxx"

#-- establish a telnet connection to all the remote hosts in the list except the host running script
telnet=telnetlib.Telnet(Host)
telnet.read_until("login:")
telnet.write(User+'\r\n')
telnet.read_until("Password:")
telnet.write(Password+'\r\n')
time.sleep(5)
print telnet.read_until('[abc@xyz abc]$ ')
telnet.write(Superuser_id+'\r\n')
print telnet.read_until("Password:")
telnet.write(su_passwd+'\r\n')
time.sleep(5)
print telnet.read_until('[root@xyz abc]#')
telnet.write("cd /folder1/folder2/%s\n"%version)
.............
...........
...........
any idea why it hangs and doesnt do a cd:?
it works fine when i don't try to do a super user login.

novice20 17 Junior Poster

Thanks grib...... it worked with subprocess.

novice20 17 Junior Poster

I need to install python 2.4 on a fedora 3 system. I followed the instructions in
http://www.python.org/download/releases/2.4/rpms/
i have the following contents in /etc/yum.repos.d/python24.repo

[python24]
name=Fedora Core 3 - $basearch - Python 2.4
baseurl=http://www.python.org/ftp/python/2.4/rpms/fedora-3/
enabled=1
gpgcheck=1

further following the instructions, rpm --import http://www.python.org/ftp/python/2.4/rpms/KRUD-GPG-KEY gave me:
error: http://www.python.org/ftp/python/2.4/rpms/KRUD-GPG-KEY: import read failed.

after a lot of search i found the above command to be replaced by
rpm --import /usr/share/rhn/RPM-GPG-KEY
rpm --import /usr/share/rhn/RPM-GPG-KEY-fedora
at http://forums.fedoraforum.org/archive/index.php/t-94931.html

after this, when i run yum install python 2.4, it gives me the following error:

ImportError: librpm-4.3.so: cannot open shared object file: No such file or directory

anybody pls tell me what can be done?? I am a project trainee and new to such stuffs, so veterans out there, pls help me...

novice20 17 Junior Poster

$? in linux gives the exit status of the recently executed command , as i have learnt.
suppose i give a cd command at the terminal and if such a directory or file is not present, echo $? following it gives me 1.

but, when the same is run via a script using os.system("cd.........."),
os.system("echo $?") gives 0.

it might be simple, but I am not able to trace out the reason.. can anyone throw some light on this?

novice20 17 Junior Poster

to add on....

how do i pipe the output of that script on remote host onto local host?

novice20 17 Junior Poster

hi....


i created a telnet connection to a remote server using telnetlib and excuted few commands using telnet.write. can't a python script residing on the remote host be run using telnet.write?
i.e, do something like

telnet.write("./script_name.py argument1 arguement2........") after the connection is established?

novice20 17 Junior Poster

You only need to match the regex r"adr[:](\d+(?:\.\d+){3})" . The benefit is that you don't need to connect to a remote address. But if you want a cross platform code, we should also examine the output of ipconfig in windows.

yes..... it is enlightening..

Thanx a lot Grib......... :)

novice20 17 Junior Poster

oh sorry....yes it's getsockname() :)

yes..have tried it...but ifconfig gives a lot of info and we need to parse the output to get the ip address

novice20 17 Junior Poster

oh sorry....yes it's getsockname() :)

novice20 17 Junior Poster

thanks Grib....

I need to maintain a list of servers that host the software release folders. I need my script to be generic, i.e., it can be run on any of the servers.
I need to check the ip address of the host on which am running the script, and for the servers in list other than my host, i need to do a telnet to connect to it and get the folder,if present do an FTP upload.

I got this Crude method of creating a dummy socket and getting my IP address from ASPN Python cookbook

import socket

s=socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(('google.com',80))
return s.getsocketname()[0]

and it helped me....

Gribouillis commented: Nice trick ! +4
novice20 17 Junior Poster

hi all...

how to find the localhost ip address in python

import socket
socket.gethostbyname(socket.gethostname())

gives me 127.0.0.1 instead of my ip address

any help...........

novice20 17 Junior Poster

thanks grib for ur help :)

novice20 17 Junior Poster

k...Grib..thanx for ur effort in helping me...

is there any way to save output of individual commands over telnet session onto seperate variables instead of using Telnet.read_all() at the end? or
is there any way to save the output of Telnet.read_all() to a file , so that the output can be parsed?

novice20 17 Junior Poster

Thanx Grib.........

I tried it, bu it gives "none" no matter the file is present or not.
Learnt that linux stores the return value of recently executed command in the variable $?. Is there any way of copying the value of $?of remote host onto a variable in the host where i run the script?

novice20 17 Junior Poster

thanks a lot grib :)

I am able to connect to remote host and change directory. I need to capture the return value of Telnet.write(). What is it's type and value?

I mean to say, what does it return when a path specified with cd is present and when it is not present?

novice20 17 Junior Poster

thanks a lot grib....

my guide suggested me to prefer telnet over ssh, as it is widely used. so I am thinking of using the module 'telnetlib'.

I am doing the following:

import telnetlib
...........
........
..........


user=User
password=pass

telnet=telnetlib.Telnet(host)
telnet.write(user+'\r\n')
telnet.write(password+'\r\n')after this, i need to check whether a folder exists on that host and if present, do an FTP upload.. how do I proceed?

novice20 17 Junior Poster

I'll try the paramiko module