Expect script
Expect to the rescue! Expect is a nifty program that automates tasks based on input and output. You should have installed Expect on your computer by now.file save to /usr/local/bin/
########################################
#!/usr/bin/expect -f
#Expect script to supply root/admin password for remote ssh server
#and execute command.
#This script needs three argument to(s) connect to remote server:
#password = Password of remote UNIX server, for root user.
#ipaddr = IP Addreess of remote UNIX server, no hostname
#scriptname = Path to remote script which will execute on remote server
#For example:
#./sshlogin.exp password 192.168.1.11 who
#------------------------------------------------------------------------
#Copyright (c) 2004 nixCraft project <http://cyberciti.biz/fb/>
#This script is licensed under GNU GPL version 2.0 or above
#-------------------------------------------------------------------------
#This script is part of nixCraft shell script collection (NSSC)
#Visit http://bash.cyberciti.biz/ for more information.
#----------------------------------------------------------------------
#set Variables
set ipaddr [lindex $argv 0]
set password [lindex $argv 1]
set scriptname [lindex $argv 2]
set arg1 [lindex $argv 3]
set arg2 [lindex $argv 4]
set arg3 [lindex $argv 5]
set arg4 [lindex $argv 6]
set arg5 [lindex $argv 7]
set arg6 [lindex $argv 8]
set arg7 [lindex $argv 9]
#setting a timeout for the password prompt 5 seconds larger than the SSH ConnectionTimeout parameter
set timeout 35
#now connect to remote UNIX box (ipaddr) with given script to execute
set pid [spawn -noecho ssh -o "ConnectTimeout 30" -o "CheckHostIP no" -o "StrictHostKeyChecking no" $ipaddr $scriptname $arg1 $arg2 $arg3 $arg4 $arg5 $arg6 $arg7]
match_max 5000
#look for password prompt
log_user 0
expect {
"denied" {puts "CRITICAL: wrong SSH password" ; exit 2}
"Name or service not known" {puts "CRITICAL: cannot resolve SSH server name $ipaddr" ; exit 2}
"Connection refused" {puts "CRITICAL: SSH connection to $ipaddr refused" ; exit 2}
"Connection timed out" {puts "CRITICAL: SSH connection to $ipaddr timed out" ; exit 2}
timeout {puts "CRITICAL: SSH server timed out while prompting for password" ; exit 2}
"?assword:"
}
# send password
send -- "$password"
# send blank line to make sure we get back to gui
send -- "r"
expect "r"
log_user 1
# now we wait up to 30 seconds
set timeout 30
expect {
timeout {puts "CRITICAL: execution of $scriptname timed out after 30 seconds" ; exit 2}
eof
}
set waitret [wait]
catch {close}
set state [lindex $waitret 2]
exit [lindex $waitret 3
#####################################
or script
#########################
#!/usr/bin/expect -f
# Expect script to supply root/admin password for remote ssh server
# and execute command.
# This script needs three argument to(s) connect to remote server:
# password = Password of remote UNIX server, for root user.
# ipaddr = IP Addreess of remote UNIX server, no hostname
# scriptname = Path to remote script which will execute on remote server
# For example:
# ./sshlogin.exp password 192.168.1.11 who
# ------------------------------------------------------------------------
# Copyright (c) 2004 nixCraft project <http://cyberciti.biz/fb/>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# ----------------------------------------------------------------------
# set Variables
set password [lindex $argv 0]
set ipaddr [lindex $argv 1]
set scriptname [lindex $argv 2]
set arg1 [lindex $argv 3]
set arg2 [lindex $argv 4]
set arg3 [lindex $argv 5]
set arg4 [lindex $argv 6]
set arg5 [lindex $argv 7]
set arg6 [lindex $argv 8]
set arg7 [lindex $argv 9]
set timeout -1
# now connect to remote UNIX box (ipaddr) with given script to execute
spawn ssh huynh-tuan-anh@$ipaddr $scriptname $arg1 $arg2 $arg3 $arg4 $arg5 $arg6 $arg7
match_max 100000
# Look for passwod prompt
expect "*?assword:*"
#expect "Password:"
# Send password aka $password
send -- "$password\r"
# send blank line (\r) to make sure we get back to gui
send -- "\r"
expect eof
###########################
Command test:
ssh.exp test 172.29.93.105 /home/huynh-tuan-anh/check_exit_status.pl -s /etc/init.d/sshd
- test: username
- 172.29.93.105: server ssh to
Post a Comment
Post a Comment