Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have had a question for a while whether it possible for a Perl or Bash script to:

  • log into another machine
  • enter password
  • run a few commands
  • save output in a local file

How can I do it?

Thanks for the great site!

share|improve this question
1  
'login' how? telnet? ssh? RDP? Citrix? Please clarify. –  pjz Sep 17 '09 at 17:00
add comment

4 Answers

It's possible, but The Right Tool is expect. There are also expect-like libraries available for most scripting languages.

share|improve this answer
add comment

your question is tagged with perl, so i'll assume you're using that.

Other assumptions:

  • you're using ssh
  • you're on a linux type system
  • if you're on windows, try installing cygwin (maybe overkill for what you're doing)

i would create ssh key pair* between your local and remote machine, this enables you to set up a secure connection w/o having to type in a password (very helpful for automated sessions). the link is to Google, because there are so many tutorials, and they vary slightly, so you should find one for your version of operating system.

once you're able to this, you can execute commands remotely

$local machine> ssh remoteserver.com /file/to/execute.sh

the output will be displayed on your local machine. you can then shell out from perl and execute this command, then save the output to a file, just as you would if you were executing a script on the local machine.

share|improve this answer
add comment

Here is a sample expect script which will automate your ssh (assuming) login Source : http://bash.cyberciti.biz/security/expect-ssh-login-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 [lrange $argv 0 0]
set ipaddr [lrange $argv 1 1]
set scriptname [lrange $argv 2 2]
set arg1 [lrange $argv 3 3]
set timeout -1
# now connect to remote UNIX box (ipaddr) with given script to execute
spawn ssh root@$ipaddr $scriptname $arg1
match_max 100000
# Look for passwod prompt
expect "*?assword:*"
# Send password aka $password
send -- "$password\r"
# send blank line (\r) to make sure we get back to gui
send -- "\r"
expect eof
share|improve this answer
    
No 'bash' in this bash script. It's actually an expect script. –  Insyte Sep 17 '09 at 19:22
    
My bad.I meant expect.Sorry. –  Sharjeel Sayed Sep 18 '09 at 7:13
add comment

I tried this with expect, but it didn't work: it closed the connection at the end.

Can we run a script via ssh which will log into remote machines, run a command, and not disconnect?

So ssh in a machine, cd to such and such a directory, and then run a command, and stay logged in.

-Jonathan

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.