LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 06-03-2005, 08:38 PM   #1
xconspirisist
Member
 
Registered: Dec 2002
Location: United Kingdom
Distribution: Ubuntu
Posts: 276

Rep: Reputation: 30
Obtain ip address and check for running process via Bash Script?


Is it possible to check if a process is running, dump a 'true', or a 'false' to a file, then get the current, external ip address of the router, and dump that to a file too?

Many thanks.
 
Old 06-03-2005, 09:14 PM   #2
anandt4u
LQ Newbie
 
Registered: Jun 2005
Location: Montreal
Distribution: Fedora Core 3
Posts: 29

Rep: Reputation: 15
http://howtos.linux.com/guides/abs-guide/procref1.shtml

hope its useful
 
Old 09-11-2008, 02:23 AM   #3
thehkv
LQ Newbie
 
Registered: Sep 2008
Posts: 3

Rep: Reputation: 0
Quote:
Originally Posted by anandt4u View Post
<cant post URL (first post) >
hope its useful
Link doesnt work anymore, can you please repost ??

Here's what i actually need : suppose i want my current ip address to be stored as a variable, then after some time, i store my now-current ip in some other variable, and then i want to compare these two through IF clause (i wonder if IF only compares strings and not numbers) . Is it possible ?
As you may have noticed, I'm new to linux bash, though I know windows batch programming pretty much - so if possible, please be elaborative.
 
Old 09-11-2008, 02:27 AM   #4
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 59
EDIT: ignore below. Response posted to wrong thread.

Code:
if [ $ip1 = $ip2 ] ; then
   echo SAME IP
else
   echo DIFFERENT IP
fi

Last edited by Mr. C.; 09-12-2008 at 11:31 AM.
 
Old 09-12-2008, 07:14 AM   #5
thehkv
LQ Newbie
 
Registered: Sep 2008
Posts: 3

Rep: Reputation: 0
Thanks Mr. C.
Although that wasn't what i was asking for. What I'm asking is - how do you store your IP address in those $ip1 & $ip2 variables.
 
Old 09-12-2008, 07:30 AM   #6
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
The link above was from the Advanced Bash Scripting Guide. You can find the latest version on The Linux Documentation Project, here. Regarding your specific issue, look at Command Substitution (chapter 11 in the above cited guide).
 
Old 09-12-2008, 08:58 AM   #7
keefaz
Senior Member
 
Registered: Mar 2004
Distribution: Slackware
Posts: 4,617

Rep: Reputation: 136Reputation: 136
For external IP, you could use:
Code:
#!/bin/bash

#file where to store IP
ipfile=/path/to/file

#get external IP
IP=$(curl http://www.whatismyip.com/automation/n09230945.asp)

#display IP
echo $IP

#get old IP
OLDIP=""
if [ -f "$ipfile" ]; then
   OLDIP=$(<$ipfile)
fi

#compare IPs
if [ "$IP" = "$OLDIP" ]; then
    echo "IPs match"
else
    echo "IPs don't match"
fi

#save new IP to the file
echo $IP > $ipfile

Last edited by keefaz; 09-12-2008 at 09:07 AM. Reason: forgot a "then" in if condition
 
Old 09-12-2008, 10:10 AM   #8
lipun4u
Member
 
Registered: Sep 2008
Location: Mumbai, india
Distribution: ubuntu and hp-unix
Posts: 118

Rep: Reputation: 15
ifconfig eth0 | grep "inet addr" | cut -d":" -f2 | cut -d" " -f1

you can change the interface eth0 to eth1 or pppoe, etc
 
Old 09-12-2008, 11:30 AM   #9
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 59
Quote:
Originally Posted by thehkv View Post
Thanks Mr. C.
Although that wasn't what i was asking for. What I'm asking is - how do you store your IP address in those $ip1 & $ip2 variables.
This is very peculiar. I have no idea how my post ended up in your thread. The response was meant for another question. Sorry.
 
Old 09-12-2008, 12:45 PM   #10
keefaz
Senior Member
 
Registered: Mar 2004
Distribution: Slackware
Posts: 4,617

Rep: Reputation: 136Reputation: 136
For the internal IP address, one could also use dig:
Code:
dig $HOSTNAME +short
 
Old 09-12-2008, 02:18 PM   #11
thehkv
LQ Newbie
 
Registered: Sep 2008
Posts: 3

Rep: Reputation: 0
Quote:
Originally Posted by Mr. C. View Post
This is very peculiar. I have no idea how my post ended up in your thread. The response was meant for another question. Sorry.
What's even funny is that the script you gave is exactly what I'm using - although it didn't answer just the part of my question which i wanted to know (which I now understand it never meant to).

Thanks keefaz for the whole code, with all the comments, very helpful.
I didn't have curl so I'm using lynx instead (yes, i wanted router's IP, not internal), which works equally good.

Once again, Thanks all.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Scripts to check if a process is running kt8993 Programming 3 07-09-2005 06:32 PM
Shell script ip address format check. rooch84 Linux - Software 6 08-18-2004 10:14 AM
script to check if process is dead or running rspurlock *BSD 6 04-13-2004 12:32 AM
[script] check for a running process mikshaw Linux - Software 2 01-13-2004 09:33 PM
program to check to make sure a process is running? IceNineJon Linux - Software 7 08-06-2003 03:07 PM


All times are GMT -5. The time now is 03:46 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration