Search code examples
linuxsshterminalautomationgnu-screen

Automate SSH and screen commands


I have a PHP script with a web interface which is used to provide input into a process which I am automating. In this process, I am attempting to include using SSH and screen to run some commands (all sent through PHP's exec).

I am currently using the following which is writing echo sent from script to the screen, but isn't executing it. I've tried adding the -ne option to echo and adding \n or ^M to the end. I've also tried changing around the types of quotes I'm using, but I'm having trouble getting the code to execute (sending enter).

ssh -t -t [email protected] 'screen -r -d -S -X myScreen stuff "echo -ne sent from script"' 2>&1

How do I go about getting that code to execute?


Solution

  • This took much longer to figure out than I would like to admit. They key was adding a $ before the command sent to stuff and then using a \n to send the enter press.

    I added an extra screen -list; to demonstrate how to send multiple commands before entering the screen. I also added an extra echo to demonstrate how to send multiple commands to the screen.

    ssh -t -t [email protected] "screen -list; screen -r -d -X -S myScreen stuff $'echo here; echo here\n'" 2>&1