开发者

How can you tell if bash is being accessed from a remote machine?

I am trying to write out a dynamic bash开发者_如何转开发 profile for a few machines, and was wondering if there is a variable that allows .bashrc if it is being accessed remotely. I've seen a few examples using X variables, but that is irrelevant to both machines.


if [ "$SSH_CONNECTION" ]; then
  echo I am remote
else
  echo I am local
fi


When you connect via ssh, your bash process is a child of sshd ($PPID is a variable of bash's parent process - ssh that is, if you connect remotely). You can check for that:

if ps ax | grep ^$PPID'.*sshd' &> /dev/null; then  
  # do your stuff
fi

Edit: I was bored and used time to get execution times and found out that this version apparently is a couple of milliseconds faster:

if grep ^sshd: /proc/$PPID/cmdline &> /dev/null; then
  # do your stuff
fi
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜