Permalink
Browse files

making betterSample to run towards ubuntu OS

  • Loading branch information...
outbounder committed Aug 26, 2011
1 parent 1d73456 commit f313965c8cfb478ce692bb268855e12b26bb311c
Showing with 53 additions and 25 deletions.
  1. +10 −11 examples/betterSample.js
  2. +6 −4 lib/index.js
  3. +37 −10 lib/login.exp
View
@@ -1,34 +1,33 @@
var SSHClient = require("NodeSSH");
var Expect = require('node-expect');
-var ssh=new SSHClient(addresses,user,password);
+var ssh=new SSHClient("ipOrHostname","user", "password");
function close(addr) {
- console.log('('+addresses.length+') Disconnected from '+addr);
+ console.log('Disconnected from '+addr);
}
-function connect() {
- console.log('Connected to '+this.address);
+function connect(addr) {
+ console.log('Connected to '+addr);
}
function doUptime(match) {
console.log('Got uptime line: '+match[0]);
}
parser = new Expect();
-prompt=/\$ /;
-parser.conversation(prompt)
+parser.conversation("logged")
.sync() // synchronous conversation.
- .expect() // the conversation trigger starts the expect. no need to expect anything more.
- .send("uptime")
- .expect(/uptime/) // expect the command to be echo'd back to us.
+ .expect(null,true) // the conversation trigger starts the expect. no need to expect anything more.
+ .send("uptime\n")
.expect(/\n([^\r]+)/)
.handler(doUptime) // call the doUptime function with the match results.
- .expect(prompt)
- .send("exit")
+ .expect("# ")
+ .send("exit\n")
.emit("close")
.end()
.monitor(ssh)
ssh.on('close',close);
ssh.connect(connect);
+
View
@@ -23,10 +23,11 @@ function sshClient(address,username,password){
}
function setupEvents(self) {
self.ssh.stdout.on('data', function (data) {
- //console.log(data);
- //console.log(data.toString());
- if (self.connected) return self.emit('data',data);
- if (data.toString().match(/^\x0d\x0d/)) { // this is the assumption of connected.
+ //console.log("in:"+data.toString());
+ if (self.connected) {
+ return self.emit('data',data);
+ }
+ if (data.toString().match("logged")) {
self.connected=true;
self.c(self.address);
return self.emit('data',data);
@@ -54,6 +55,7 @@ function sshClient(address,username,password){
}
this.write=function(data){
+ //console.log("out:"+data.toString());
this.ssh.stdin.write(data);
}
this.close=function(){
View
@@ -1,17 +1,44 @@
-set host [lindex $argv 0]
+set timeout 30
+
+#example of getting arguments passed from command line..
+#not necessarily the best practice for passwords though...
+set server [lindex $argv 0]
set user [lindex $argv 1]
set pass [lindex $argv 2]
-set param [lindex $argv 3]
-set cmd [lindex $argv 4]
-set timeout 10
+# connect to server via ssh, login, and su to root
+send_user "connecting to $server\n"
+spawn ssh $user@$server
-spawn ssh $param $user@$host $cmd
-expect "yes/no" {
- send "yes\r"
-}
-expect "password:" {
- send "$pass\r"
+#login handles cases:
+# login with keys (no user/pass)
+# user/pass
+# login with keys (first time verification)
+expect {
+ "> " { }
+ "$ " { }
+ "# " { }
+ "assword: " {
+ send "$pass\n"
+ expect {
+ "> " { }
+ "$ " { }
+ "# " { }
+ }
+ }
+ "(yes/no)? " {
+ send "yes\n"
+ expect {
+ "> " { }
+ "$ " { }
+ "# " { }
+ }
+ }
+ default {
+ send_user "Login failed\n"
+ exit
+ }
}
+send_user "logged"
interact

0 comments on commit f313965

Please sign in to comment.