Expect script to su to root and perform sed

Asked
Active3 hr before
Viewed126 times

6 Answers

90%

Meta Stack Overflow ,Stack Overflow en español,Stack Overflow em Português, Stack Overflow Public questions & answers

This is more or less what you asked, but is untested.

#!/bin/sh

#
if run from shell, restart with wish\
exec wish "$0"
"$@"

package require Expect

proc fix_a_host {
   host usrname password root_password
} {
   # open session to host, wait
   for a username prompt
   spawn ssh $host
   expect "username:"

   # send username, wait
   for a password prompt
   send "$usrname\r"
   expect "password:"

   # send password, wait
   for shell prompt
   send "$password\r"
   expect "%"

   # become root, wait
   for prompt
   send "su\r"
   expect "#"

   # change TEXT to NEWTEXT in password file
   send "sed 's/TEXT/NEWTEXT'" / etc / passwd
   expect "#"

   # exit root, exit host connection
   send "exit\r"
   expect "%"

   send "exit\r"
   expect eof
}

fix_a_host {
   "host1"
   "someuser"
   "user_password"
   "root_password"
}
fix_a_host {
   "host2"
   "someuser"
   "user_password"
   "root_password"
}
load more v
88%

Execute the above expect program as shown below.,Execute this as shown below.,Execute it as shown below.,The example expect program shown below automates the ssh login from one machine to another machine.

The following expect script is expecting the specific string “hello”. When it finds it (after user enters it), “world” string will be send as response.

#!/usr/bin/expect

expect "hello"
send "world"

By default, the expect timeout is 10 seconds. If you don’t enter anything for the expect command, it times out in 20 seconds. You can also change the timeout as shown below.

#!/usr/bin/expect

set timeout 10
expect "hello"
send "world"

The below example does the addition program automation.

#!/usr/bin/expect

set timeout 20

spawn "./addition.pl"

expect "Enter the number1 :" {
   send "12\r"
}
expect "Enter the number2 :" {
   send "23\r"
}

interact

Execute this as shown below.

$. / user_proc.exp
spawn. / addition.pl
Enter the number1: 12
Enter the number2: 23
Result: 35

On the successful matching of string expect returns, but before that it stores the matched string in $expect_out(0,string). The string that are received prior plus the matched string are stored in $expect_out(buffer). The below example shows you the value of these two variable on match.

#!/usr/bin/expect

set timeout 20

spawn "./hello.pl"

expect "hello"
send "no match : <$expect_out(buffer)> \n"
send "match :  <$expect_out(0,string)>\n"

interact

The hello.pl program just prints only two lines as shown below.

#!/usr/bin/perl

print "Perl program\n";
print "hello world\n";

Execute it as shown below.

$ ./match.exp
spawn ./hello.pl
Perl program
hello world
no match : <Perl program hello>
   match : <hello>

Expect allows you to pass the password for the Linux login account from the program, instead of entering the password on the terminal. In the below program, su login is automated to login into desired accounts.

#!/usr/bin/expect

set timeout 20

set user[lindex $argv 0]

set password[lindex $argv 1]

spawn su $user

expect "Password:"

send "$password\r";

interact

Execute the above expect program as shown below.

bala @localhost $. / su.exp guest guest
spawn su guest
Password:
   guest @localhost $

The example expect program shown below automates the ssh login from one machine to another machine.

#!/usr/bin/expect

set timeout 20

set ip[lindex $argv 0]

set user[lindex $argv 1]

set password[lindex $argv 2]

spawn ssh "$user\@$ip"

expect "Password:"

send "$password\r";

interact

Execute the above expect program as shown below.

guest @host1 $. / ssh.exp 192.168 .1 .2 root password
spawn ssh root @192 .168 .1 .2
Password:
   Last login: Sat Oct 9 04: 11: 35 2010 from host1.geetkstuff.com
root @host2 #
load more v
72%

We can use spawn to run any program we want or any other interactive script.,The remaining lines are the Expect script that interacts with our shell script.,We did not interact with our script at all; the Expect program does the job for us.,Now we will write the Expect scripts that will answer this automatically:

If the expect command if not installed on your system, you can install it using the following command:

$ apt - get install expect
load more v
65%

Thanks for contributing an answer to Unix & Linux Stack Exchange!,Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up., Stack Exchange network consists of 178 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers. , By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

sudo sed 's|gateway 192.168.56.1| |g' / etc / network / interfaces > /etc/network / interfaces
load more v
75%

how do i do this- grep -w “…” filename with the comand “sed”?,How to use df command in Linux / Unix {with examples},Below is some standard grep command explained with examples to get you started with grep on Linux, macOS, and Unix:,Linux grep commands explained with shell pipes examples

grep 'word'
filename
fgrep 'word-to-search'
file.txt
grep 'word'
file1 file2 file3
grep 'string1 string2'
filename
cat otherfile | grep 'something'
command | grep 'something'
command option1 | grep 'data'
grep--color 'data'
fileName
grep[-options] pattern filename
fgrep[-options] words file
load more v
40%

Runs a program or script as a substitute user. su rjones starts a shell as user rjones. A naked su defaults to root. See Example A-14.,The chown command changes the ownership of a file or files. This command is a useful method that root can use to shift file ownership from one user to another. An ordinary user may not change the ownership of files, not even her own files. [1] ,The ifconfig command is most often used at bootup to set up the interfaces, or to shut them down when rebooting.,Command-line tool for manipulating the root window of the screen.

bash$ groups
bozita cdrom cdwriter audio xgrp

bash$ echo $GROUPS
501
load more v

Other "undefined-undefined" queries related to "Expect script to su to root and perform sed"