Skip to content

Instantly share code, notes, and snippets.

@coffeant
Created May 5, 2015 10:45
Show Gist options
  • Save coffeant/3a29990c0fcbd3a6425b to your computer and use it in GitHub Desktop.
Save coffeant/3a29990c0fcbd3a6425b to your computer and use it in GitHub Desktop.
login.sh
#!/usr/bin/expect -f
#Usage
#assume you save this file as "go"
#1 ./go 10.148.137.174 user is default "root"
#2 ./go 10.148.137.174:/usr/local user is default "root" after login,it will open directory /usr/local/ automatically
#3 ./go franklan@10.148.137.174 user is "franklan"
#4 ./go root@10.148.137.174:/usr/local/ after login,it will open directory /usr/local/ automatically
proc try_password { path} {
#import global variables
global global_passwd_index
global global_passwd
#send_user $passwd
expect {
"*assword:" {
#get a password from array
set passwd_to_sent $global_passwd($global_passwd_index)
send "$passwd_to_sent\r"
expect {
#After login, most shells look like "~#" or "]$".
#We can input our commands
"#*" {
send "cd $path\r"
}
"$*" {
send "cd $path\r"
}
#Login fails, try the next password
"*assword:" {
#iterate the next password
set global_passwd_index [expr $global_passwd_index+1]
set passwd_to_sent $global_passwd($global_passwd_index)
send "$passwd_to_sent\r"
#recursion
try_password $path
}
}
}
"#*" {
send "cd $path\r"
}
"$*" {
send "cd $path\r"
}
"yes/no)?" {
send "yes\r"
#recursion
try_password $path
}
}
}
set timeout 30
#Get the first argv
set ip [lindex $argv 0]
#Default user
set user "root"
#Default directory you want to go into after login
set path "/usr/local/storage/http/"
#The most usual passwords
#In ideal condition, you can have N elements in password array to try one by one. But most machines only allow you to input password 3 times.
#So have 3 passwords here is fine
set global_passwd(1) "#7a9@#cr"
set global_passwd(2) "isd!@#user"
set global_passwd(3) "Wyw&have9"
#Index to iterate password array
set global_passwd_index 1
#Parse params like root@10.148.137.174:/usr/local/storage/pic_share/tools/report
set flag [regexp {([a-z0-9A-Z_]*)@(\d+\.\d+\.\d+\.\d+):(.*)} $ip total_param param1 param2 param3]
if {$flag} {
set user $param1
set ip $param2
set path $param3
} else {
#Parse params like root@10.148.137.174
set flag [regexp {([a-z0-9A-Z_]*)@(\d+\.\d+\.\d+\.\d+).*} $ip total_param param1 param2]
#puts $param1
#puts $param2
#puts $flag
if {$flag} {
set user $param1
set ip $param2
} else {
#Parse params like 10.148.137.174:/usr/local
set flag [regexp {(\d+\.\d+\.\d+\.\d+):(.*)} $ip total_param param1 param2]
if {$flag} {
set ip $param1
set path $param2
}
}
}
# SSH
spawn ssh $user@$ip -q
try_password $path
interact
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment