bash SSH然后更改Shell

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24993598/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 00:56:54  来源:igfitidea点击:

SSH then change Shell

bashloginsshsolariscsh

提问by user2511875

Potentially a real easy question but I was wondering if anybody can kindly provide some advice.

可能是一个非常简单的问题,但我想知道是否有人可以提供一些建议。

To accomplish a repeating task, I am constantly logging into a remote Solaris server using credentials given to us from our system admin. However, each time I log in, I must change shell (from csh -> bash) as the specific task must be run using BASH.

为了完成重复的任务,我不断使用系统管理员提供给我们的凭据登录到远程 Solaris 服务器。但是,每次登录时,我都必须更改 shell(从 csh -> bash),因为必须使用 BASH 运行特定任务。

Although it is not a major problem doing this, I find the changing to bash shell somewhat tedious as I must repeat this task several times a day, and also occasionally may forget to change shells before running the task, etc (also I prefer BASH too so).

虽然这样做不是一个主要问题,但我发现更改为 bash shell 有点乏味,因为我必须每天多次重复此任务,而且有时可能会忘记在运行任务之前更改 shell,等等(我也更喜欢 BASH所以)。

Is there a way where I can ssh and change default shell in one line so I can start immediately with the script I want on the remote server? Note, I do not what to change any log in files (like .login or .cshrc) as the remote server & the credentials are shared an not specifically for me. I don't want to change the default shell either on the server either as, again, the server & the credentials are used by several people.

有没有一种方法可以让我在一行中 ssh 并更改默认 shell,以便我可以立即在远程服务器上使用我想要的脚本?请注意,我不会更改任何登录文件(如 .login 或 .cshrc),因为远程服务器和凭据不是专门为我共享的。我也不想更改服务器上的默认外壳程序,因为服务器和凭据已被多人使用。

Would anybody have any ideas how to get around such a problem? Any suggestions would be greatly appreciated.

有没有人有任何想法如何解决这样的问题?任何建议将不胜感激。

回答by Beggarman

SSH usually executes the command you pass it as an argument and then disconnects. You'll need three options set to get your interactive session to work:

SSH 通常会执行您作为参数传递的命令,然后断开连接。您需要设置三个选项才能使交互式会话正常工作:

  • ssh -twill force the pseudo-tty allocation necessary for you to interact with the remote command you're asking SSH to run
  • bash -lwill start an interactive login shell
  • csh -l -cwill start an interactive login shell in csh, and then execute the command that follows
  • ssh -t将强制您与您要求 SSH 运行的远程命令交互所需的伪 tty 分配
  • bash -l将启动一个交互式登录 shell
  • csh -l -c将在csh中启动一个交互式登录shell,然后执行下面的命令

To just launch a different shell (i.e., your default is csh, and you want to launch bash):

要启动不同的 shell(即,您的默认值是 csh,并且您想启动 bash):

ssh -t <user>@<server> "bash -l"

To pickup the csh environment first, we start the interactive shell, and then pass the command to switch to bash:

要先拾取 csh 环境,我们启动交互式 shell,然后传递命令切换到 bash:

ssh -t <user>@<server> 'csh -l -c "bash"'