From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 14532 invoked from network); 1 Apr 2001 03:54:14 -0000 Received: from sunsite.dk (130.225.51.30) by ns1.primenet.com.au with SMTP; 1 Apr 2001 03:54:14 -0000 Received: (qmail 8926 invoked by alias); 1 Apr 2001 03:54:00 -0000 Mailing-List: contact zsh-users-help@sunsite.dk; run by ezmlm Precedence: bulk X-No-Archive: yes X-Seq: 3785 Received: (qmail 8909 invoked from network); 1 Apr 2001 03:53:59 -0000 Date: Sat, 31 Mar 2001 22:53:57 -0500 Message-Id: <200104010353.f313rvu04868@soup.in.ql.org> From: "E. Jay Berkenbilt" To: schaefer@candle.brasslantern.com CC: zsh-users@sunsite.dk In-reply-to: <1010331191449.ZM8000@candle.brasslantern.com> (schaefer@candle.brasslantern.com) Subject: Re: Differrent prompt for remote machines References: <1010331191449.ZM8000@candle.brasslantern.com> MIME-Version: 1.0 (qmime) Content-Type: multipart/mixed; boundary="qmime=_-20010331-225145-1275-72159-" --qmime=_-20010331-225145-1275-72159- Content-Type: text/plain 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. --qmime=_-20010331-225145-1275-72159- Content-Type: application/octet-stream Content-Disposition: attachment; filename="utmp_checkremote.c" Content-Transfer-Encoding: 7bit /* * $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 #include #include #include #include #include #include #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; } --qmime=_-20010331-225145-1275-72159---