Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
detecting when I'm logged in remotely?
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Other Things Gentoo
View previous topic :: View next topic  
Author Message
Tub
n00b
n00b


Joined: 08 Feb 2005
Posts: 21

PostPosted: Wed Jun 13, 2007 10:14 pm    Post subject: detecting when I'm logged in remotely? Reply with quote

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
View user's profile Send private message
javeree_work
Tux's lil' helper
Tux's lil' helper


Joined: 13 Apr 2005
Posts: 84

PostPosted: Thu Jun 14, 2007 12:07 pm    Post subject: Reply with quote

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
View user's profile Send private message
Tub
n00b
n00b


Joined: 08 Feb 2005
Posts: 21

PostPosted: Thu Jun 14, 2007 3:27 pm    Post subject: Reply with quote

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
View user's profile Send private message
Tub
n00b
n00b


Joined: 08 Feb 2005
Posts: 21

PostPosted: Thu Jun 14, 2007 10:46 pm    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Other Things Gentoo All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum