屏幕是惊人的,当然,但我不想去想它。我经常ssh到一台机器,开始做一堆东西,然后想“天哪,我希望我原以为这样做一切,所以我可以从家里重新连接到稍后才启动屏幕会话。”

我想有每当我登录到一台机器屏幕自动启动。 当我得到断开的,我希望能够立即只需重新连接而不会“屏幕-ls”和“屏幕-dr”。

大惊小怪

我有一个实现一个解决这个问题,我会后作为回答的脚本。我很感兴趣,看看其他的方法。

有帮助吗?

解决方案

使用以下,ssc,代替ssh。如果你只是做“ssc remote.com”,那么它会列出现有的屏幕会话。给它一个第三个参数,它会连接到屏幕会话,或者创建它,连接到它。无论哪种方式,如果您断开连接,你可以做的外壳重新连接“向上箭头,输入”。屏幕零所需的知识! 编辑:感谢@klochner延长该处理任意SSH选项。您现在可以使用这个就像SSH!

#!/usr/bin/env perl
# Use 'ssc' (this script) instead of 'ssh' to log into a remote machine.
# Without an argument after the hostname it will list available screens.
# Add an argument after the hostname to attach to an existing screen, or
#   specify a new screen.  Eg, ssc remote.com foo
# The numbers in front of the screen tag can usually be ignored.

# ssh option parsing by @klochner
my $optstring = ""; 
while ($val = shift) {
  if    ($val =~ /^-\w$/)  { $optstring .= " ".$val.(shift); }
  elsif ($val =~ /^-\w+$/) { $optstring .= " ".$val;         }
  elsif ($machine)         { $tag        =     $val;         }
  else                     { $machine    =     $val;         }
}

if (!$machine) {
  print "USAGE: ssc [ssh options] remote.com [screen name]\n";
} elsif (!$tag) {
  @screens = split("\n", `ssh $optstring $machine screen -ls`);
  for(@screens) {
    if(/^\s*(\d+)\.(\S+)\s+\(([^\)]*)\)/) {
      ($num, $tag, $status) = ($1, $2, $3);
      if($status =~ /attached/i) { $att{"$num.$tag"} = 1; }
      elsif($status =~ /detached/i) { $att{"$num.$tag"} = 0; }
      else { print "Couldn't parse this: $_\n"; }
      # remember anything weird about the screen, like shared screens
      if($status =~ /^(attached|detached)$/i) { 
        $special{"$num.$tag"} = "";
      } else {
        $special{"$num.$tag"} = "[$status]";
      }
    }
  }
  print "ATTACHED:\n";
  for(sort { ($a=~/\.(\w+)/)[0] cmp ($b=~/\.(\w+)/)[0] } keys(%att)) {
    ($tag) = /\.(\w+)/;
    print "  $tag\t($_)\t$special{$_}\n" if $att{$_};
  }
  print "DETACHED:\n";
  for(sort { ($a=~/\.(\w+)/)[0] cmp ($b=~/\.(\w+)/)[0] } keys(%att)) {
    ($tag) = /\.(\w+)/;
    print "  $tag\t($_)\t$special{$_}\n" unless $att{$_};
  }
} else {
 system("ssh $optstring -t $machine \"screen -S $tag -dr || screen -S $tag\"");
}

顺便说一下,有一招迫使SSH会话,当你失去网络连接退出,并给你回你的本地终端提示:点击 https://superuser.com/questions/ 147873 / SSH-会话合的xterms冻结换许多 - 分钟 - 每当-它们断开

其他提示

autossh 其中自动重新连接断开的SSH-会话。

它带有一个示例脚本调用rscreen这正是如此。它是,只需:

#!/bin/sh
autossh -M 0 -t $1 "screen -e^Aa -D -R"

然后,你必须重新训练你的手指,而不是键入rscreen hostnamessh hostname

实际上屏幕设置TERM变量“屏幕”。因此脚本更容易。下面是我用什么:

if [ "$TERM" != "screen" ]; then
  screen -xRR
fi

工程就像一个魅力,-x确保即使在屏幕连接别的地方我重视它在这里。这样,我只有每一个屏幕,我可以跟踪所有的。

ssh user@host.com -t 'screen -dRR'

这将重新加载/上连接创建你的屏幕会话。这确实是要求什么,即使它移动的会议产卵到发起客户端的责任。理想情况下,你会希望在服务器管理得到什么呈现给连接客户端的一些过程。据我知道,不存在。没有人建议在此线程的理想解决方案。对我来说这是不及其余“不理想”。没有剧本,没有错误,没有TTY问题,与其他SSH命令无不良相互作用,无限循环,没有文件的编辑没有潜在的,不需要额外的软件包。

我在我的.bashrc以下

 if [ "$PS1" != "" -a "${_STARTED_SCREEN:-x}" = x -a "${SSH_TTY:-x}" ] 
 then 
     export _STARTED_SCREEN=1;
     sleep 1 
     screen -RR && exit 0 
     # normally, execution of this rc script ends here... 
     echo "Screen failed! continuing with normal bash startup" 
 fi

我在网上找到的地方前一段时间,不知道在哪里。

<强>更新修正一个指出在注释中的错误。感谢R.佩特

我已经使用autossh,它是对我非常有用

也许把exec screen -dr在你的.login?

我使用 MOSH(移动壳)。该公司密切关注,即使你去从网络休眠模式,断开连接,改变IP,所以您的连接。当您返回时,你会得到你的连接了。

取决于你的shell,但怎么样的.bashrc? (如果使用bash “屏幕-rd”)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top