How do I execute a command every time after ssh'ing from one machine to another?
每次从一台机器转到另一台机器后,如何执行命令?
e.g
ssh mymachine
stty erase ^H
I'd rather just have "stty erase ^H" execute every time after my ssh connection completes.
我的ssh连接完成后,我宁愿每次都执行“stty erase ^ H”。
This command can't simply go into my .zshrc file. i.e. for local sessions, I can't run the command (it screws up my keybindings). But I need it run for my remote sessions.
此命令不能简单地进入我的.zshrc文件。即对于本地会话,我无法运行命令(它搞砸了我的键绑定)。但我需要它运行我的远程会话。
6 个解决方案
#1
12
Put the commands in ~/.ssh/rc
将命令放在〜/ .ssh / rc中
#2
7
You can put something like this into your shell's startup file:
你可以把这样的东西放到shell的启动文件中:
if [ -n "$SSH_CONNECTION" ]
then
stty erase ^H
end
The -n
test will determine if SSH_CONNECTION
is set which happens only when logged in via SSH.
-n测试将确定是否设置了SSH_CONNECTION,仅在通过SSH登录时才会发生。
#3
1
If you're logging into a *nix box with a shell, why not put it in your shell startup?
如果您正在登录带有shell的* nix框,为什么不将它放在shell启动中?
.bashrc
or .profile
in most cases.
大多数情况下.bashrc或.profile。
#4
0
Assuming a linux target, put it in your .profile
假设有一个linux目标,请将它放在.profile中
#5
0
Try adding the command below the end of your ~/.bashrc. It should be exited upon logoff. Do you want this command only executed when logging off a ssh session? What about local sessions, etc?
尝试在〜/ .bashrc末尾添加命令。它应该在注销时退出。您是否希望仅在注销ssh会话时执行此命令?那么本地会议等呢?
trap 'stty erase ^H; exit 0' 0
You probably could setup a .logout file from /etc/profile using this same pattern as well.
您可能也可以使用相同的模式从/ etc / profile设置.logout文件。
#6
0
An answer for us, screen/byobu users:
我们的答案,屏幕/ byobu用户:
The geocar's solution will not work as screen will complain that "Must be connected to a terminal.". (This is probably caused by the fact that .ssh/rc is processed before shell is started. See LOGIN PROCESS section from man 8 sshd
).
geocar的解决方案不起作用,因为屏幕会抱怨“必须连接到终端。”。 (这可能是因为在shell启动之前处理了.ssh / rc这一事实。请参阅man 8 sshd的LOGIN PROCESS部分)。
Robert's solution is better here but since screen and byobu open it's own bash instance, we need to avoid infinite recursion. So here is adjusted byobu-friendly version:
Robert的解决方案在这里更好,但是由于screen和byobu打开它自己的bash实例,我们需要避免无限递归。所以这里调整了byobu友好版本:
## RUN BYOBU IF SSH'D ##
## '''''''''''''''''' ##
# (but only if this is a login shell)
if shopt -q login_shell
then
if [ -n "$SSH_CONNECTION" ]
then
byobu
exit
fi
fi
Note that I also added exit
after byobu
, since IMO if you use byobu in the first place, you normally don't want to do anything outside of it.
请注意,我还在byobu之后添加了退出,因为IMO如果你首先使用byobu,你通常不希望在它之外做任何事情。
#1
12
Put the commands in ~/.ssh/rc
将命令放在〜/ .ssh / rc中
#2
7
You can put something like this into your shell's startup file:
你可以把这样的东西放到shell的启动文件中:
if [ -n "$SSH_CONNECTION" ]
then
stty erase ^H
end
The -n
test will determine if SSH_CONNECTION
is set which happens only when logged in via SSH.
-n测试将确定是否设置了SSH_CONNECTION,仅在通过SSH登录时才会发生。
#3
1
If you're logging into a *nix box with a shell, why not put it in your shell startup?
如果您正在登录带有shell的* nix框,为什么不将它放在shell启动中?
.bashrc
or .profile
in most cases.
大多数情况下.bashrc或.profile。
#4
0
Assuming a linux target, put it in your .profile
假设有一个linux目标,请将它放在.profile中
#5
0
Try adding the command below the end of your ~/.bashrc. It should be exited upon logoff. Do you want this command only executed when logging off a ssh session? What about local sessions, etc?
尝试在〜/ .bashrc末尾添加命令。它应该在注销时退出。您是否希望仅在注销ssh会话时执行此命令?那么本地会议等呢?
trap 'stty erase ^H; exit 0' 0
You probably could setup a .logout file from /etc/profile using this same pattern as well.
您可能也可以使用相同的模式从/ etc / profile设置.logout文件。
#6
0
An answer for us, screen/byobu users:
我们的答案,屏幕/ byobu用户:
The geocar's solution will not work as screen will complain that "Must be connected to a terminal.". (This is probably caused by the fact that .ssh/rc is processed before shell is started. See LOGIN PROCESS section from man 8 sshd
).
geocar的解决方案不起作用,因为屏幕会抱怨“必须连接到终端。”。 (这可能是因为在shell启动之前处理了.ssh / rc这一事实。请参阅man 8 sshd的LOGIN PROCESS部分)。
Robert's solution is better here but since screen and byobu open it's own bash instance, we need to avoid infinite recursion. So here is adjusted byobu-friendly version:
Robert的解决方案在这里更好,但是由于screen和byobu打开它自己的bash实例,我们需要避免无限递归。所以这里调整了byobu友好版本:
## RUN BYOBU IF SSH'D ##
## '''''''''''''''''' ##
# (but only if this is a login shell)
if shopt -q login_shell
then
if [ -n "$SSH_CONNECTION" ]
then
byobu
exit
fi
fi
Note that I also added exit
after byobu
, since IMO if you use byobu in the first place, you normally don't want to do anything outside of it.
请注意,我还在byobu之后添加了退出,因为IMO如果你首先使用byobu,你通常不希望在它之外做任何事情。