473,788 Members | 3,078 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Show current ip on Linux

Hi,
Im a newbie in Python, and also in Fedora Core 3. (Yes, Linux is fine
man :-)

My question is : How can i rwite a script that show my current ip. If i have
more than one network card, the script must then show all used ip.

It's important that this will work on a linux. i Have rwite some piece of
code that realy work under Windows XP, but the same script wil not work on
Linux.

Verry thanks to all vulunteers.

--
David Van Mosselbeen - DVM
http://dvm.zapto.org:3333
---
Fedora Core 3 User
Jul 19 '05 #1
6 2398
David Van Mosselbeen wrote:
Hi,
Im a newbie in Python, and also in Fedora Core 3. (Yes, Linux is fine
man :-)

My question is : How can i rwite a script that show my current ip. If i have
more than one network card, the script must then show all used ip.

It's important that this will work on a linux. i Have rwite some piece of
code that realy work under Windows XP, but the same script wil not work on
Linux.

Verry thanks to all vulunteers.


How about use the shell command "ifconfig | grep inet" ?
--
Qiangning Hong

_______________ _______________ _______________ _______________ _
( <zhen> so <zhen> when I do a chroot /path /bin/bash, i can )
( see the processes )
( )
( outside of the chroot <zhen> and i can kill those processes )
( * klieber claps for zhen <zhen> oh go die )
-------------------------------------------------------------
o \_______
v__v o \ O )
(OO) ||----w |
(__) || || \/\

Jul 19 '05 #2
Previously, on Jun 13, Qiangning Hong said:

# David Van Mosselbeen wrote:
# > Hi,
# > Im a newbie in Python, and also in Fedora Core 3. (Yes, Linux is fine
# > man :-)
# >
# > My question is : How can i rwite a script that show my current ip. If i have
# > more than one network card, the script must then show all used ip.
# >
# > It's important that this will work on a linux. i Have rwite some piece of
# > code that realy work under Windows XP, but the same script wil not work on
# > Linux.
# >
# > Verry thanks to all vulunteers.
# >
#
# How about use the shell command "ifconfig | grep inet" ?
#

I think you mean ifconfig -a|grep 'inet '

He needs to see all the interfaces, no options will only print a help
message. 'inet ' will get get rid of the inet6 addresses assuming he
doesn't need those.

#
# --
# Qiangning Hong
#
# _______________ _______________ _______________ _______________ _
# ( <zhen> so <zhen> when I do a chroot /path /bin/bash, i can )
# ( see the processes )
# ( )
# ( outside of the chroot <zhen> and i can kill those processes )
# ( * klieber claps for zhen <zhen> oh go die )
# -------------------------------------------------------------
# o \_______
# v__v o \ O )
# (OO) ||----w |
# (__) || || \/\
#
# --
# http://mail.python.org/mailman/listinfo/python-list
#
---
James Tanis
jt****@pycoder. org
http://pycoder.org
Jul 19 '05 #3
On 2005-06-13, David Van Mosselbeen <da************ ******@telenet. be> wrote:
Hi,
Im a newbie in Python, and also in Fedora Core 3. (Yes, Linux is fine
man :-)

My question is : How can i rwite a script that show my current ip. If i have
more than one network card, the script must then show all used ip.

It's important that this will work on a linux. i Have rwite some piece of
code that realy work under Windows XP, but the same script wil not work on
Linux.

Verry thanks to all vulunteers.


This comes up at least once a month. Google. Is. Your. Friend.

http://mail.python.org/pipermail/pyt...ry/258883.html

Jul 19 '05 #4
Lee Harr wrote:
On 2005-06-13, David Van Mosselbeen <da************ ******@telenet. be>
wrote:
Hi,
Im a newbie in Python, and also in Fedora Core 3. (Yes, Linux is fine
man :-)

My question is : How can i rwite a script that show my current ip. If i
have more than one network card, the script must then show all used ip.

It's important that this will work on a linux. i Have rwite some piece of
code that realy work under Windows XP, but the same script wil not work
on Linux.

Verry thanks to all vulunteers.


This comes up at least once a month. Google. Is. Your. Friend.

http://mail.python.org/pipermail/pyt...ry/258883.html


Thanks for support.
I have read the refered page you show above. I try some piece of code that
im have copy and paste it into a blank file that i give the name
"ip_adress. py" to test it.
THE SOURCE CODE :
-----------------

import commands

ifconfig = '/sbin/ifconfig'
# name of ethernet interface
iface = 'eth0'
# text just before inet address in ifconfig output
telltale = 'inet addr:'

def my_addr():
cmd = '%s %s' % (ifconfig, iface)
output = commands.getout put(cmd)

inet = output.find(tel ltale)
if inet >= 0:
start = inet + len(telltale)
end = output.find(' ', start)
addr = output[start:end]
else:
addr = ''

return addr
# End python code

But now, it's fine to have some piece of code but this wil not work on my
computer. I'm sure that y do somethings bad.
To run the python script on a Linux machine. How to proceed it ?

1) I have open a terminal
2) then i type "python ip_adress.py" (to run the script)

But nothings, i not view the current ip of my computer.
What happend ?

--
David Van Mosselbeen - DVM
http://dvm.zapto.org:3333
---
Fedora Core 3 User
Jul 19 '05 #5
David Van Mosselbeen schrieb:

Thanks for support.
I have read the refered page you show above. I try some piece of code that
im have copy and paste it into a blank file that i give the name
"ip_adress. py" to test it.
THE SOURCE CODE :
-----------------

import commands

ifconfig = '/sbin/ifconfig'
# name of ethernet interface
iface = 'eth0'
# text just before inet address in ifconfig output
telltale = 'inet addr:'

def my_addr():
cmd = '%s %s' % (ifconfig, iface)
output = commands.getout put(cmd)

inet = output.find(tel ltale)
if inet >= 0:
start = inet + len(telltale)
end = output.find(' ', start)
addr = output[start:end]
else:
addr = ''

return addr
# End python code

But now, it's fine to have some piece of code but this wil not work on my
computer. I'm sure that y do somethings bad.
To run the python script on a Linux machine. How to proceed it ?

1) I have open a terminal
2) then i type "python ip_adress.py" (to run the script)

But nothings, i not view the current ip of my computer.
What happend ?


You have defined a function, but you never call this function. Running a
script won't do anything, if this script consists only of function (or
class) definitions.

You can either

- open the interactive python interpreter and type:
import ip_adress
ip_adress.my_ad dr()

or, probably simpler,

- add the following two lines to your script, after the definition of
your function:

if __name__ == '__main__':
print my_addr()

Then run your script just as you did before. Now you are calling the
function and printing the value it returns.

--
Dr. Sibylle Koczian
Universitaetsbi bliothek, Abt. Naturwiss.
D-86135 Augsburg
e-mail : Si************* @Bibliothek.Uni-Augsburg.DE
Jul 19 '05 #6
Sibylle Koczian wrote:
David Van Mosselbeen schrieb:

Thanks for support.
I have read the refered page you show above. I try some piece of code
that im have copy and paste it into a blank file that i give the name
"ip_adress. py" to test it.
THE SOURCE CODE :
-----------------

import commands

ifconfig = '/sbin/ifconfig'
# name of ethernet interface
iface = 'eth0'
# text just before inet address in ifconfig output
telltale = 'inet addr:'

def my_addr():
cmd = '%s %s' % (ifconfig, iface)
output = commands.getout put(cmd)

inet = output.find(tel ltale)
if inet >= 0:
start = inet + len(telltale)
end = output.find(' ', start)
addr = output[start:end]
else:
addr = ''

return addr
# End python code

But now, it's fine to have some piece of code but this wil not work on my
computer. I'm sure that y do somethings bad.
To run the python script on a Linux machine. How to proceed it ?

1) I have open a terminal
2) then i type "python ip_adress.py" (to run the script)

But nothings, i not view the current ip of my computer.
What happend ?


You have defined a function, but you never call this function. Running a
script won't do anything, if this script consists only of function (or
class) definitions.

You can either

- open the interactive python interpreter and type:
import ip_adress
ip_adress.my_ad dr()

or, probably simpler,

- add the following two lines to your script, after the definition of
your function:

if __name__ == '__main__':
print my_addr()

Then run your script just as you did before. Now you are calling the
function and printing the value it returns.


It's work now :-)
Verry thanks to all peoples that work free ont participating on
documentation and shares hers brain.

--
David Van Mosselbeen - DVM
http://dvm.zapto.org:3333
---
Fedora Core 3 User
Jul 19 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
5055
by: Tim Williams | last post by:
I'm trying to write a simple python program to access a MySQL database. I'm having a problem with using MySQLdb to get the results of a SQL command in a cursor. Sometimes the cursor.execute works, sometimes not. From mysql: mysql> show databases; +-----------+ | Database |
1
6744
by: Venkat | last post by:
Hi All, I have got two windows namely parent and a child . My parent window has got a link, on clicking which opens a child window(i am using window.open function). I do the following set of operations In my parent window i click the link which results in popping up the child
6
5401
by: Shaun Heveron | last post by:
Hi, I'm working on a portable ANSI C++ application designed to compile and execute under Windows and LINUX/UNIX. Is there a portable way of obtaining the current working directory? Sorry, if I'm being stupid... Thanks,
4
1588
by: kevin.hall | last post by:
Is there a way with Linux x86 to report a way the current stack trace for a thread during runtime without a debugger? (I couldn't find anything searching Google.) For Windows this is possible. Here's an example of an application that uses Window's stack tracing abilities: http://www.codeproject.com/tools/visualleakdetector.asp Many thanks!
5
3188
by: Robert Heuvel | last post by:
Hi, this is what I did: public struct SWaitCursor:IDisposable { public SWaitCursor (int i) { Cursor.Current = Cursors.WaitCursor; } void System.IDisposable.Dispose() { Cursor.Current = Cursors.Default;
4
10605
by: bnob | last post by:
In a Button clik event I have this code at the end of the event Response.Redirect("Page.aspx") But in this event I must show a message before redirect to the Page.aspx. I use to show Message this System.Web.HttpContext.Current.Response.Write("<SCRIPT LANGUAGE=""JavaScript"">" & vbCrLf) System.Web.HttpContext.Current.Response.Write("alert('A message')" &
35
5513
by: Alex Martelli | last post by:
Having fixed a memory leak (not the leak of a Python reference, some other stuff I wasn't properly freeing in certain cases) in a C-coded extension I maintain, I need a way to test that the leak is indeed fixed. Being in a hurry, I originally used a q&d hack...: if sys.platform in ('linux2', 'darwin'): def _memsize(): """ this function tries to return a measurement of how much memory this process is consuming, in some arbitrary unit...
7
2553
by: Scamjunk | last post by:
Hi, I am basically a linux person being made to work on windows. and i have been given a limited version of the windows i guess - for it doesn't provide a proper help option :(. I sorely miss working on linux. but now, I need to get the current date and if possible the current time as well, from the system. I tried a lot of the inbuilt functions (like localtime()) but it gave me errors that i couldn't rightly comprehend. could anybody...
0
2594
by: mel_apiso | last post by:
Hi, after uncatalog one database, and catalog again with other name, if I try to connect with this database, everything is ok, but list applications only show me the connection with the uncatalog/catalog database. After I disconnect from this database, list applications command show all instances connection. Below I paste the situation.
0
9498
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10172
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9964
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8993
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7517
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6749
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5398
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5535
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.