Expect script to su to root and perform sed

jaxstorm
jaxstorm used Ask the Experts™
on
I've never used expect before but need a script to do the following for a large list of hosts

- ssh into a machine
- su to root and enter the root password
- sed a file in /etc/passwd to replace some text with some other text, for this example lets just say the original text is TEXT and the text to replace it with is NEWTEXT
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
You will have to do something like this:
#!/usr/bin/expect

set timeout 15
set ssh_server servername
spawn ssh $ssh_server
  expect {
       "User*:" {
           send "UserID\r"
           expect {
               "Password:" {
                   send "passwd\r"
                   
                   return 0
               }
           }
         }
}

Open in new window


This is not complete.  You will have to follow this logic and have to test at each step because it is hard to program it without having your complete setup.

Follow examples like
http://floppsie.comp.glam.ac.uk/Glamorgan/gaius/scripting/5.html
http://www.thegeekstuff.com/2010/10/expect-examples/
Top Expert 2011
Commented:
Assumption
1. Assume you run on an account can ssh to remote machine without password.
2. Assume you only need to pass the root password for su

------- /tmp/ssh_su.exp ---

#!/usr/bin/expect -f

# display usage
if {$argc!=2} {
   send_user "usage: $argv0 hostname root_password \n"
   exit
}

set timeout -1
match_max 100000

# read first argument as hostname
set hostname [lindex $argv 0]
# stopre root password
set root_pass [lindex $argv 1]

# now ssh to remote UNIX box
spawn ssh $hostname

# Switch to root account
send "su -\n"
# Look for passwod prompt
expect "*?assword:*"
# Send password aka $password
send -- "$root_pass\r";

# Replace strings
send -- "sed 's/TEXT/NEWTEXT/g' /etc/passwd > /etc/passwd.new\n"

# logout from su
send "exit\n"
# logout from ssh
send "exit\n"
expect eof
-------
chmod +x /tmp/ssh_su.exp

So you can run it in your shell script like

for HOST in `awk '!/^$/ && !/^#/ {print $2}' /etc/hosts`
do
   /tmp/ssh_su.exp $HOST  <root password>    # pass the root password here
done
Top Expert 2007

Commented:
I would say you are going about it the wrong way.

You should be using ssh keys and proper tools to modify /etc/passwd

For example:


#!/bin/bash
for host in $(cat /path/to/hosts.txt)
do
  ssh root@$host "usermod -c 'Some text' username"
done

Open in new window

Top Expert 2011

Commented:
1. For security concern, most of companies doesn't not allow ssh root@<remote machine>.
root access is either through console login or "su".
So I thought of security requirement and ruled out ssh root@host.

2. For changing the default shell for multiple users, replace string on /etc/passwd is quicker than usermod command.

For my expect script:
-----------
...
# Replace strings
send -- "sed 's/TEXT/NEWTEXT/g' /etc/passwd > /etc/passwd.new\n"
--------
can be replaced as
--------
send --"/usr/bin/perl -i.bak -pe 's/TEXT/NEWTEXT/g' etc/passwd\n"
-----
Which will generate backup the old /etc/passwd as /etc/passwd.bak

Author

Commented:
Perfect, exactly what I was looking for.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial