V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Distributions
Ubuntu
Fedora
CentOS
中文资源站
网易开源镜像站
Liang
V2EX  ›  Linux

linux 如何自定义命令?

  •  
  •   Liang · 2014-08-07 20:42:34 +08:00 · 3931 次点击
    这是一个创建于 3551 天前的主题,其中的信息可能已经有所发展或是发生改变。
    我想搭建一台跳板机,来管理手上所有的服务器。例如
    输入conn a就ssh 1.2.3.4
    输入conn b就ssh 5.6.7.8

    请问这样的命令如何实现?
    22 条回复    2014-08-08 15:44:20 +08:00
    Mutoo
        1
    Mutoo  
       2014-08-07 20:45:30 +08:00   ❤️ 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  
       2014-08-07 20:51:04 +08:00   ❤️ 1
    Havee
        3
    Havee  
       2014-08-07 20:54:59 +08:00   ❤️ 1
    可以写个脚本
    case $num in
    1) command ;;
    2) command ;;
    ......
    esac
    clino
        4
    clino  
       2014-08-07 20:55:34 +08:00 via Android   ❤️ 1
    随便用python shell之类的写几行就出来了
    当然针对楼主ssh的场合,在 .ssh/config里配置host就行了
    Tink
        5
    Tink  
       2014-08-07 21:01:09 +08:00 via iPhone   ❤️ 1
    alias
    Liang
        6
    Liang  
    OP
       2014-08-07 21:09:09 +08:00
    @Mutoo
    @Tink

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

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

    随后直接
    ssh name
    xuxu
        11
    xuxu  
       2014-08-07 21:28:26 +08:00   ❤️ 1
    写个可以接受参数的脚本,去掉后缀ln到PATH执行路径下。
    sampeng
        12
    sampeng  
       2014-08-07 22:32:50 +08:00   ❤️ 1
    @Havee 才是真理好么。。你们还想这么多。。。
    我的sshconfig。。里面有几十个服务器了= =!
    sampeng
        13
    sampeng  
       2014-08-07 22:33:12 +08:00
    发慢了。。这种办法还能直接免密码登陆。。
    leavic
        14
    leavic  
       2014-08-07 23:06:12 +08:00
    明显alias啊
    SoloCompany
        15
    SoloCompany  
       2014-08-07 23:34:49 +08:00
    .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  
       2014-08-08 00:00:51 +08:00   ❤️ 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  
       2014-08-08 00:49:56 +08:00 via iPad
    ssh-copy-id
    alias
    我现在都sshxx直接登录
    tonitech
        18
    tonitech  
       2014-08-08 01:07:31 +08:00
    你到/etc/profile里面设置
    alias conna="ssh 1.2.3.4"
    alias connb="ssh 5.6.7.8"
    caonan
        19
    caonan  
       2014-08-08 09:37:19 +08:00
    10 楼正解。
    Liang
        20
    Liang  
    OP
       2014-08-08 09:52:01 +08:00
    @Havee 已通过该方法实现!3Q

    @xuxu
    @sampeng
    @leavic
    @SoloCompany
    @kodango
    @dingyaguang117
    @tonitech
    @caonan
    3Q,很热心!
    bjzhush
        21
    bjzhush  
       2014-08-08 11:29:39 +08:00
    alias
    huangyan9188
        22
    huangyan9188  
       2014-08-08 15:44:20 +08:00
    linux 的命令本身就是跑全局的程序 这些程序一般放在/usr/bin /usr/sbin 或者是环境变指定的文件夹下面,然后输入程序名(也就是指令)全局的来执行就好了
    因此楼主只需要写一个程序放在全局路径下就ok了
    怎么实现都可以,楼上诸位大神已经给出了脚本代码
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2907 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 14:40 · PVG 22:40 · LAX 07:40 · JFK 10:40
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.