Unix and Linux network configuration. Multiple network interfaces. Bridged NICs. High-availability network configurations.


Reviews of latest Unix and Linux software. Helpful tips for application support admins. Automating application support.


Disk partitioning, filesystems, directories, and files. Volume management, logical volumes, HA filesystems. Backups and disaster recovery.


Distributed server monitoring. Server performance and capacity planning. Monitoring applications, network status and user activity.

Commands & Shells

Cool Unix shell commands and options. Command-line tools and application. Things every Unix sysadmin needs to know.

Home » Commands & Shells

Using Expect with SSH and Su

Submitted by on August 29, 2011 – 10:47 am 6 Comments

We have discussed using expect with sudo in an SSH session. Another common way to run commands on remote servers as root is to use the su – root command, where sudo is not configured. The trick to automating su operations using expect is to launch the su command with the spawn ssh command. For our next example we have a file called server_list, containing a list of remote hosts. We need to ssh to each host using a non-privileged user account (local, LDAP, NIS), then su to root and change permissions of the /etc/issue file.

# cat ./server_list

The following shell script will read hostnames from the list, initiate SSH, enter passwords for the user and root account, and execute the chmod command.

cat server_list | while read host
expect -c "
set timeout 5
spawn ssh -tq ${username}@${host} su - root
expect "ssword" { send "${userpass}r" }
expect "ssword" { send "${rootpass}r" }
expect "#" { send "chmod 644 /etc/issuer" }
expect "#" { send "exitr" }
expect eof"

You will provide the username and both user and root passwords from command line when running the script:

./ userbob bobspassword rootpassword

Print Friendly, PDF & Email


  • Mak Sultan says:

    Hi guys

    I am fairly new to shell scripts, and have written the following script to SSH into a certain server, SU into the correct user account (I do not control the server, so this is the only way to do it), set the display and run a series of applications.

    The Issue I am having is, as the shell script SSH’es into the server, the script does not continue to write the output into the SSH session. I was not expecting this to work, as I believe that the script terminates as SSH starts, my question is, how can I achieve what I want?

    The script is below:

    echo Loading Remote Sparc Display – Press Enter to Begin
    ssh ruser@
    sleep 5
    sleep 5
    su – rmtinstall
    sleep 5
    AnOTH3R P455w0Rd
    sleep 5
    setenv DISPLAY
    cmdtool& cmdtool& iview&

    Any Ideas would be much appreciated!!


  • Gage says:

    I would like to use telnet or ssh session to administer my mysql database and develop my php sites with vi editor.

  • RuMKilleR says:

    How can I restore an sql dump file to a mysql server?

  • Mathew says:

    I work in an office where an underling constantly uses the internet for frivolous reasons–or just bugs the crap out of various people by forcing everyone to watch Youtube videos, or read Lolcats or FML aloud. This…makes me want to strangle. I need a way to remotely shut off the internet on the iMac using my little Macbook laptop. Any ideas?

  • Xavier Hawthorne says:

    What protocol is used to establish a secure telnet session over a TCP/IP connection?

  • Dana G says:

    It takes along time for my ssh sessions to connect when my firewall is running, why is that?

    ipfilters opensolaris/solaris 10

Leave a Reply

%d bloggers like this: