Browse Source

commandos de telnet

Luciano Andrade 8 years ago
parent
commit
4765b15790
2 changed files with 106 additions and 0 deletions
  1. 60 0
      supervisord/bin/fiberhome/telnet
  2. 46 0
      supervisord/bin/fiberhome/telnet.py

+ 60 - 0
supervisord/bin/fiberhome/telnet

@@ -0,0 +1,60 @@
+#!/usr/bin/expect -d
+#As first thing we check to have 5 arguments:
+if {[llength $argv] != 3} {
+ 
+# We give a message so the user know our syntax:
+puts "usage: ssh.exp username password server port program"
+ 
+#We exit with return code = 1
+exit 1
+}
+ 
+# Now we set variables in expect, note:  [lrange $argv 0 0 =$1 the first parameter, and so on.
+ 
+set username [lrange $argv 0 0]
+set password [lrange $argv 1 1]
+set server [lrange $argv 2 2]
+
+set timeout 30
+
+spawn telnet $server 
+
+### Login:
+expect {
+    "Login:"    { send -- "$username\r" }
+    timeout        { exit 1 }
+}
+
+### Password:
+expect {
+    "Password:"    { send -- "$password\r" }
+    timeout        { exit 1 }
+}
+
+### User>
+expect {
+    "User>"    { send -- "enable\r" }
+    timeout        { exit 1 }
+}
+
+expect {
+    "Password:"    { send -- "$password\r" }
+    timeout        { exit 1 }
+}
+
+expect {
+    -re ".*#"    {
+	send_user "Login Success\n"
+	foreach line [split [gets stdin ] "\n"] {
+		# do s
+		send_user "....$line\n"
+	}
+    }
+    timeout        { exit 1 }
+}
+
+
+#send "quit\n"
+
+#exit 0
+#interact

+ 46 - 0
supervisord/bin/fiberhome/telnet.py

@@ -0,0 +1,46 @@
+import getpass
+import sys
+import telnetlib
+import time
+
+###############################################################################
+
+user = sys.argv[1]
+password = sys.argv[2]
+HOST = sys.argv[3]
+
+###############################################################################
+import struct, fcntl, os
+
+f = open("/var/lock/" + HOST, "w")
+fcntl.flock(f, fcntl.LOCK_EX)
+
+###############################################################################
+
+tn = telnetlib.Telnet(HOST)
+tn.set_debuglevel(1)
+tn.expect(["Login:"],10)
+tn.write(user + "\n")
+tn.expect(["Password:"],10)
+tn.write(password + "\n")
+time.sleep(1);
+tn.write("enable\n");
+tn.expect(["Password:"],10)
+tn.write(password + "\n")
+time.sleep(1);
+#tn.expect(["Password:"],10)
+#tn.write(password + "\n")
+print tn.read_very_lazy()
+
+tn.expect(["#"],10)
+
+#tn.interact()
+time.sleep(1)
+stdin = sys.stdin.read()
+for line in stdin.split("\n"):
+	print("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
+	tn.write(line + "\n")
+	time.sleep(1)
+	print(tn.read_until("#"))
+
+tn.write("\nquit\n")