This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

connect to remote machine with ssh

Help for those learning Tcl or writing their own scripts.
Post Reply
User avatar
ultralord
Master
Posts: 255
Joined: Mon Nov 06, 2006 6:52 pm

connect to remote machine with ssh

Post by ultralord »

Hello with tcl scripting can i connect and execute command to remote machine with ssh?? can someone tell me? and how?


thanks..
Image
You need a iRC bot for your CS Gameserver? Rent one here
User avatar
ultralord
Master
Posts: 255
Joined: Mon Nov 06, 2006 6:52 pm

Post by ultralord »

i found this:

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
}
Image
You need a iRC bot for your CS Gameserver? Rent one here
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

Lets see...
First off, tcl has no native support for ssh. Thus you'll either have to rely on an external binary, or load some extension to provide ssh functionality.

Which brings us to the next post. This script is written for expect, not tcl. At a minimum you'd need to make sure this is installed along with the appropriate tcl packages, and you'll have to load the expect package in your script:

Code: Select all

package require expect
Next, all expect does, is spawn an external process, and implement a framework for challenge/response operation with this process. However, this interaction is blocking, and will thus prevent your eggdrop from performing any other tasks while the ssh-session is being managed.

For sending a single command through a ssh-session, you could achieve this alot simpler using the ssh-client's commandline options along with rsa or dsa keypairs.

Code: Select all

...
[exec /usr/bin/ssh -i .ssh/id_eggdrop.rsa user@example.com remotecommand with options &]
...
Details for setting up rsa and/or dsa keypairs, along with setting up automated remote logins over ssh is well covered in the ssh documentations, so I'm not going into depths on that here.
NML_375
User avatar
ultralord
Master
Posts: 255
Joined: Mon Nov 06, 2006 6:52 pm

Post by ultralord »

thanks i will take a look yes :-)
Image
You need a iRC bot for your CS Gameserver? Rent one here
Post Reply