Skip to content

Instantly share code, notes, and snippets.

@gretel
Last active December 21, 2015 10:17
Show Gist options
  • Save gretel/c2ef247f51e3cf0c654e to your computer and use it in GitHub Desktop.
Save gretel/c2ef247f51e3cf0c654e to your computer and use it in GitHub Desktop.
Use 'ssc' (this script) instead of 'ssh' to do your ssh logins. Without a 3rd argument it will list available screens. Given a 3rd argument it will either reattach an existing screen or create a new screen, i.e. 'ssc host.tld session'.
#!/usr/bin/env perl
#
# taken from https://stackoverflow.com/a/1075807/1972627 (Daniel Reeves)
# slightly modified by tom hensel <[email protected]>
# https://gist.github.com/gretel/c2ef247f51e3cf0c654e
#
# Use 'ssc' (this script) instead of 'ssh' to do your ssh logins.
# Without a 3rd argument it will list available screens.
# Given a 3rd argument it will either reattach an existing screen or create a new screen,
# i.e. 'ssc host.tld session'.
#
# The numbers in front of the screen tag can usually be ignored,
# except more than one session has the same symbolic name.
if(scalar(@ARGV)==0 || scalar(@ARGV) > 2) {
print "usage: ssc <host> [screen session 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 "parsing error on: $_\n"; }
}
}
print "OO attached screens:\n";
for(keys(%att)) { print " $_\n" if $att{$_}; }
print "vv 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\"");
}
#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment