Skip to content

Instantly share code, notes, and snippets.

@AbhishekGhosh
Created August 27, 2022 19:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AbhishekGhosh/8d2e3650558f4693ab1b5b64753a7be6 to your computer and use it in GitHub Desktop.
Save AbhishekGhosh/8d2e3650558f4693ab1b5b64753a7be6 to your computer and use it in GitHub Desktop.
Bash Script for SSH Login
#!/usr/bin/expect
set USER [lindex $argv 0]
set HOST [lindex $argv 1]
set PWD [lindex $argv 2]
log_file /var/log/ssh_tmp.log
set timeout 30
log_user 1
set send_slow {1 .01}
send_log "Connecting to $HOST using $USER user\n"
eval spawn ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o Connecttimeout =30 "$USER\@$HOST"
expect {
timeout { send_user "timeout while connecting to $HOST\n"; exit }
"*No route to host*" { send_user "$HOST not reachable\n"; exit }
"*assword: " { send -s $PWD\r }
}
expect {
timeout { send_user "timeout waiting for prompt\n"; exit }
"*]#" { send_user "Login successful to $HOST\n" }
}
send "hostname\r"
expect {
"*]#" { send "exit\r" }
}
send_user "Disconnected\n"
close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment