TCL/Expect was made for this. You can use screen as part of the process. For example:
Code:
#!/usr/bin/expect
set error 0
spawn -noecho /usr/bin/ssh user@host
expect "$ "
after 1000
send "screen\r"
expect {
"$ " {
after 1000
send "ls\r"
}
timeout {
set error 1
}
}
after 1000
send "\01\04"
exit $error
This will connect via ssh to user@host, run screen, issue the command 'ls' and disconnect. When you reconnect to the session, you will see the output of the 'ls command.
Update: I changed the code to not wait for the completion of the command ("ls" in the example), since I think you want to use this to start a long running command.