Skip to content

Instantly share code, notes, and snippets.

@andrewdeandrade
Created June 29, 2012 18:19
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save andrewdeandrade/3019777 to your computer and use it in GitHub Desktop.
Save andrewdeandrade/3019777 to your computer and use it in GitHub Desktop.
Node.js ('child_process').spawn bug with SSH

This node app attempts to run ssh via require('child_process').spawn with the following arguments:

$ ssh -tt -l ubuntu -N -i ~/.ssh/scrapers.pem -D 9050 0.0.0.0 -o StrictHostKeyChecking=no

... where 'ubuntu' is the username for the AWS EC2 AMI you are SSHing to and '0.0.0.0' is the IP address for the instance.

To replicate this issue, edit app.js to include the path to your *.pem file to login to the instance you want to ssh and the IP address of that machine, then run it with:

$ node app.js 

To confirm an SSH dynamic tunnel has been created and is working, run this line:

$ casperjs casperproxytest.js --proxy=127.0.0.1:9050 --proxy-type=socks5

If you don't have phantomjs and casperjs installed, you can install them both with brew:

$ brew install phantomjs

$ brew install casperjs
var spawn = require('child_process').spawn
var instance = {
ipAddress: "0.0.0.0"
};
var awsPreSharedKeyPath = '~/.ssh/aws-preshared-key.pem'; // make sure key has permissions 600 with chmod
var spawnArgs = [ '-tt', // force teletype since ssh uses a psuedo-terminal
'-l ubuntu', // login name for the AMI being used
'-N', // don't execute remote commands since we are using instance for dynamic tunneling
//'-v', // verbose mode
'-i ' + awsPreSharedKeyPath, // identity file
'-D 9050', // dynamic port on localhost
instance.ipAddress, // ip address of machine you are SSHing into
'-o StrictHostKeyChecking=no' ]; // don't require you to type 'yes' if IP not yet on the list of known hosts
var ssh = spawn('ssh', spawnArgs, {
env: process.env
});
ssh.stdout.on('data', function (data) {
console.log('stdout: ' + data);
});
ssh.stderr.on('data', function (data) {
console.log('stderr: ' + data);
});
ssh.on('exit', function (code) {
console.log('child process exited with code ' + code);
});
var casper = require('casper').create();
casper.start('http://whatismyipaddress.com/', function() {
this.debugPage();
});
casper.run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment