├── examples ├── betterSample.js └── sample.js ├── lib ├── index.js └── login.exp ├── package.json └── readme.txt /examples/betterSample.js: -------------------------------------------------------------------------------- 1 | var SSHClient = require("NodeSSH"); 2 | var Expect = require('node-expect'); 3 | 4 | var ssh=new SSHClient("ipOrHostname","user", "password"); 5 | function close(addr) { 6 | console.log('Disconnected from '+addr); 7 | } 8 | 9 | function connect(addr) { 10 | console.log('Connected to '+addr); 11 | } 12 | 13 | function doUptime(match) { 14 | console.log('Got uptime line: '+match[0]); 15 | } 16 | 17 | parser = new Expect(); 18 | parser.conversation("logged") 19 | .sync() // synchronous conversation. 20 | .expect(null,true) // the conversation trigger starts the expect. no need to expect anything more. 21 | .send("uptime\n") 22 | .expect(/\n([^\r]+)/) 23 | .handler(doUptime) // call the doUptime function with the match results. 24 | .expect("# ") 25 | .send("exit\n") 26 | .emit("close") 27 | .end() 28 | .monitor(ssh) 29 | 30 | ssh.on('close',close); 31 | 32 | ssh.connect(connect); 33 | 34 | -------------------------------------------------------------------------------- /examples/sample.js: -------------------------------------------------------------------------------- 1 | 2 | var SSHClient = require("NodeSSH"); 3 | 4 | var ssh=new SSHClient(addresses,user,password); 5 | var cmds=["uptime","logout"]; 6 | function close(addr) { 7 | console.log('('+addresses.length+') Disconnected from '+addr); 8 | } 9 | 10 | function data(buffer) { 11 | s=buffer.toString(); 12 | if (/\$ /.test(s)) { 13 | if (cmd=cmds.shift()) 14 | ssh.write(cmd+"\r\n"); 15 | else ssh.close(); 16 | } 17 | } 18 | 19 | function connect() { 20 | console.log('Connected to '+this.address); 21 | this.on('data',data); 22 | } 23 | 24 | ssh.on('close',close); 25 | 26 | 27 | ssh.connect(connect); 28 | 29 | -------------------------------------------------------------------------------- /lib/index.js: -------------------------------------------------------------------------------- 1 | var spawn = require('child_process').spawn; 2 | 3 | function sshClient(address,username,password){ 4 | var self=this; 5 | this.address=address; 6 | this.user=username; 7 | this.line=0; 8 | this.buffer=null; 9 | this.exec=function(cmd) { 10 | if (this.connected) return; 11 | this.ssh = spawn('expect', [__dirname+'/login.exp',this.address,this.user,password,"-o ConnectTimeout=10",cmd]); 12 | setupEvents(this) 13 | } 14 | this.connect=function(connect) { 15 | //console.log('Connecting'); 16 | if (this.connected) return; 17 | this.ssh = spawn('expect', [__dirname+'/login.exp',this.address,this.user,password,"-to ConnectTimeout=10"]); 18 | setupEvents(this) 19 | this.c=connect; 20 | } 21 | this.pipe=function(target) { 22 | this.ssh.stdout.pipe(target); 23 | } 24 | function setupEvents(self) { 25 | self.ssh.stdout.on('data', function (data) { 26 | //console.log("in:"+data.toString()); 27 | if (self.connected) { 28 | return self.emit('data',data); 29 | } 30 | if (data.toString().match("logged")) { 31 | self.connected=true; 32 | self.emit('connected',self.address); 33 | return self.emit('data',data); 34 | } 35 | var str = data.toString().substr(0,16); 36 | if(str == "Connection refuse"){ 37 | self.emit('refused',self.address); 38 | self.ssh.kill(); 39 | return; 40 | } 41 | if(str == "Permission denied"){ 42 | self.emit('denied',self.address); 43 | self.ssh.kill(); 44 | return; 45 | } 46 | }); 47 | 48 | self.ssh.on("exit",function(){ 49 | self.connected=false; 50 | self.removeAllListeners('data'); 51 | self.ssh.stdout.removeAllListeners('data'); 52 | self.ssh.removeAllListeners(); 53 | self.emit('close',self.address); 54 | }); 55 | } 56 | 57 | this.write=function(data){ 58 | //console.log("out:"+data.toString()); 59 | this.ssh.stdin.write(data); 60 | } 61 | this.close=function(){ 62 | this.ssh.kill(); 63 | } 64 | } 65 | require('util').inherits(sshClient,require('events').EventEmitter); 66 | module.exports=sshClient; 67 | -------------------------------------------------------------------------------- /lib/login.exp: -------------------------------------------------------------------------------- 1 | set timeout 30 2 | 3 | #example of getting arguments passed from command line.. 4 | #not necessarily the best practice for passwords though... 5 | set server [lindex $argv 0] 6 | set user [lindex $argv 1] 7 | set pass [lindex $argv 2] 8 | 9 | # connect to server via ssh, login, and su to root 10 | send_user "connecting to $server\n" 11 | spawn ssh $user@$server 12 | 13 | #login handles cases: 14 | # login with keys (no user/pass) 15 | # user/pass 16 | # login with keys (first time verification) 17 | expect { 18 | "> " { } 19 | "$ " { } 20 | "# " { } 21 | "assword:" { 22 | send "$pass\n" 23 | expect { 24 | "> " { } 25 | "$ " { } 26 | "# " { } 27 | } 28 | } 29 | "(yes/no)? " { 30 | send "yes\n" 31 | expect { 32 | "> " { } 33 | "$ " { } 34 | "# " { } 35 | } 36 | } 37 | default { 38 | send_user "Login failed\n" 39 | exit 40 | } 41 | } 42 | 43 | send_user "logged" 44 | interact 45 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "NodeSSH", 3 | "description": "SSH Client for NodeJS", 4 | "version": "0.5.0", 5 | "author": "tsmith", 6 | "repository": "git://github.com/Trakkasure/NodeSSH.git", 7 | "maintainers": [ 8 | { "name": "tsmith", "email": "" }, 9 | { "name": "Brandon Myers", "email": "trakkasure@gmail.com" } 10 | ], 11 | "contributors": [ 12 | { "name": "tsmith", "email": "" }, 13 | { "name": "Brandon Myers", "email": "trakkasure@gmail.com"} 14 | ], 15 | "keywords": ["ssh","net","expect"], 16 | "main": "lib/index.js", 17 | "engines": { "node": ">= 0.2.0" } 18 | } 19 | -------------------------------------------------------------------------------- /readme.txt: -------------------------------------------------------------------------------- 1 | NodeSSH 2 | ------------------------------------------------------------------------ 3 | 4 | NodeSSH is a SSH client for node.js. I would call it lightweight, but 5 | since it spawns subprocesses, this makes it heavyweight to me. 6 | It uses OpenSSH and expect. Make sure you have installed this software. 7 | 8 | Use your system's built-in package manager (apt-get,yum,rpm,dselect,etc..) 9 | to install expect on you machine. Most systems (OSX included) should already 10 | have this installed. This will not work with windows machines. 11 | 12 | 13 | How to use 14 | ------------------------------------------------------------------------ 15 | 16 | Check out the file "sample.js". It should help show you all you need ;) 17 | A better version with node-expect is in betterSample.js 18 | 19 | --------------------------------------------------------------------------------