-1

Hi I am trying to use ipaddress which is integer in this program. but I need to call this as string in response = os.system("ping -c 1 " + hostname + "-I" + str(mystring))

#!/usr/bin/python
import os
interface = os.system("ifconfig ge1 | grep UP")
ip = os.system("ifconfig ge1.1 | grep UP")
ipaddress = os.system("ifconfig ge1 | grep 'inet addr:' | cut -d: -f2 |  awk '{ print $1}'")
print ipaddress
mystring = repr(ipaddress)

print mystring

if interface == 0:
 print interface, ' interface is UP!'
 hostname = "8.8.8.8"
 response = os.system("ping -c 1 " + hostname + "-I" + str(mystring))
 if response == 0:
  print hostname, 'is up!'
 else:
   print hostname, 'is down!'
else:
   print interface, ' interface is down!'
Andy
  • 49,085
  • 60
  • 166
  • 233
user1056087
  • 17
  • 1
  • 1
  • 2
  • 1
    Import ipaddress class – stark Jun 20 '15 at 13:20
  • os.system() returns exit status code, not an IP address ! – Iron Fist Jun 20 '15 at 13:28
  • 1
    http://stackoverflow.com/questions/24196932/how-can-i-get-the-ip-address-of-eth0-in-python/24196955#24196955 – stark Jun 20 '15 at 13:28
  • My idea is to form a command to see destination 8.8.8.8 is reachable form interface whose ip address keep on changing. if destination is reachable, I want to consider the exit code is true and then I will do some other extra work. for eample : ping -c1 8.8.8.8 -I (here I need to have dynamic ip address assigned on Ethernet port 0) – user1056087 Jun 20 '15 at 17:06

1 Answers1

0

os.system("ifconfig ge1 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}'") will not return to you IP address instead EXIT STATUS code, so you need to use a module that gets you the IP address of your interface(eth0, WLAN0..etc),

As suggested by the @stark link comment, use netifaces package or socket module, examples taken from this post :

import netifaces as ni
ni.ifaddresses('eth0')
ip = ni.ifaddresses('eth0')[2][0]['addr']
print ip

===========================================================================

import socket
import fcntl
import struct

def get_ip_address(ifname):
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    return socket.inet_ntoa(fcntl.ioctl(
        s.fileno(),
        0x8915,  # SIOCGIFADDR
        struct.pack('256s', ifname[:15])
    )[20:24])

get_ip_address('eth0')

EDIT-1:

It is recommended that you run your terminal commands through subprocess rather than os.system as I've read it's a lot more safer.

Now, if you wan to pass the result of ip_address into your ping command, here we go:

import subprocess
import socket
import fcntl
import struct

def get_ip_address(ifname):
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    return socket.inet_ntoa(fcntl.ioctl(
        s.fileno(),
        0x8915,  # SIOCGIFADDR
        struct.pack('256s', ifname[:15])
    )[20:24])


hostname = "8.8.8.8"
cmdping = "ping -c 1 " + hostname + " -I " + get_ip_address('eth0')

p = subprocess.Popen(cmdping, shell=True, stderr=subprocess.PIPE)

#The following while loop is meant to get you the output in real time, not to wait for the process to finish until then print the output.

while True:
    out = p.stderr.read(1)
    if out == '' and p.poll() != None:
        break
    if out != '':
        sys.stdout.write(out)
        sys.stdout.flush()
Community
  • 1
  • 1
Iron Fist
  • 10,739
  • 2
  • 18
  • 34
  • Thanks Khalil. from the above code. I am able to find ip address but how I can build that value as input for the command ping 8.8.8.8 -I get_ip_address('eth0') – user1056087 Jun 20 '15 at 16:48