在 Shell 脚本中如何判断当前是 ssh 登陆,还是普通终端登陆 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Distributions
Ubuntu
Fedora
CentOS
中文资源站
网易开源镜像站
naijnehc
V2EX    Linux

在 Shell 脚本中如何判断当前是 ssh 登陆,还是普通终端登陆

  •  
  •   naijnehc 2019-02-22 20:51:19 +08:00 4492 次点击
    这是一个创建于 2471 天前的主题,其中的信息可能已经有所发展或是发生改变。

    想到这个需求

    9 条回复    2019-08-22 10:26:37 +08:00
    Dragonish3600
        1
    Dragonish3600  
       2019-02-22 20:56:49 +08:00
    last 一下就好了啊
    远程登录有 IP 显示
    pkokp8
        2
    pkokp8  
       2019-02-22 20:57:05 +08:00 via Android
    export | grep ssh -i
    xupefei
        3
    xupefei  
       2019-02-22 21:01:04 +08:00
    who am i 判断里面有没有 IP 地址。
    naijnehc
        4
    naijnehc  
    OP
       2019-02-22 21:23:24 +08:00
    感谢各位
    Nitroethane
        5
    Nitroethane  
       2019-02-22 21:24:36 +08:00
    从 macOS ssh 到 Arch Linux 上:
    # env | grep SSH
    SSH_COnNECTION=192.168.50.45 61360 192.168.50.84 22

    从 macOS ssh 到 CentOS 7 上:
    # env | grep SSH
    SSH_CLIENT=27.xxx.xx.99 43735 29089
    SSH_TTY=/dev/pts/0
    SSH_COnNECTION=27.xxx.xx.99 43735 172.21.0.12 29089
    wdlth
        6
    wdlth  
       2019-02-22 21:31:38 +08:00
    判断 tty
    codehz
        7
    codehz  
       2019-02-23 12:35:42 +08:00 via Android
    @wdlth 很多图形界面终端也是 pty
    maoyipeng
        8
    maoyipeng  
       2019-02-23 13:25:09 +08:00
    环境变量
    CooperQ
        9
    CooperQ  
       2019-08-22 10:26:37 +08:00 via iPhone
    正好碰到这个问题,直接 po 代码
    export |grep SSH_TTY -i >/dev/null
    RTE=$?
    if [ “$RTE ” !=“ 0 ” ]; then
    # if not login by ssh
    fi
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2710 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 29ms UTC 07:58 PVG 15:58 LAX 23:58 JFK 02:58
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86