Distributions
Ubuntu
Fedora
CentOS
中文资源站
网易开源镜像站
Liang
V2EX  ›  Linux

linux 如何自定义命令?

  •  
  •   Liang · Aug 7, 2014 · 5171 views
  •   You need to sign in to view this topic
    This topic created in 4377 days ago, the information mentioned may be changed or developed.
    我想搭建一台跳板机,来管理手上所有的服务器。例如
    输入conn a就ssh 1.2.3.4
    输入conn b就ssh 5.6.7.8

    请问这样的命令如何实现?
    22 replies    2014-08-08 15:44:20 +08:00
    Mutoo
        1
    Mutoo  
       Aug 7, 2014   ❤️ 1
    1) 把 ip 加到 hosts

    1.2.3.4 a
    5.6.7.8 b

    然后 ssh a / ssh b

    2) 用 alias

    alias conna="ssh 1.2.3.4"
    strak47
        2
    strak47  
       Aug 7, 2014   ❤️ 1
    Busy
        3
    Busy  
       Aug 7, 2014   ❤️ 1
    可以写个脚本
    case $num in
    1) command ;;
    2) command ;;
    ......
    esac
    clino
        4
    clino  
       Aug 7, 2014 via Android   ❤️ 1
    随便用python shell之类的写几行就出来了
    当然针对楼主ssh的场合,在 .ssh/config里配置host就行了
    Tink
        5
    Tink  
       Aug 7, 2014 via iPhone   ❤️ 1
    alias
    Liang
        6
    Liang  
    OP
       Aug 7, 2014
    @Mutoo
    @Tink

    Thx,别名应该不能实现 conn a吧?只能实现conna这样写死的别名。
    Liang
        7
    Liang  
    OP
       Aug 7, 2014
    @Havee 3q,我试试。
    Liang
        8
    Liang  
    OP
       Aug 7, 2014
    @Mutoo host的方法很灵活,谢谢
    clino
        9
    clino  
       Aug 7, 2014 via Android
    @Liang 配置 ssh host更方便,还可以配置用户名和端口
    Busy
        10
    Busy  
       Aug 7, 2014   ❤️ 3
    .ssh/config

    Host name
    IdentityFile ~/.ssh/yourkey
    HostName ip
    port port
    User remote-user

    随后直接
    ssh name
    xuxu
        11
    xuxu  
       Aug 7, 2014   ❤️ 1
    写个可以接受参数的脚本,去掉后缀ln到PATH执行路径下。
    sampeng
        12
    sampeng  
       Aug 7, 2014   ❤️ 1
    @Havee 才是真理好么。。你们还想这么多。。。
    我的sshconfig。。里面有几十个服务器了= =!
    sampeng
        13
    sampeng  
       Aug 7, 2014
    发慢了。。这种办法还能直接免密码登陆。。
    leavic
        14
    leavic  
       Aug 7, 2014
    明显alias啊
    SoloCompany
        15
    SoloCompany  
       Aug 7, 2014
    .bash_profile

    conn() {
    local host
    case "$1” in
    ‘a’)
    host=1.2.3.4
    ;;
    ‘b’)
    host=5.6.7.8
    ;;
    ‘*’)
    echo 'conn what?'
    ;;
    esac

    shift 1
    ssh $host “$@"
    }
    kodango
        16
    kodango  
       Aug 8, 2014   ❤️ 1
    将以下内容添加到~/.bash_profile文件中:

    # Auto complete ssh server defined in ~/.ssh/config
    #complete -W "$(awk '/^Host/{if ($2!="*") print $2}' ~/.ssh/config)" ssh

    # Define ssh alias for server defined in ~/.ssh/config
    for host in $(awk '/^Host/{if ($2!="*") print $2}' ~/.ssh/config); do
    alias $host="ssh $host"
    done
    然后 source ~/.bash_profile或者重新登录 shell,直接键入服务器别名,例如test,来ssh到相应的机器。

    http://kodango.com/manage-ssh-connectiions
    dingyaguang117
        17
    dingyaguang117  
       Aug 8, 2014 via iPad
    ssh-copy-id
    alias
    我现在都sshxx直接登录
    tonitech
        18
    tonitech  
       Aug 8, 2014
    你到/etc/profile里面设置
    alias conna="ssh 1.2.3.4"
    alias connb="ssh 5.6.7.8"
    caonan
        19
    caonan  
       Aug 8, 2014
    10 楼正解。
    Liang
        20
    Liang  
    OP
       Aug 8, 2014
    @Havee 已通过该方法实现!3Q

    @xuxu
    @sampeng
    @leavic
    @SoloCompany
    @kodango
    @dingyaguang117
    @tonitech
    @caonan
    3Q,很热心!
    bjzhush
        21
    bjzhush  
       Aug 8, 2014
    alias
    huangyan9188
        22
    huangyan9188  
       Aug 8, 2014
    linux 的命令本身就是跑全局的程序 这些程序一般放在/usr/bin /usr/sbin 或者是环境变指定的文件夹下面,然后输入程序名(也就是指令)全局的来执行就好了
    因此楼主只需要写一个程序放在全局路径下就ok了
    怎么实现都可以,楼上诸位大神已经给出了脚本代码
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2600 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 49ms · UTC 01:03 · PVG 09:03 · LAX 18:03 · JFK 21:03
    ♥ Do have faith in what you're doing.