Automated Screen Launch
Automation is easy,
if you know what commands to run, which required a little investigation on my part. I found a number of useful commands, which you can use to roll your own automation, or simply use for fun and profit!
- First and most importantly, you need to start a screen daemon. The following command starts a single screen session in detached mode (
-dm) and names it ssh (
-S "ssh"), since I was using this for an SSH server.
screen -dmS "ssh"Next, you want to be able to create new sessions in your screen daemon, and that’s very easy. Specify which one you want to work with (
-S "ssh") and then tell it a command.
screen -S "ssh" -X screenNow that you’ve got a screen daemon going and can create new screen sessions, you need to tell those sessions what to do. The easiest way is with exec, and is shown below. There are two things to keep in mind using this A) the
-p 1portion dictates which session number this command is executed on. The first screen is always session #0 (it’s just like an array in almost any programming language). B) I don’t like this method because it does not work with Ryan Lane’s status tips.
screen -S "ssh" -p 1 -X exec ssh [email protected]If you’d like to keep your status bar up to date with the name of the server, you need to go about connecting to SSH slightly differently. The following example enters the text “ssh [email protected]” and THEN sends a carriage return. It acts just like you had typed it yourself.
screen -S "ssh" -p 1 -X stuff "ssh [email protected]$(printf \\r)"Another variation on the above is to use \n instead of the printf bits above. While it worked for me on the command line, my automation script didn’t like this (no matter how I escaped it). I didn’t have time to figure out exactly why and honestly didn’t care, since I already had a method that worked.
screen -S "ssh" -p 1 -X stuff $'ssh [email protected]\n'So now the fun part, putting it altogether. I created two scripts (in PHP) for your ease of use. All you need to do is download them, update a few variables,
chmod a+xthem and make sure you have php-cli installed. After you execute the following start script, you’ll connect to it with “
screen -x ssh” where ssh is the configured
$name. Once connected to screen you’ll find session 0 stays on the home machine (that’s my personal preference) and then 1 session per server you entered in
$name = "ssh";# Change this line to be whatever name you want, but it doesn’t really matter — as long as it doesn’t collide with another screen.
$servers = array("srv01","srv02","test01");# Change this array to be your server names. You can use FQDN’s if necessary.
$user = "jon";# Change this to be your username.
- Since it is so easy to start screen, it should be easy to stop it. So I made a stop script. This is not a graceful disconnect. It just instructs screen to end itself and nothing more:
- stop_ssh_server Update 2011-11-17: If you want a non-PHP reliant version, check out Julien’s blog post on Dev-tricks.net