Create a Ruby Pseudo Terminal (PTY) and Invoke an Interactive Command (SFTP)
Purpose
I needed to upload files on a SFTP server programmatically and automatically in a RoR Enviroment. SFTP ruby library wrapper are very limited (I only found this one actually) and is in maintenance (not more maintained) and I had some troubles uploading large files.
Anyway I decided to come back to use the old SFTP Command Line Interface who is perfectly working.
Unlucky this one is an Interactive CLI.
The trick is to use a Ruby Pseudo Terminal (PTY), listen to the console input for some patterns and write in the console output according this pattern as a real user would do.
Here is a code snippet who doing the job and working perfectly.
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
|
Gist
Available here. Please feel free to improve it !