Automatically (or more easily) reconnect to a screen session after network interruption

后端 未结 6 1226
旧巷少年郎
旧巷少年郎 2021-01-30 00:20

ADDED: This question is now, I believe, subsumed by this one: Using GNU Screen completely transparently and automatically

See also this related question:
https://s

6条回答
  •  天命终不由人
    2021-01-30 00:48

    This is now subsumed by this: Using GNU Screen completely transparently and automatically


    Here's a script, ssc, that works just like ssh but takes a third argument to specify the screen to reconnect to, or the name of a new screen. I believe this script subsumes everything in the original question.

    #!/usr/bin/env perl
    # Use 'ssc' (this script) instead of 'ssh' to log into a remote machine.
    # Without a 3rd argument it will list available screens.
    # Give it a 3rd argument 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.
    # Screen is a little too clever though in that if there's an existing screen "bar"
    #   and you say "ssc remote.com b" it will reconnect you to "bar" instead of making
    #   a new screen "b".  It's like invisible and silent tab-completion.
    
    if(scalar(@ARGV)==0 || scalar(@ARGV) > 2) {
      print "USAGE: ssc remote.com [screen name]\n";
    } elsif (scalar(@ARGV) == 1) {
      $machine = shift;
      @screens = split("\n", `ssh $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"; }
        }
      }
      print "ATTACHED screens:\n";
      for(keys(%att)) { print "  $_\n" if $att{$_}; }
      print "DETACHED screens:\n";
      for(keys(%att)) { print "  $_\n" unless $att{$_}; }
    } else {
      $machine = shift;
      $tag = shift;
      system("ssh -t $machine \"screen -S $tag -dr || screen -S $tag\"");
    }
    

提交回复
热议问题