Networking

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

Applications

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

Data

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

Monitoring

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
node1
node2
node3
node4
node5

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.

#!/bin/ksh
username=$1
userpass=$2
rootpass=$3
cat server_list | while read host
do
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"
done

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

./chmod_script.sh userbob bobspassword rootpassword

Print Friendly, PDF & Email

6 Comments »

  • 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:

    #!/bin/csh
    echo Loading Remote Sparc Display – Press Enter to Begin
    read
    ssh ruser@169.24.7.249
    sleep 5
    P4ssW0rD
    sleep 5
    su – rmtinstall
    sleep 5
    AnOTH3R P455w0Rd
    sleep 5
    setenv DISPLAY 10.81.38.153:0.0
    cmdtool& cmdtool& iview&
    exit

    Any Ideas would be much appreciated!!

    Paul

  • 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?

    opensolaris
    ipfilters opensolaris/solaris 10

Leave a Reply

%d bloggers like this: