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

linux 如何自定义命令?

  •  
  •   Liang · Aug 7, 2014 · 5486 views
    This topic created in 4481 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
    Mutoo
    Aug 7, 2014❤️1
    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"
    Busy
    Busy
    Aug 7, 2014❤️1
    3
    可以写个脚本
    case $num in
    1) command ;;
    2) command ;;
    ......
    esac
    clino
    clino
    Aug 7, 2014 via Android❤️1
    4
    随便用python shell之类的写几行就出来了
    当然针对楼主ssh的场合,在 .ssh/config里配置host就行了
    Tink
    Tink
    Aug 7, 2014 via iPhone❤️1
    5
    alias
    Liang
    Liang
    OP
    Aug 7, 2014
    6
    @Mutoo
    @Tink

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

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

    随后直接
    ssh name
    xuxu
    xuxu
    Aug 7, 2014❤️1
    11
    写个可以接受参数的脚本,去掉后缀ln到PATH执行路径下。
    sampeng
    sampeng
    Aug 7, 2014❤️1
    12
    @Havee 才是真理好么。。你们还想这么多。。。
    我的sshconfig。。里面有几十个服务器了= =!
    sampeng
    sampeng
    Aug 7, 2014
    13
    发慢了。。这种办法还能直接免密码登陆。。
    leavic
    leavic
    Aug 7, 2014
    14
    明显alias啊
    SoloCompany
    SoloCompany
    Aug 7, 2014
    15
    .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
    kodango
    Aug 8, 2014❤️1
    16
    将以下内容添加到~/.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
    dingyaguang117
    Aug 8, 2014 via iPad
    17
    ssh-copy-id
    alias
    我现在都sshxx直接登录
    tonitech
    tonitech
    Aug 8, 2014
    18
    你到/etc/profile里面设置
    alias conna="ssh 1.2.3.4"
    alias connb="ssh 5.6.7.8"
    caonan
    caonan
    Aug 8, 2014
    19
    10 楼正解。
    Liang
    Liang
    OP
    Aug 8, 2014
    20
    @Havee 已通过该方法实现!3Q

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