V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
usdc
0.01D
V2EX  ›  问与答

问一下有简化命令的库吗

  •  
  •   usdc · Aug 5, 2018 · 2710 views
    This topic created in 2822 days ago, the information mentioned may be changed or developed.

    类似 alias 但是要能配参数的那种

    10 replies    2018-08-07 03:38:04 +08:00
    usdc
        1
    usdc  
    OP
       Aug 5, 2018
    比如说我 ssh 1.1.1.1@root -p 22

    然后用别名登录 xxx some 就直用上面的命令

    然后 xxx another 就 ssh 另外一个服务器
    iwannarun
        2
    iwannarun  
       Aug 5, 2018
    搜 ssh 免密登录
    suixn
        3
    suixn  
       Aug 5, 2018 via Android
    配置下.ssh/config
    jason19659
        4
    jason19659  
       Aug 5, 2018
    写个 shell ./xxx some
    iwannarun
        5
    iwannarun  
       Aug 5, 2018
    本机生成秘钥对,scp 拷贝到目标服务器后,
    cat ~/id_rsa.pub >> ~/.ssh/authorized_keys

    回到本机用 key 登入,然后如下设置下本机的 .ssh/config
    Host *
    UseKeychain yes
    AddKeysToAgent yes
    IdentityFile ~/.ssh/id_rsa

    Host some
    HostName [IP address]
    Port 22
    User root
    ServerAliveInterval 10

    最后效果就是:本机上执行 ssh some 即可登录服务器
    hearfish
        6
    hearfish  
       Aug 5, 2018   ❤️ 1
    在~/.bash_profile 里定义一个函数就行了

    xxx() {
    if [ -n "$1" ]
    then
    ssh -p22 root@$1
    fi
    }
    Howlaind
        7
    Howlaind  
       Aug 5, 2018
    ssh 直接写 .ssh/config 就好,其他的命令写一个 shell 也不麻烦。如果真连写 shell 也嫌麻烦,那干脆可以 alias 个长点的名字,像 alias ssh-some="ssh 1.1.1.1@root -p 22";alias ssh-another="ssh 2.2.2.2@root -p 22"
    usdc
        8
    usdc  
    OP
       Aug 5, 2018
    @Howlaind 主要是记不住 ip
    xierch
        9
    xierch  
       Aug 5, 2018
    以前我也是给 ssh 加 alias 的
    后来发现 ctrl-r 很方便就不加 alias 了

    类似的还有输入命令开头然后上下方向键搜索:
    https://askubuntu.com/questions/59846/bash-history-search-partial-up-arrow#59855
    Arnie97
        10
    Arnie97  
       Aug 7, 2018 via Android
    .ssh/config 和 /etc/hosts 二选一
    (实际上我用的二级域名记录 IP,因为懒得同步配置…
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3200 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 65ms · UTC 13:39 · PVG 21:39 · LAX 06:39 · JFK 09:39
    ♥ Do have faith in what you're doing.