zsh-users
 help / color / mirror / code / Atom feed
* Differrent prompt for remote machines
@ 2001-03-30  9:42 Dominik Vogt
  2001-03-30  9:59 ` Andrej Borsenkow
                   ` (3 more replies)
  0 siblings, 4 replies; 12+ messages in thread
From: Dominik Vogt @ 2001-03-30  9:42 UTC (permalink / raw)
  To: zsh-users

I want to solve the following problem:

  I'm often logged in to different machines that all share the
  same zsh configuration files (via ssh or telnet).  But I
  frequently forget on which machine I am, mainly because either
  I have the machine name in my promt for all machines or for no
  machine at all.  What I'd like to have is this:

    For the machine at which I logged in:

      <username> ...

    and

      <username>@<machine> ...

    for remote logins (and possibly the machine name in bright
    red for our server and in a different colour for other remote
    machines).

  Basically it breaks down to detecting if the shell runs on a
  local terminal or through a network connection.

Bye

Dominik ^_^  ^_^

--
Dominik Vogt, email: d.vogt@lifebits.de
LifeBits Aktiengesellschaft, Albrechtstr. 9, D-72072 Tuebingen
fon: ++49 (0) 7071/7965-0, fax: ++49 (0) 7071/7965-20


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Differrent prompt for remote machines
  2001-03-30  9:42 Differrent prompt for remote machines Dominik Vogt
@ 2001-03-30  9:59 ` Andrej Borsenkow
  2001-03-30 10:48   ` Dominik Vogt
  2001-03-31 11:32 ` Zefram
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 12+ messages in thread
From: Andrej Borsenkow @ 2001-03-30  9:59 UTC (permalink / raw)
  To: Dominik Vogt; +Cc: zsh-users

On Fri, 30 Mar 2001, Dominik Vogt wrote:

> I want to solve the following problem:
>
>   I'm often logged in to different machines that all share the
>   same zsh configuration files (via ssh or telnet).  But I
>   frequently forget on which machine I am, mainly because either
>   I have the machine name in my promt for all machines or for no
>   machine at all.  What I'd like to have is this:
>
>     For the machine at which I logged in:
>
>       <username> ...
>
>     and
>
>       <username>@<machine> ...
>
>     for remote logins (and possibly the machine name in bright
>     red for our server and in a different colour for other remote
>     machines).
>
>   Basically it breaks down to detecting if the shell runs on a
>   local terminal or through a network connection.
>

One possibility is to use finger (is it available on all systems?)
Something like

finger -f
root     Super-User            console        3 Tue 17:02
root     Super-User            pts/1            Fri 09:00  foo.bar.com

then check for current line and see if the last field is empty. Solaris
supports who -m:

# who -m
root       pts/1        Mar 30 09:00    (foo.bar.com)

that basically gives the same information, but it depends on system, of
course.

You could check for pty, but it won't catch the case when you run under
X11.



-andrej


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Differrent prompt for remote machines
  2001-03-30  9:59 ` Andrej Borsenkow
@ 2001-03-30 10:48   ` Dominik Vogt
  2001-03-30 15:55     ` Bart Schaefer
  0 siblings, 1 reply; 12+ messages in thread
From: Dominik Vogt @ 2001-03-30 10:48 UTC (permalink / raw)
  To: Andrej Borsenkow, zsh-users

On Fri, Mar 30, 2001 at 01:59:33PM +0400, Andrej Borsenkow wrote:
> On Fri, 30 Mar 2001, Dominik Vogt wrote:
> 
> > I want to solve the following problem:
> >
> >   I'm often logged in to different machines that all share the
> >   same zsh configuration files (via ssh or telnet).  But I
> >   frequently forget on which machine I am, mainly because either
> >   I have the machine name in my promt for all machines or for no
> >   machine at all.  What I'd like to have is this:
> >
> >     For the machine at which I logged in:
> >
> >       <username> ...
> >
> >     and
> >
> >       <username>@<machine> ...
> >
> >     for remote logins (and possibly the machine name in bright
> >     red for our server and in a different colour for other remote
> >     machines).
> >
> >   Basically it breaks down to detecting if the shell runs on a
> >   local terminal or through a network connection.
> >
> 
> One possibility is to use finger (is it available on all systems?)
> Something like
> 
> finger -f
> root     Super-User            console        3 Tue 17:02
> root     Super-User            pts/1            Fri 09:00  foo.bar.com
> 
> then check for current line and see if the last field is empty. Solaris
> supports who -m:
> 
> # who -m
> root       pts/1        Mar 30 09:00    (foo.bar.com)
> 
> that basically gives the same information, but it depends on system, of
> course.
> 
> You could check for pty, but it won't catch the case when you run under
> X11.

Thanks, I got it working with 'who -m | cut -d "!" -f 1'.  Now
there's only the problem of setting the colours, but I think I'll
stick to underline/standout/bold mode in the prompt to keep this
portable.

Bye

Dominik ^_^  ^_^

--
Dominik Vogt, email: d.vogt@lifebits.de
LifeBits Aktiengesellschaft, Albrechtstr. 9, D-72072 Tuebingen
fon: ++49 (0) 7071/7965-0, fax: ++49 (0) 7071/7965-20


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Differrent prompt for remote machines
  2001-03-30 10:48   ` Dominik Vogt
@ 2001-03-30 15:55     ` Bart Schaefer
  0 siblings, 0 replies; 12+ messages in thread
From: Bart Schaefer @ 2001-03-30 15:55 UTC (permalink / raw)
  To: zsh-users

On Mar 30, 12:48pm, Dominik Vogt wrote:
}
} On Fri, Mar 30, 2001 at 01:59:33PM +0400, Andrej Borsenkow wrote:
} > On Fri, 30 Mar 2001, Dominik Vogt wrote:
} > 
} > >   Basically it breaks down to detecting if the shell runs on a
} > >   local terminal or through a network connection.
} > 
} > One possibility is to use finger (is it available on all systems?)
} > Solaris supports who -m

There's also `last -adn 1 $LOGNAME' on linux, which will attempt to reverse
map an IP address if the remote host name is not stored in wtmp.

} Thanks, I got it working with 'who -m | cut -d "!" -f 1'.

Quicker is ${"$(who -m)"%%\!*}

-- 
Bart Schaefer                                 Brass Lantern Enterprises
http://www.well.com/user/barts              http://www.brasslantern.com

Zsh: http://www.zsh.org | PHPerl Project: http://phperl.sourceforge.net   


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Differrent prompt for remote machines
  2001-03-30  9:42 Differrent prompt for remote machines Dominik Vogt
  2001-03-30  9:59 ` Andrej Borsenkow
@ 2001-03-31 11:32 ` Zefram
  2001-03-31 18:26   ` Stephane Bortzmeyer
  2001-03-31 19:14   ` Bart Schaefer
  2001-03-31 18:26 ` Stephane Bortzmeyer
  2001-04-02 20:46 ` Matt Pharr
  3 siblings, 2 replies; 12+ messages in thread
From: Zefram @ 2001-03-31 11:32 UTC (permalink / raw)
  To: d.vogt; +Cc: zsh-users

Dominik Vogt wrote:
>  Basically it breaks down to detecting if the shell runs on a
>  local terminal or through a network connection.

if [[ $TTY == /dev/pts/* || $TTY == /dev/tty[a-z]* ]]; then
	PS1='%n@%m> '
else
	PS1='%n> '
fi

-zefram


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Differrent prompt for remote machines
  2001-03-30  9:42 Differrent prompt for remote machines Dominik Vogt
  2001-03-30  9:59 ` Andrej Borsenkow
  2001-03-31 11:32 ` Zefram
@ 2001-03-31 18:26 ` Stephane Bortzmeyer
  2001-04-04 17:21   ` Dominik Vogt
  2001-04-02 20:46 ` Matt Pharr
  3 siblings, 1 reply; 12+ messages in thread
From: Stephane Bortzmeyer @ 2001-03-31 18:26 UTC (permalink / raw)
  To: d.vogt; +Cc: zsh-users

On Friday 30 March 2001, at 11 h 42, the keyboard of Dominik Vogt 
<d.vogt@lifebits.de> wrote:

>     For the machine at which I logged in:
> 
>       <username> ...
> 
>     and
> 
>       <username>@<machine> ...
> 
>     for remote logins 

If all the remote connections use SSH (which is highly recommended anyway), 
here is what I use:

if [ -z "$SSH_CLIENT" ]; then
    export PROMPT='%# '
else
    export PROMPT='%m: %# '
fi
export RPROMPT='%4~'





^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Differrent prompt for remote machines
  2001-03-31 11:32 ` Zefram
@ 2001-03-31 18:26   ` Stephane Bortzmeyer
  2001-03-31 19:14   ` Bart Schaefer
  1 sibling, 0 replies; 12+ messages in thread
From: Stephane Bortzmeyer @ 2001-03-31 18:26 UTC (permalink / raw)
  To: Zefram; +Cc: d.vogt, zsh-users

On Saturday 31 March 2001, at 12 h 32, the keyboard of Zefram <zefram@fysh.org>
 wrote:

> if [[ $TTY == /dev/pts/* 

TTY is a pts both for remote sessions and for X11 sessions.



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Differrent prompt for remote machines
  2001-03-31 11:32 ` Zefram
  2001-03-31 18:26   ` Stephane Bortzmeyer
@ 2001-03-31 19:14   ` Bart Schaefer
  2001-04-01  3:53     ` E. Jay Berkenbilt
  1 sibling, 1 reply; 12+ messages in thread
From: Bart Schaefer @ 2001-03-31 19:14 UTC (permalink / raw)
  To: zsh-users

On Mar 31, 12:32pm, Zefram wrote:
} Subject: Re: Differrent prompt for remote machines
}
} Dominik Vogt wrote:
} >  Basically it breaks down to detecting if the shell runs on a
} >  local terminal or through a network connection.
} 
} if [[ $TTY == /dev/pts/* || $TTY == /dev/tty[a-z]* ]]; then
} 	PS1='%n@%m> '
} else
} 	PS1='%n> '
} fi

That doesn't work when X11 is involved, because all X terminal emulators
appear to be running "through a network connection" even when they're on
the local machine.

As it turns out, I've been trying to solve a similar problem for another
reason (I'll resist the urge to bore you with details) and came up with
the solution below.

One remaining issue is that if you're on a university timesharing system
or the like, you're probably on /dev/tty[a-z]* even if you're just using
an ancient vt100 in the lab; but even then it should be possible to come
up with a test for "is this shell on a pseudo-terminal".

The `|| ps -lp' is for systems that insist on a leading `-' in the `ps'
options (and therefore probably don't have `w').  Maybe that's overkill.

It should be obvious that this is meant to be a shell function.

---- 8< ---- snip ---- 8< ----
emulate -L zsh
local h ps ppid=$PPID
while (( ppid > 1 ))
do
    ps=("${(f)$(ps lwp$ppid 2>/dev/null || ps -lp$ppid)}")
    h=($=ps[1])
    ps=($=ps[2])
    case "$ps" in
    *sshd*)
	# Shell running from ssh, probably not the local machine
	PS1='%n@%m> '
	break
	;;
    *(xterm|rxvt|dtterm|eterm|gnoterm|emacs)*)
	# Shell running from an emulator, check for local displays
	if [[ -n $SSH_CLIENT ]]
	then
	    # Emulator started from ssh, probably port-forwarding
	    PS1='%n@%m> '
	    break
	elif [[ $DISPLAY = ($HOST*|):* && ${HOSTDISPLAY:-$HOST} = $HOST* ]]
	then
	    # Shell running on a local display (though could be VNC)
	    PS1='%n> '
	    break
	elif [[ -n $DISPLAY ]]
	then
	    # Shell running on a remote display
	    PS1='%n@%m> '
	    break
	else
	    # Probably running inside a text-mode emacs
	fi
	;;
    *login*)
	# Shell running on the local console, or from rlogin or telnet
	if [[ $TTY == /dev/pts/* || $TTY == /dev/tty[A-Za-z]* ]]
	then
	    # Shell running from rlogin or telnet
	    PS1='%n@%m> '
	else
	    # Shell running on local console
	    PS1='%n> '
	fi
	break
	;;
    *)
	# Shell running from su or from some other shell or program
	if [[ -n $SSH_CLIENT ]]
	then
	    # Some ancestor is ssh, probably not local
	    PS1='%n@%m> '
	    break
	fi
	;;
    esac
    # Not enough information yet, climb the process tree
    ppid=${ps[$h[(i)PPID]]:-1}
done
---- 8< ---- snip ---- 8< ----

-- 
Bart Schaefer                                 Brass Lantern Enterprises
http://www.well.com/user/barts              http://www.brasslantern.com

Zsh: http://www.zsh.org | PHPerl Project: http://phperl.sourceforge.net   


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Differrent prompt for remote machines
  2001-03-31 19:14   ` Bart Schaefer
@ 2001-04-01  3:53     ` E. Jay Berkenbilt
  2001-04-01  4:41       ` Bart Schaefer
  0 siblings, 1 reply; 12+ messages in thread
From: E. Jay Berkenbilt @ 2001-04-01  3:53 UTC (permalink / raw)
  To: schaefer; +Cc: zsh-users

[-- Attachment #1: Type: text/plain, Size: 639 bytes --]

I determine whether I'm remote using this C program I wrote.  I've
tested it only linux.  It uses the fact that most well-behaved remote
login daemons make sure that your tty's utmp entry contains the host
from which you logged in if you are logged in remotely.  This program
prints "local" if you're local, "remote" if you remote, and a few
other things if it can't tell for various reasons.  I've found it to
accurately detect remote logins with ssh, telnet, and (Kerberos)
rlogin and to accurately detect local logins with or without X.  If
you remote login back to your local host (for some reason) it still
considers you to be local.

[-- Attachment #2: utmp_checkremote.c --]
[-- Type: application/octet-stream, Size: 2333 bytes --]

/*
 * $Id: utmp_checkremote.c,v 1.1 2000/09/05 21:41:27 ejb Exp $
 * $Source: /home/ejb/source/util/login/RCS/utmp_checkremote.c,v $
 * $Author: ejb $
 *
 */

#ifndef NO_RCS_HDRS
/* Define a static function and call it.  No warnings this way. */
static void rcsid(char *s)
{rcsid("@(#)$Id: utmp_checkremote.c,v 1.1 2000/09/05 21:41:27 ejb Exp $");s=s;}
#endif /* ! NO_RCS_HDRS */

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <utmp.h>
#include <netdb.h>
#include <errno.h>

#define UTL_NOTTY "notty"
#define UTL_NOTFOUND "notfound"
#define UTL_LOCAL "local"
#define UTL_REMOTE "remote"
#define UTL_UNKNOWN "unknown"

static char* whoami = 0;

static void deal_with_host(char* host)
{
    char* result = UTL_UNKNOWN;
    if (strlen(host) == 0)
    {
	result = UTL_LOCAL;
    }
    else
    {
	char thishost[UT_HOSTSIZE + 1];
	char otherhost[UT_HOSTSIZE + 1];
	struct hostent* hp = 0;

	thishost[UT_HOSTSIZE] = '\0';
	otherhost[UT_HOSTSIZE] = '\0';
	if (gethostname(thishost, UT_HOSTSIZE) == -1)
	{
	    fprintf(stderr, "%s: gethostname failed: %s\n",
		    whoami, strerror(errno));
	    exit(2);
	}
	if ((hp = gethostbyname(thishost)) != 0)
	{
	    strncpy(thishost, hp->h_name, UT_HOSTSIZE);
	    if ((hp = gethostbyname(host)) != 0)
	    {
		strncpy(otherhost, hp->h_name, UT_HOSTSIZE);
		if (strcmp(thishost, otherhost) == 0)
		{
		    result = UTL_LOCAL;
		}
		else
		{
		    result = UTL_REMOTE;
		}
	    }
	}
    }

    printf("%s\n", result);
}

int main(int argc, char* argv[])
{
    struct utmp *uptr = 0;
    char* tty = 0;

    if ((whoami = strrchr(argv[0], '/')) == NULL)
    {
	whoami = argv[0];
    }
    else
    {
	++whoami;
    }

    tty = ttyname(0);
    if (tty == 0)
    {
	printf("%s\n", UTL_NOTTY);
	exit(0);
    }
    else if (strncmp(tty, "/dev/", 5) != 0)
    {
	fprintf(stderr,
		"%s: ttyname() returned something that didn't start with /dev!",
		whoami);
	exit(2);
    }
    tty += 5;

    setutent();
    while ((uptr = getutent())!=NULL) {
	if (!uptr->ut_name[0])
	    continue;
#ifdef USER_PROCESS
	if (uptr->ut_type != USER_PROCESS) continue;
#endif
	if (uptr->ut_host && uptr->ut_line &&
	    (strcmp(uptr->ut_line, tty) == 0))
	{
	    deal_with_host(uptr->ut_host);
	    exit(0);
	}
    }
    endutent();

    printf("%s\n", UTL_NOTFOUND);
    return 0;
}

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Differrent prompt for remote machines
  2001-04-01  3:53     ` E. Jay Berkenbilt
@ 2001-04-01  4:41       ` Bart Schaefer
  0 siblings, 0 replies; 12+ messages in thread
From: Bart Schaefer @ 2001-04-01  4:41 UTC (permalink / raw)
  To: E. Jay Berkenbilt; +Cc: zsh-users

On Mar 31, 10:53pm, E. Jay Berkenbilt wrote:
} 
} I determine whether I'm remote using this C program I wrote.  I've
} tested it only linux.  It uses the fact that most well-behaved remote
} login daemons make sure that your tty's utmp entry contains the host
} from which you logged in if you are logged in remotely.

This is essentially the same as [[ "${$(who am i)%%!*}" = $HOST ]].  It's
a reasonable approach, but it doesn't work when `XTerm*UtmpInhibit: true',
nor when e.g. `xon remotehost xterm' was used, nor when ssh is performing
X11 port-forwarding.  Unless I'm missing something?

(Where "doesn't work" means that those three cases will report unknown,
local, and local, respectively, I believe.)

-- 
Bart Schaefer                                 Brass Lantern Enterprises
http://www.well.com/user/barts              http://www.brasslantern.com

Zsh: http://www.zsh.org | PHPerl Project: http://phperl.sourceforge.net   


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Differrent prompt for remote machines
  2001-03-30  9:42 Differrent prompt for remote machines Dominik Vogt
                   ` (2 preceding siblings ...)
  2001-03-31 18:26 ` Stephane Bortzmeyer
@ 2001-04-02 20:46 ` Matt Pharr
  3 siblings, 0 replies; 12+ messages in thread
From: Matt Pharr @ 2001-04-02 20:46 UTC (permalink / raw)
  To: zsh-users


Dominik Vogt <d.vogt@lifebits.de> writes:
>   I'm often logged in to different machines that all share the
>   same zsh configuration files (via ssh or telnet).  But I
>   frequently forget on which machine I am, mainly because either
>   I have the machine name in my promt for all machines or for no
>   machine at all.  What I'd like to have is this:
> 
>     For the machine at which I logged in:
> 
>       <username> ...
> 
>     and
> 
>       <username>@<machine> ...

I've got the following, which seems to catch that well in my setup, at
least.

if [[ $SSH_CLIENT = *.* || $REMOTEHOST = *.* ]] then
	PROMPT='<%m> %B%#%b '
else
   	PROMPT='%B%#%b '
fi

-matt

-- 
Matt Pharr    |    Exluna, Inc.    |    <URL:http://graphics.stanford.edu/~mmp>
===============================================================================
In a cruel and evil world, being cynical can allow you to get some
entertainment out of it. --Daniel Waters


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Differrent prompt for remote machines
  2001-03-31 18:26 ` Stephane Bortzmeyer
@ 2001-04-04 17:21   ` Dominik Vogt
  0 siblings, 0 replies; 12+ messages in thread
From: Dominik Vogt @ 2001-04-04 17:21 UTC (permalink / raw)
  To: zsh-users

On Sat, Mar 31, 2001 at 08:26:24PM +0200, Stephane Bortzmeyer wrote:
> On Friday 30 March 2001, at 11 h 42, the keyboard of Dominik Vogt 
> <d.vogt@lifebits.de> wrote:
> 
> >     For the machine at which I logged in:
> > 
> >       <username> ...
> > 
> >     and
> > 
> >       <username>@<machine> ...
> > 
> >     for remote logins 
> 
> If all the remote connections use SSH (which is highly recommended anyway), 
> here is what I use:
> 
> if [ -z "$SSH_CLIENT" ]; then
>     export PROMPT='%# '
> else
>     export PROMPT='%m: %# '
> fi
> export RPROMPT='%4~'

Thanks for all the replies.  Currently I use the 'who -m' but I'll
possibly revert to using something from the ssh environment.

Bye

Dominik ^_^  ^_^

--
Dominik Vogt, dominik.vogt@gmx.de
Reply-To: dominik.vogt@gmx.de


^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2001-04-04 17:22 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-03-30  9:42 Differrent prompt for remote machines Dominik Vogt
2001-03-30  9:59 ` Andrej Borsenkow
2001-03-30 10:48   ` Dominik Vogt
2001-03-30 15:55     ` Bart Schaefer
2001-03-31 11:32 ` Zefram
2001-03-31 18:26   ` Stephane Bortzmeyer
2001-03-31 19:14   ` Bart Schaefer
2001-04-01  3:53     ` E. Jay Berkenbilt
2001-04-01  4:41       ` Bart Schaefer
2001-03-31 18:26 ` Stephane Bortzmeyer
2001-04-04 17:21   ` Dominik Vogt
2001-04-02 20:46 ` Matt Pharr

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).