linux 如何自定义命令?

2014 年 8 月 7 日
 Liang
我想搭建一台跳板机,来管理手上所有的服务器。例如
输入conn a就ssh 1.2.3.4
输入conn b就ssh 5.6.7.8

请问这样的命令如何实现?
4743 次点击
所在节点    Linux
22 条回复
Mutoo
2014 年 8 月 7 日
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
2014 年 8 月 7 日
Busy
2014 年 8 月 7 日
可以写个脚本
case $num in
1) command ;;
2) command ;;
......
esac
clino
2014 年 8 月 7 日
随便用python shell之类的写几行就出来了
当然针对楼主ssh的场合,在 .ssh/config里配置host就行了
Tink
2014 年 8 月 7 日
alias
Liang
2014 年 8 月 7 日
@Mutoo
@Tink

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

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

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

@xuxu
@sampeng
@leavic
@SoloCompany
@kodango
@dingyaguang117
@tonitech
@caonan
3Q,很热心!

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/126655

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX