Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
'login' how? telnet? ssh? RDP? Citrix? Please clarify. – pjz Sep 17 '09 at 17:00

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

share|improve this answer

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 /file/to/

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

Here is a sample expect script which will automate your ssh (assuming) login Source :

#!/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 who
# ------------------------------------------------------------------------
# Copyright (c) 2004 nixCraft project <>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit 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 Sep 18 '09 at 7:13

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.


share|improve this answer

Your Answer


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.