From: JF Mezei on
If I access my server from another machine via telnet (or ssh), I would
like to have a script automatically obtain the IP address that
originated the telnet (or ssh) call and do certain things (such as setup
the DISPLAY variable, and add the address to my postfix config for instance.

How does one obtain the ip address from a shell script ?


aka: if my laptop is at 10.0.0.150 and telnets to the server at
10.0.0.20, I want the script running on 10.0.0.20 get "10.0.0.150" into
a shall variable that enables me to do things.

Any hints on how to achieve this ?
From: Doug Anderson on
JF Mezei <jfmezei.spamnot(a)vaxination.ca> writes:

> If I access my server from another machine via telnet (or ssh), I would
> like to have a script automatically obtain the IP address that
> originated the telnet (or ssh) call and do certain things (such as setup
> the DISPLAY variable, and add the address to my postfix config for instance.
>
> How does one obtain the ip address from a shell script ?
>
>
> aka: if my laptop is at 10.0.0.150 and telnets to the server at
> 10.0.0.20, I want the script running on 10.0.0.20 get "10.0.0.150" into
> a shall variable that enables me to do things.
>
> Any hints on how to achieve this ?

I don't know the answer to your question, but

1) if you use

ssh -X

it should be unnecessary to think about your DISPLAY variable.

2) see the commands w, who (with the -l option), and last (I don't
know what logfile last reads on OS X - on linux it typically reads
/var/log/wtmp or something like that).

From: Jolly Roger on
In article <4c01370f$0$2114$c3e8da3(a)news.astraweb.com>,
JF Mezei <jfmezei.spamnot(a)vaxination.ca> wrote:

> If I access my server from another machine via telnet (or ssh), I would
> like to have a script automatically obtain the IP address that
> originated the telnet (or ssh) call and do certain things (such as setup
> the DISPLAY variable, and add the address to my postfix config for instance.
>
> How does one obtain the ip address from a shell script ?
>
>
> aka: if my laptop is at 10.0.0.150 and telnets to the server at
> 10.0.0.20, I want the script running on 10.0.0.20 get "10.0.0.150" into
> a shall variable that enables me to do things.
>
> Any hints on how to achieve this ?

Here's a Perl script you can easily modify to do what you want. The
script looks at the output of the 'ifconfig' tool and reports the IP
address (and other info) for the first active network interface it
encounters.

Watch for line wraps introduced by your news client:

#!/usr/bin/perl

use strict;

my $ifconfig = '/sbin/ifconfig';
my $found = 0;
my $interface;
my $mac;
my $ip;
my $netmask;
my $broadcast;
my $status;

# get airport interface status

my @status = `$ifconfig`;

# examine each line in turn

for (my $l = 0; $l < scalar @status; $l++)
{
my $line = $status[$l];

# get interface name

if ($line =~ m/^(\w+)\:\s+flags/)
{
$interface = $1;
}

# get MAC address

if ($line =~ m/^\s+ether\s+(\S+)/)
{
$mac = $1;
}

# get IP info

if ($line =~
m/^\s+inet\s+(\S+)\s+netmask\s+(\S+)\s+broadcast\s+(\S+)$/)
{
$ip = $1;
$netmask = $2;
$broadcast = $3;
}

# get status: active

if ($line =~ m/^\s+status\:\s+(active)$/)
{
$found = 1;
last;
}
}

if ($found)
{
print "Found active interface:\n";
print " interface: $interface\n";
print "mac address: $mac\n";
print " ip address: $ip\n";
print " net mask: $netmask\n";
print " broadcast: $broadcast\n";
}
else
{
print STDERR "ERROR: No active interface was found.\n";
}

exit;

--
Send responses to the relevant news group rather than email to me.
E-mail sent to this address may be devoured by my very hungry SPAM
filter. Due to Google's refusal to prevent spammers from posting
messages through their servers, I often ignore posts from Google
Groups. Use a real news client if you want me to see your posts.

JR
From: Ian Gregory on
On 2010-05-29, JF Mezei <jfmezei.spamnot(a)vaxination.ca> wrote:
> If I access my server from another machine via telnet (or ssh), I would
> like to have a script automatically obtain the IP address that
> originated the telnet (or ssh) call and do certain things (such as setup
> the DISPLAY variable, and add the address to my postfix config for instance.
>
> How does one obtain the ip address from a shell script ?
>
>
> aka: if my laptop is at 10.0.0.150 and telnets to the server at
> 10.0.0.20, I want the script running on 10.0.0.20 get "10.0.0.150" into
> a shall variable that enables me to do things.

For telnet it is a bit tricky but for ssh it is trivial. The ssh daemon
running on the server should set an environment variable called
SSH_CLIENT which contains the IP address of the laptop (as well as the
remote and local TCP port numbers. Check it by typing "echo $SSH_CLIENT"
once you are connected to the server.

If you want to do it for a telnet connection take a look at:

<http://unixwiz.net/tools/whoamip.html>

Ian

--
Ian Gregory
http://www.zenatode.org.uk/
From: VAXman- on
In article <4c01370f$0$2114$c3e8da3(a)news.astraweb.com>, JF Mezei <jfmezei.spamnot(a)vaxination.ca> writes:
>If I access my server from another machine via telnet (or ssh), I would
>like to have a script automatically obtain the IP address that
>originated the telnet (or ssh) call and do certain things (such as setup
>the DISPLAY variable, and add the address to my postfix config for instance.
>
>How does one obtain the ip address from a shell script ?
>
>
>aka: if my laptop is at 10.0.0.150 and telnets to the server at
>10.0.0.20, I want the script running on 10.0.0.20 get "10.0.0.150" into
>a shall variable that enables me to do things.
>
>Any hints on how to achieve this ?

Why not log in and then type:

% printenv

This should shed some light.

--
VAXman- A Bored Certified VMS Kernel Mode Hacker VAXman(at)TMESIS(dot)ORG

And ever since I was a boy I never felt that I belonged Like everything they
did to me Was an experiment to see How I would cope with the illusion In which
direction would I jump Would I do it all the same As the actors in the game Or
would I spit it back at them And not get caught up in their rules And live
according to my own And not be used To find the fundamental truths It was
going to take some time Thirty five summers down the line The wisdom of each
passing year Seems to serve only to confuse Seems to serve only to confuse