Avatar of jaxstorm
jaxstormFlag for United Kingdom of Great Britain and Northern Ireland

asked on 

Expect script to su to root and perform sed

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
LinuxUnix OSShell Scripting

Avatar of undefined
Last Comment
jaxstorm
Avatar of farzanj
farzanj
Flag of Canada image

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/
ASKER CERTIFIED SOLUTION
Avatar of wesly_chen
wesly_chen
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Tintin
Tintin

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

Avatar of wesly_chen
wesly_chen
Flag of United States of America image

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
Avatar of jaxstorm
jaxstorm
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

Perfect, exactly what I was looking for.
Linux
Linux

Linux is a UNIX-like open source operating system with hundreds of distinct distributions, including: Fedora, openSUSE, Ubuntu, Debian, Slackware, Gentoo, CentOS, and Arch Linux. Linux is generally associated with web and database servers, but has become popular in many niche industries and applications.

71K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo