473,327 Members | 2,074 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

how to execute the shell script in python

Hi,

I need to execute the below script in python to find the ip address of a host

/sbin/ifconfig'+'|'+' grep 'inet addr:''+'|'+' grep -v '127.0.0.1''+'|'+'cut -d: -f2'+'|'+'awk '{ print $1}'

Please let me know how can I execute that in python script.

Regards,
Siva
Nov 18 '14 #1
5 1448
bvdet
2,851 Expert Mod 2GB
socket.gethostbyname(hostname) will return the IPv4 address of a host name.
Nov 18 '14 #2
Hi bvdet,

The
Expand|Select|Wrap|Line Numbers
  1. socket.gethostbyname(hostname)
command returns only loopback address 127.0.0.1 but it won't display actual IP address like '10.111.212.111. Thats the reason i want to execute the below command in python.

Please let me know how can i execute the below command in python

Expand|Select|Wrap|Line Numbers
  1. sbin/ifconfig | grep 'inet addr:' | grep -v '127.0.0.1' | 'cut -d: -f2 | awk '{ print $1}'
Regards,
Siva
Nov 18 '14 #3
bvdet
2,851 Expert Mod 2GB
How about this:
Expand|Select|Wrap|Line Numbers
  1. >>> socket.gethostbyname(socket.gethostname())
  2. '10.0.0.3'
  3. >>> 
I'm on Win 7.
Nov 18 '14 #4
Hi

I got the answer for this.
Expand|Select|Wrap|Line Numbers
  1. import commands
  2.  
  3. commands.getoutput("/sbin/ifconfig | grep 'inet addr:' | grep -v '127.0.0.1' |cut -d: -f2 | awk '{ print $1}'")
  4.  
will return the actual ip address like '10.120.11.123'

Regards,
Siva
Nov 19 '14 #5
dwblas
626 Expert 512MB
You can use the subprocess.check_output module to get the output from ifconfig and then parse that to find the inet rec. Note that on my system the rec starts with inet, not inet addr.
Nov 19 '14 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: spec | last post by:
Hi all, I know nothing about Python. What I need to do is to get a Python script to execute a local shell script. I do not need any output. What would be th eeasiest way to accomplish this? ...
9
by: sohan | last post by:
Hi, I want to know how to connect and execute a db2 query from inside a UNIX shell script. Details: We have a unix shell script. We need to execute multiple db2 sql queries from this shell...
2
by: mesum98 | last post by:
Hi, I have a python script which creates a csv file using following method def printOutInformation(self): if self.orderby == 'r': ...
3
by: yogesh_anand | last post by:
Hi I am running one shell script from my application process which is running on sun solaris platform. If i run script suppose as "oracle" user then its successfully execute within process. ...
3
by: telduivel | last post by:
Can someone please help me with this: I have a python script, that at some point calls a linux bash script (.sh). Starting the shell script is the last thing my python script needs to do, so I...
1
by: ksn2007 | last post by:
Hi All, I have to run a shell script from my php script and while shell script is running I have to do some other tasks in my php script.To run the shell script I am using ...
2
by: ashutoshrawat | last post by:
Hi I had 2 AIX system.i want to run a shell script on one machine, which will execute another shell script on another AIX machne. the 2nd remotely executed shell script will execute a java...
7
by: Samuel A. Falvo II | last post by:
I have a shell script script.sh that launches a Java process in the background using the &-operator, like so: #!/bin/bash java ... arguments here ... & In my Python code, I want to invoke...
4
by: devi thapa | last post by:
Hi, I am executing a python script in a shell script. The python script actually returns a value. So, can I get the return value in a shell script? If yes, then help me out. Regards, Devi
4
by: balnair | last post by:
I have a shell file in location /home/transfer/check.sh I need to excecute this shell file using python script
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.