PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Serveur - Sécurité et techniques > comp.unix.shell > howto use execpt from a ksh script
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

howto use execpt from a ksh script

Réponse
 
LinkBack Outils de la discussion
Vieux 27/07/2007, 12h01   #1
relikwie
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut howto use execpt from a ksh script

Hi,

I'm trying to create a script to automate user account creation on two
machines.
I use expect to handle passwd input. At this first stage (changing
password locally)
I have difficulty.

I hope someone can me out here.

Thanks in advance


Problem:

expect code in a seperate script is working fine when calling it with
two params.
But calling it from a shell script is a bit hard, where putting expect
code in a function,
and calling the function with params goes wrong.

The error returned is:
-
end: spawn id exp4 not open
while executing
"send r"
-> spawn passwd 0
sh: /usr/local/bin/stty: not found.
3004-687 User "0" does not exist.



A part of the script below:
-
#!/usr/bin/ksh
#

passwd_chng ()
{
# These two echo's return correct values. but how to get expect to use
them???
echo ${1}
echo ${2}
RET_0=$(/usr/local/bin/expect -c "
set PASSWORD [lindex $argv 1]
spawn passwd [lindex $argv 0]
expect "password:"
send "$PASSWORD\r"
expect "password:"
send "$PASSWORD\r"
expect eof
")
echo "-> $RET_0"
}

_create ()
{

if [ "$(grep -w ^${_USER} /etc/passwd | awk -F ":" '{print $1}')" = "$
{_USER}" ]
then
#
echo "\nUser already exists\n"
exit 1
#
fi

# -- Create the user with mls as primary group.
#
mkuser pgrp='mls' home="/home/${_USER}" shell='/usr/bin/ksh' gecos="$
{INFO}" ${_USER}
#

if [[ $? -eq 0 ]]
then
# -- call expect function.
passwd_chng ${_USER} ${PASSWORD}
#
fi
}

#
#===# Checks and settings
#

if [ "$(uname -n)" != "HOST1" ]
then
#
echo "\nCan only run from HOST1.\n"
exit 1
#
fi


if [[ $# -ne 3 ]]
then
# call script with ${0} username "userinfo" password
exit 1
#
fi
if [[ "${1}" = "root" ]]
then
#
echo "Not a good idea!"
exit 3
#
else
#
export GEBRUIKER=${1}
export VOLNAAM=${2}
export PASSWORD=${3}
_create $_USER $INFO $PASSWORD
#
fi

  Réponse avec citation
Vieux 27/07/2007, 14h04   #2
Stephane CHAZELAS
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: howto use execpt from a ksh script

2007-07-27, 04:01(-07), relikwie:
> Hi,
>
> I'm trying to create a script to automate user account creation on two
> machines.
> I use expect to handle passwd input. At this first stage (changing
> password locally)
> I have difficulty.


You may not need ksh at all. expect is a TCL interpreter, and
the TCL language can certainly let you do all ksh can let you do
(it can invoke ksh anyway in any case).

[...]
> end: spawn id exp4 not open
> while executing
> "send r"
> -> spawn passwd 0
> sh: /usr/local/bin/stty: not found.


expect runs the stty command to tune the pseudo-terminals it
opens. The path to stty is hardcoded in expect itself (or in
libexpect). The above suggests that expect has not been compiled
properly on your system as it expects to find stty in
/usr/local/bin where it obviously isn't.

> 3004-687 User "0" does not exist.

[...]
> passwd_chng ()
> {
> # These two echo's return correct values. but how to get expect to use
> them???
> echo ${1}
> echo ${2}
> RET_0=$(/usr/local/bin/expect -c "
> set PASSWORD [lindex $argv 1]
> spawn passwd [lindex $argv 0]
> expect "password:"
> send "$PASSWORD\r"
> expect "password:"
> send "$PASSWORD\r"
> expect eof
> ")


You're messing up with quotes and are not passing any argument
to expect.

You can't pass arguments with -c, but you can use environment
variables.

RET_0=$(
USER=$1 PASSWORD=$2 /usr/local/bin/expect -c '
spawn passwd $env(USER)
expect "password:"
send "$env(PASSWORD)\r"
expect "password:"
send "$env(PASSWORD)\r"
expect eof
'
)

--
Stéphane
  Réponse avec citation
Vieux 27/07/2007, 16h05   #3
relikwie
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: howto use execpt from a ksh script

On Jul 27, 3:04 pm, Stephane CHAZELAS <this.addr...@is.invalid> wrote:
> 2007-07-27, 04:01(-07), relikwie:
>
> > Hi,

>
> > I'm trying to create a script to automate user account creation on two
> > machines.
> > I use expect to handle passwd input. At this first stage (changing
> > password locally)
> > I have difficulty.

>
> You may not need ksh at all. expect is a TCL interpreter, and
> the TCL language can certainly let you do all ksh can let you do
> (it can invoke ksh anyway in any case).
>
> [...]
>
> > end: spawn id exp4 not open
> > while executing
> > "send r"
> > -> spawn passwd 0
> > sh: /usr/local/bin/stty: not found.

>
> expect runs the stty command to tune the pseudo-terminals it
> opens. The path to stty is hardcoded in expect itself (or in
> libexpect). The above suggests that expect has not been compiled
> properly on your system as it expects to find stty in
> /usr/local/bin where it obviously isn't.



I've downloaded a precompiled binary for AIX 5.2.
But could create a softlink pointing the stty binary.

Although the warning, the passwd succseeds.


>
>
>
>
>
> > 3004-687 User "0" does not exist.

> [...]
> > passwd_chng ()
> > {
> > # These two echo's return correct values. but how to get expect to use
> > them???
> > echo ${1}
> > echo ${2}
> > RET_0=$(/usr/local/bin/expect -c "
> > set PASSWORD [lindex $argv 1]
> > spawn passwd [lindex $argv 0]
> > expect "password:"
> > send "$PASSWORD\r"
> > expect "password:"
> > send "$PASSWORD\r"
> > expect eof
> > ")

>
> You're messing up with quotes and are not passing any argument
> to expect.
>
> You can't pass arguments with -c, but you can use environment
> variables.
>
> RET_0=$(
> USER=$1 PASSWORD=$2 /usr/local/bin/expect -c '
> spawn passwd $env(USER)
> expect "password:"
> send "$env(PASSWORD)\r"
> expect "password:"
> send "$env(PASSWORD)\r"
> expect eof
> '
> )
>


Stephane, reading your reaction I realize im only messing around.
No background on how things really relate and work.
In fact the expect bit was copy and paste, trying to get it to work.

Your suggestion worked flawlessly.
Have only a issue with setting the users passwd remotely.
There I adjusted your lines to this:

RET_0=$(
log_user 0
_USER_0=${1} _PASSWORD_0=${2} /usr/local/bin/expect -c '
spawn rsh host2 passwd $env(_USER_0)
expect "password:"
send "$env(_PASSWORD_0)\r"
expect "password:"
send "$env(_PASSWORD_0)\r"
expect eof
'
)

This error occurs:
send: spawn id exp4 not open
while executing
"send "$env(_PASSWORD_0)\r""

  Réponse avec citation
Vieux 27/07/2007, 16h14   #4
Stephane CHAZELAS
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: howto use execpt from a ksh script

2007-07-27, 08:05(-07), relikwie:
[...]
> Your suggestion worked flawlessly.
> Have only a issue with setting the users passwd remotely.
> There I adjusted your lines to this:
>
> RET_0=$(
> log_user 0
> _USER_0=${1} _PASSWORD_0=${2} /usr/local/bin/expect -c '
> spawn rsh host2 passwd $env(_USER_0)
> expect "password:"
> send "$env(_PASSWORD_0)\r"
> expect "password:"
> send "$env(_PASSWORD_0)\r"
> expect eof
> '
> )
>
> This error occurs:
> send: spawn id exp4 not open
> while executing
> "send "$env(_PASSWORD_0)\r""


It suggests that the rsh command terminated at the time expect
did one of the "send"s.

Did you try an run that command manually. Some "passwd"
implementations will refuse to work if there's no terminal to
ask a real user for a passwd. expect is a tool to fake that real
user terminal (and fool passwd), but over rsh, there's no
terminal (expect will provide *rsh* with a faked terminal, but
rshd on the remote end will no provide *passwd* with a
terminal).

You could use rlogin instead of rsh (when rsh is invoked without
a command to run, it invokes rlogin), rlogin starts a login
session on the remote end with a pseudo-terminal.

spawn rlogin host
expec <the-prompt>
send "passwd\r"
expect "assword:"
[...]
expect <the-prompt>

Also, you'll probably want to check for errors and problems.

--
Stéphane
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 05h25.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,15715 seconds with 12 queries