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?
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?
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?
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
:( Else, can anybody tell me how to import a function from one python script into another?
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?
hey..got a way. Running top in background (top &) and issuing killall after some delay.:D
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 :(
~ # top -n 3
top: invalid option -- n
got this............
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
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 :)
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??
Thanks all for the inputs. Happy new year :)
djidjadji's solution worked great:)
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?
I am clueless too... thanx for all the inputs :)
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...
and for..
1.print (str(obj))
2.print(repr(obj))
is..
Null('')
anything useful???
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']
@ ultimatebuster
yeah.....i am going for a dictionary instead. thanx...
@Grib........
value=varBinds[0][1].get()
AttributeError: OctetString instance has no attribute 'get'
k......i can see that every element of string say, 'west' is treated as a seperate string
<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 …
@tonyjv:
print 'East-West ' in str(obj) gave me False
@Grib....
Python gave me this:
<type 'instance'>
pysnmp.proto.rfc1902.OctetString
thanks for the reply...but it's not helping me :(
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?
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....
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...
ok..thanks richieking:)
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
I know there must be something pretty simple, but it's blocking me, need help :)
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....
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
hello.......can any body help me out??
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.
Thanks grib...... it worked with subprocess.
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...
$? 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?
to add on....
how do i pipe the output of that script on remote host onto local host?
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?
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......... :)
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
oh sorry....yes it's getsockname() :)
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....
thanks grib for ur help :)
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?
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?
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?
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?
I'll try the paramiko module