Hello with tcl scripting can i connect and execute command to remote machine with ssh?? can someone tell me? and how?
thanks..
Code: Select all
#!/usr/bin/expect
################################################################
# What we are trying to create here is a method of
# running SSH all at once. AKA - the user enters
# user/pass/host/command all at once, and this
# script takes care of the rest of the hard stuff.
#===============================================================
set USERNAME [lindex $argv 0]
set PASSWORD [lindex $argv 1]
set SERVER [lindex $argv 2]
set COMMAND [lindex $argv 3]
# Since we are going to need something to "expect"
# we may as well put a temp global variable to the
# task.
set NEW_PROMPT "."
set CHECK_FOR "EXPECT_ME"
################################################################
# What we want to do here is:
# 1 - Make sure we are getting *some* interaction
# 2 - Set the remote terminal (on some types of
# servers) prompt to something pre-specified
# 3 - Expect that we can now see the prompt as the
# first thing on a line when we hit <ENTER>
# 4 - Send a newline, so that the next thing we
# can do is start with another expect
# 5 - Return 0 if it didnt fail, or 1 if it failed
#===============================================================
proc setPrompt_fail {} {
global CHECK_FOR
expect {
-re "." {
send "PS1=$CHECK_FOR\n"
expect {
-re "^$CHECK_FOR" {
set NEW_PROMPT $CHECK_FOR
send "\n"
return 0
}
}
return 1
}
}
return 1
}
################################################################
# What we want to do here is:
# 1 - Get the password argument
# 2 - Determine what type of SSH connection is
# being made: 1st requires authentication
# and password, 2nd requires only password,
# 3rd requires nothing (passwordless SSH)
# 3 - For each type, send and expect, up to the
# point of verified login
# 4 - Attempt to set the prompt of the remote
# machine
# 5 - Return 1 if connect fails, 0 if success
#===============================================================
proc connect_fail {} {
global PASSWORD
expect {
"(yes/no)" {
send "yes\n"
expect {
"assword:" {
send "$PASSWORD\n"
expect {
-re "." {
send "\n"
if {[setPrompt_fail]} {
puts "I couldnt set the remote prompt\n"
} else {
return 0
}
}
}
}
}
}
"assword:" {
send "$PASSWORD\n"
expect {
-re "." {
if {[setPrompt_fail]} {
puts "I couldnt set the remote prompt\n"
} else {
return 0
}
}
}
}
-re "." {
if {[setPrompt_fail]} {
puts "I couldnt set the remote prompt\n"
} else {
return 0
}
}
}
return 1
}
################################################################
# Main Functionality:
# 1 - Turn off logging
# 2 - Spawn SSH connection (using argvs)
# 3 - Do the hard stuff (passwords & stuff)
# 4 - Expect the newly-set prompt
# 5 - Run the argv-quoted command
# 6 - Log ONLY the output from the command
# not all the prompts and such
#===============================================================
log_user 0
spawn ssh $USERNAME@$SERVER
if {[connect_fail]} {
puts "Well that was a miserable failure...\n"
} else {
expect {
-re "^$NEW_PROMPT" {
send "$COMMAND\n"
log_user 1
}
}
send "exit\n"
expect "logout"
expect eof
}
Code: Select all
package require expect
Code: Select all
...
[exec /usr/bin/ssh -i .ssh/id_eggdrop.rsa user@example.com remotecommand with options &]
...