egghelp.org community Forum Index
[ egghelp.org home | forum home ]
egghelp.org community
Discussion of eggdrop bots, shell accounts and tcl scripts.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

connect to remote machine with ssh

 
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help
View previous topic :: View next topic  
Author Message
ultralord
Master


Joined: 06 Nov 2006
Posts: 255

PostPosted: Fri Nov 04, 2011 6:16 am    Post subject: connect to remote machine with ssh Reply with quote

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


thanks..
_________________

You need a iRC bot for your CS Gameserver? Rent one here
Back to top
View user's profile Send private message
ultralord
Master


Joined: 06 Nov 2006
Posts: 255

PostPosted: Fri Nov 04, 2011 9:35 am    Post subject: Reply with quote

i found this:

Code:
#!/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
}

_________________

You need a iRC bot for your CS Gameserver? Rent one here
Back to top
View user's profile Send private message
nml375
Revered One


Joined: 04 Aug 2006
Posts: 2857

PostPosted: Fri Nov 04, 2011 12:58 pm    Post subject: Reply with quote

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:
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:
...
[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, idling at #eggdrop@IrcNET
Back to top
View user's profile Send private message
ultralord
Master


Joined: 06 Nov 2006
Posts: 255

PostPosted: Sat Nov 05, 2011 6:58 am    Post subject: Reply with quote

thanks i will take a look yes Smile
_________________

You need a iRC bot for your CS Gameserver? Rent one here
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help All times are GMT - 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Forum hosting provided by Reverse.net

Powered by phpBB © 2001, 2005 phpBB Group
subGreen style by ktauber