通过ssh会话执行bash别名

系统 Linux
SSH 客户端 (ssh) 是一个登录远程服务器并在远程系统上执行 shell 命令的 Linux/Unix 命令。它被设计用来在两个非信任的机器上通过不安全的网络(比如互联网)提供安全的加密通讯。

[[217825]]

我在远程主机上上设置过一个叫做 file_repl 的 bash 别名 。当我使用 ssh 命令登录远程主机后,可以很正常的使用这个别名。然而这个 bash 别名却无法通过 ssh 来运行,像这样:

  1. $ ssh vivek@server1.cyberciti.biz file_repl
  2. bashfile_replcommand not found

我要怎样做才能通过 ssh 命令运行 bash 别名呢?

SSH 客户端 (ssh) 是一个登录远程服务器并在远程系统上执行 shell 命令的 Linux/Unix 命令。它被设计用来在两个非信任的机器上通过不安全的网络(比如互联网)提供安全的加密通讯。

 

如何用 ssh 客户端执行命令

通过 ssh 运行 free 命令或 date 命令 可以这样做:

  1. $ ssh vivek@server1.cyberciti.biz date

结果为:

  1. Tue Dec 26 090250 UTC 2017

或者:

  1. $ ssh vivek@server1.cyberciti.biz free -h

结果为:

  1. total used free shared buff/cache available
  2. Mem2.0G 428M 138M 145M 1.4G 1.1G
  3. Swap0B 0B 0B

 

理解 bash shell 以及命令的类型

bash shell 共有下面几类命令:

  1. 别名,比如 ll
  2. 关键字,比如 if
  3. 函数 (用户自定义函数,比如 genpasswd
  4. 内置命令,比如 pwd
  5. 外部文件,比如 /bin/date

type 命令command 命令 可以用来查看命令类型:

  1. $ type -a date
  2. date is /bin/date
  3. $ type -a free
  4. free is /usr/bin/free
  5. $ command -V pwd
  6. pwd is a shell builtin
  7. $ type -a file_repl
  8. is aliased to `sudo -i /shared/takes/master.replication'

datefree 都是外部命令,而 file_replsudo -i /shared/takes/master.replication 的别名。你不能直接执行像 file_repl 这样的别名:

  1. $ ssh user@remote file_repl

 

在 Unix 系统上无法直接通过 ssh 客户端执行 bash 别名

要解决这个问题可以用下面方法运行 ssh 命令:

  1. $ ssh -t user@remote /bin/bash -ic 'your-alias-here'
  2. $ ssh -t user@remote /bin/bash -ic 'file_repl'

ssh 命令选项:

bash shell 的选项:

  • -i:运行交互 shell,这样 shell 才能运行 bash 别名。
  • -c:要执行的命令取之于***个非选项参数的命令字符串。若在命令字符串后面还有其他参数,这些参数会作为位置参数传递给命令,参数从 $0 开始。

总之,要运行一个名叫 ll 的 bash 别名,可以运行下面命令:

  1. $ ssh -t vivek@server1.cyberciti.biz -ic 'll'

结果为:

Running bash aliases over ssh based session when using Unix or Linux ssh cli

Running bash aliases over ssh based session when using Unix or Linux ssh cli

下面是我的一个 shell 脚本的例子:

  1. #!/bin/bash
  2. I="tags.deleted.410"
  3. O="/tmp/https.www.cyberciti.biz.410.url.conf"
  4. box="vivek@server1.cyberciti.biz"
  5. [!-f "$I" ] && { echo "$I file not found。"; exit 10; }
  6. >$O
  7. cat "$I" | sort | uniq | while read -r u
  8. do
  9. uu="${u##https://www.cyberciti.biz}"
  10. echo "~^$uu 1;" >>"${O}"
  11. done
  12. echo "Config file created at ${O} and now updating remote nginx config file"
  13. scp "${O}" ${box}:/tmp/
  14. ssh ${box} /usr/bin/lxc file push /tmp/https.www.cyberciti.biz.410.url.conf nginx-container/etc/nginx/
  15. ssh -t ${box} /bin/bash -ic 'push_config_job'

 

相关资料

更多信息请输入下面命令查看 OpenSSH 客户端bash 的 man 帮助

  1. $ man ssh
  2. $ man bash
  3. $ help type
  4. $ help command 
责任编辑:庞桂玉 来源: Linux中国
相关推荐

2019-08-05 10:00:13

LinuxBash命令

2010-06-23 17:16:33

Linux Bash

2018-01-16 10:08:25

Linuxbashshell

2018-10-12 10:40:45

LinuxBash命令

2023-09-11 17:39:35

SSH服务TCP

2020-03-03 22:15:58

byobuSSH会话Linux

2019-05-14 11:00:07

LinuxSSH别名

2022-11-18 08:55:33

2018-04-10 09:06:45

LinuxSSH别名

2017-06-22 08:58:51

Hadoopssh+IPssh+

2020-03-01 17:49:16

Linux脚本语言操作系统

2010-09-17 09:24:30

PowerShellSSH

2022-11-01 16:26:25

Linux命令脚本

2020-12-28 06:29:31

Bash互动游戏Linux

2020-02-24 21:46:09

ChromiumFirefox桌面应用

2022-04-10 22:50:20

SSHLinux会话

2022-03-11 13:28:54

SSHLinux命令

2021-01-31 09:52:49

SSH监控网络攻击

2014-09-26 15:41:51

2009-12-30 16:06:08

LDP协议
点赞
收藏

51CTO技术栈公众号