Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 如何将ssh直接连接到特定目录?_Bash_Shell_Scripting_Ssh - Fatal编程技术网

Bash 如何将ssh直接连接到特定目录?

Bash 如何将ssh直接连接到特定目录?,bash,shell,scripting,ssh,Bash,Shell,Scripting,Ssh,我经常不得不登录到多个服务器中的一个,然后转到这些机器上的多个目录中的一个。目前我正在做这类事情: localhost ~]$ ssh somehost Welcome to somehost! somehost ~]$ cd /some/directory/somewhere/named/Foo somehost Foo]$ localhost~]$ssh somehost 欢迎来到somehost! somehost~]$cd/some/directory/somewhere/named

我经常不得不登录到多个服务器中的一个,然后转到这些机器上的多个目录中的一个。目前我正在做这类事情:

localhost ~]$ ssh somehost Welcome to somehost! somehost ~]$ cd /some/directory/somewhere/named/Foo somehost Foo]$ localhost~]$ssh somehost 欢迎来到somehost! somehost~]$cd/some/directory/somewhere/named/Foo somehost Foo]$ 我有一些脚本可以确定需要进入哪个主机和哪个目录,但我无法找到一种方法:

localhost ~]$ go_to_dir Foo Welcome to somehost! somehost Foo]$ localhost~]$go\u to\u dir Foo 欢迎来到somehost! [美食家]$
有没有一种简单、聪明的方法可以做到这一点?

SSH本身提供了一种通信手段,它对目录一无所知。由于您可以指定要执行的远程命令(默认情况下,这是您的shell),因此我从这里开始。

您可以执行以下操作:

ssh -t xxx.xxx.xxx.xxx "cd /directory_wanted ; bash --login"
ssh -t xxx.xxx.xxx.xxx "cd /directory_wanted && bash"
/home/me$ cd /usr/share/teminal; rm -R *
这样,您将在您想要的目录上获得一个登录shell


解释

-t
强制伪终端分配。这可用于在远程机器上执行任意基于屏幕的程序,这非常有用,例如在实现菜单服务时

多个
-t
选项强制tty分配,即使ssh没有本地tty

  • 如果不使用
    -t
    ,则不会出现提示
  • 如果不添加
    ;bash
    然后连接将关闭,并将控制权返回到本地计算机
  • 如果您不添加
    bash--login
    ,那么它将不会使用您的配置,因为它不是登录shell

我使用您可以添加的环境变量CDPATH

cd /some/directory/somewhere/named/Foo
到另一台主机上的
.bashrc
文件(或
.profile
或您所称的任何文件)。这样,无论您做什么或从哪里
ssh
登录到该服务器,它都会
cd
到适合您的目录,您所要做的就是像正常情况一样使用
ssh


当然,rogeriopvl的解决方案也很管用,但它有点冗长,而且你必须记住每次都要这么做(除非你做了一个别名),所以它看起来没有那么“有趣”。

另一种在登录后直接使用的方法是创建“别名”。当您登录到系统时,只需键入该别名,您就可以进入该目录

示例:Alias=myfolder'/var/www/Folder'

登录到系统后,请键入该别名(该别名适用于系统的任何部分)
此命令如果不在bashrc中,将适用于当前会话。因此,您还可以将此别名添加到bashrc,以便将来使用它


$myfolder=>将您带到该文件夹

我已经创建了一个工具,可以将SSH和CD连续地放入服务器中,并恰当地命名为。对于您给出的示例,您只需使用:

sshcd somehost:/some/directory/somewhere/named/Foo

如果您有任何问题,请告诉我

只需使用以下命令修改您的主页:
usermod-d/newhome username

在我非常具体的情况下,我只想在远程主机中,在Jenkins从机的特定目录中执行一个命令:

ssh myuser@mydomain
cd /home/myuser/somedir 
./commandThatMustBeRunInside_somedir
exit
sshcd example.com \$JBOSS_HOME
但是我的机器无法执行ssh(我想它无法分配一个伪tty),并让我给出以下错误:

Pseudo-terminal will not be allocated because stdin is not a terminal
我可以通过将“cd-to-dir+my-command”作为ssh命令的参数(不必分配伪终端)并通过传递选项-T来明确告诉ssh命令我不需要分配伪终端来解决这个问题

ssh -T myuser@mydomain "cd /home/myuser/somedir; ./commandThatMustBeRunInside_somedir"

根据@rogeriopvl答案的补充,我建议如下:

ssh -t xxx.xxx.xxx.xxx "cd /directory_wanted ; bash --login"
ssh -t xxx.xxx.xxx.xxx "cd /directory_wanted && bash"
/home/me$ cd /usr/share/teminal; rm -R *
通过
&&
链接命令将使下一个命令仅在上一个命令成功时运行(与使用顺序执行命令的
相反)。当需要
cd
到执行该命令的目录时,这尤其有用

想象一下,执行以下操作:

ssh -t xxx.xxx.xxx.xxx "cd /directory_wanted ; bash --login"
ssh -t xxx.xxx.xxx.xxx "cd /directory_wanted && bash"
/home/me$ cd /usr/share/teminal; rm -R *
目录
teminal
不存在,这会导致您留在主目录中,并使用以下命令删除其中的所有文件

如果您使用
&&

/home/me$ cd /usr/share/teminal && rm -R *

在找不到目录后,该命令将失败。

使用
-t
思想更进一步。我保存了一组脚本,用于调用下面的脚本,以转到频繁访问的主机中的特定位置。我将它们都保存在
~/bin
中,并将该目录保存在我的路径中

#!/bin/bash

# does ssh session switching to particular directory
# $1, hostname from config file 
# $2, directory to move to after login
# can save this as say 'con' then
# make another script calling this one, e.g.
# con myhost repos/i2c

ssh -t $1 "cd $2; exec \$SHELL --login"

我首选的方法是使用SSH配置文件(如下所述),但根据您的使用情况,有几种可能的解决方案

命令行参数 我认为这种方法的最佳答案是christianbundy对公认答案的回答:

ssh -t example.com "cd /foo/bar; exec \$SHELL -l"
使用双引号将允许您使用本地计算机中的变量,除非它们被转义(这里是
$SHELL
)。或者,您可以使用单引号,并且您使用的所有变量都是来自目标计算机的变量:

ssh -t example.com 'cd /foo/bar; exec $SHELL -l'
Bash函数 通过将命令包装到bash函数中,可以简化该命令。假设您只想键入以下内容:

sshcd example.com /foo/bar
您可以通过将其添加到
~/.bashrc
中来实现这一点:

sshcd () { ssh -t "$1" "cd \"$2\"; exec \$SHELL -l"; }
如果使用远程计算机上存在的变量作为目录,请确保对其进行转义或将其放在单引号中。例如,这将cd存储到远程计算机上
JBOSS\u HOME
变量中的目录:

ssh myuser@mydomain
cd /home/myuser/somedir 
./commandThatMustBeRunInside_somedir
exit
sshcd example.com \$JBOSS_HOME
SSH配置文件 如果希望在不使用额外命令行参数的情况下,始终使用普通ssh命令查看特定(或任何)主机的这种行为,可以在ssh配置文件中设置
RequestTTY
RemoteCommand
选项

例如,我只想键入以下命令:

ssh qaapps18
ssh -t qaapps18 'cd $JBOSS_HOME; exec $SHELL'
但希望它始终像以下命令那样运行:

ssh qaapps18
ssh -t qaapps18 'cd $JBOSS_HOME; exec $SHELL'
所以我把它添加到我的
~/.ssh/config
文件中:

Host *apps*
    RequestTTY yes
    RemoteCommand cd $JBOSS_HOME; exec $SHELL
现在,此规则适用于主机名中包含“apps”的任何主机


有关更多信息,请参见

我知道这在很久以前就已经得到了回答,但我在尝试将ssh登录合并到bash脚本和日志中时发现了这个问题