View previous topic :: View next topic |
Author |
Message |
Tub n00b
Joined: 08 Feb 2005 Posts: 21
|
Posted: Wed Jun 13, 2007 10:14 pm Post subject: detecting when I'm logged in remotely? |
|
|
Hi,
I like to have a prompt that'll highlight anything I should be aware of. Let's say I'm sitting at computer 'local' and there's a server 'server' I can ssh to. I managed to set up my bashrcs to do the following:
tub@local: (everything's fine, no colors)
root@local: (root! be careful)
tub@server: (careful, this is not the machine I'm sitting at!)
root@server: this is wrong. the hostname should be red.
I'm currently checking for the presence of environment variables ($SSH_CLIENT / $SSH_CONNECTION) to determine if this is a local login. But when I su - on the remote host, the environment variables are removed, and my prompt doesn't warn me any more.
Of course I could use su instead of su - (or su -p or whatever), but that's not a good idea.
Another way to check remote logins would be to check if one of the parent processes of the shell is called 'sshd'. I can get that information from pstree -h, but that's not good for automatic parsing at all. Neither 'man ps' nor browsing /proc brought me closer to a solution.
Anyone with a better idea? _________________ m00 |
|
Back to top |
|
|
javeree_work Tux's lil' helper
Joined: 13 Apr 2005 Posts: 84
|
Posted: Thu Jun 14, 2007 12:07 pm Post subject: |
|
|
Just from memory:
I believe the command 'tty' returns /dev/ttyx if you are working locally and dev/ptyx (or some other acronym meaning 'pseudoterminal') if you are working remote.
Also check the command 'fgconsole' |
|
Back to top |
|
|
Tub n00b
Joined: 08 Feb 2005 Posts: 21
|
Posted: Thu Jun 14, 2007 3:27 pm Post subject: |
|
|
thank you.
Unfortunately xterm/aterm/konsole (and probably every other x11-terminal) use virtual ttys even when working locally. /dev/tty/x is only used on the framebuffer-consoles (Strg-Alt-F1 to F6). Since I'm mostly using aterm, sometimes putty and very rarely a framebuffer console, that check won't help. _________________ m00 |
|
Back to top |
|
|
Tub n00b
Joined: 08 Feb 2005 Posts: 21
|
Posted: Thu Jun 14, 2007 10:46 pm Post subject: |
|
|
browsing the pstree source code revealed that the PID of the parent process is available via /proc/$PID/stats .. so I can do a simple recursive lookup.
Code: |
PS1=" ... "
PS1_P=$PPID
while [ $PS1_P -gt 0 ] ; do
if [ $(awk '{print $2}' /proc/${PS1_P}/stat) = '(sshd)' ] ; then
PS1_P=0
PS1="${PS1}\e[31m"
else
PS1_P=$(awk '{print $4}' /proc/${PS1_P}/stat)
fi
done
unset PS1_P
PS1="${PS1}\H ....."
|
problem solved. _________________ m00 |
|
Back to top |
|
|
|