Archiv verlassen und diese Seite im Standarddesign anzeigen : Passwörter per Script ändern
muellners
09-05-2005, 20:59
Hallo,
kann ich als root, user-Passwörter per script ändern ???
Der wechsel von ca. 200 Passwörtern steht an, da wäre
es leichter per Script eine Liste abarbeiten zu lassen.
Bin für jeden Tip dankbar!
Hi, leider "passwd" command in einer Script zu benutzen ist nicht möglich. Dafür habe ich ein C code, compile it, und danach kannst du von deine shellscript abrufen ...
Usage: # chpwd <user> <password>
chpwd.c
-------------------------------------------------
/*
chpass - change a user's password from a script
Use the following command to compile:
# cc -o chpass chpass.c -ls
*/
#include <userpw.h>
#include <sys/types.h>
#include <pwd.h>
#include <usersec.h>
#include <errno.h>
#define KEYCRYPTLEN 2
void usage (){
printf("Usage: chpass user passwd\n");
exit(1);
}
main (int argc,char *argv[]){
int id;
struct userpw *p;
char user[PW_NAMELEN], pass[10], pass2[256];
char *nwpass;
char **message;
struct userpw newpw; /* passwd structure if getuserpw fails */
if (argc != 3) usage();
strcpy(user,argv[1]);
strcpy(pass,argv[2]);
strncpy(pass2,pass,2);
nwpass=(pass,pass2);
/* infos DEBUG
printf("passwd crypti : %s\n",nwpass);
printf("Avant modif : \n");
printf("------------- \n");
*/
setpwdb (S_READ|S_WRITE);
setuserdb (S_READ|S_WRITE);
p=getuserpw(user);
/* Check if the user exists */
if (getuserattr(user,S_ID,&id,SEC_INT))
{
printf("\nuser %s inconnu ...\n\n",user);
return(-1);
exit(ENOENT);
}
if (!(p=getuserpw(user))){
printf("structure passwd vide %s ...\n\n",user);
printf("%s : user\n",user);
/* initialize new userpw struct */
strcpy(newpw.upw_name,user);
newpw.upw_passwd = nwpass;
newpw.upw_lastupdate = time ((long *) 0);
newpw.upw_flags =0;
printf("Nom : %s\n",user);
printf("Password : %s\n",newpw.upw_passwd);
printf("LastUpdate : %u\n",newpw.upw_lastupdate);
printf("Flags : %u\n\n\n",newpw.upw_flags);
putuserpwhist(&newpw, message);
putuserattr(user,S_PWD,"!",SEC_CHAR);
putuserattr(user,NULL,NULL,SEC_COMMIT);
p=getuserpw(user);
/*putuserpw(p); */
}
/* infos DEBUG
printf("Nom : %s\n",p->upw_name);
printf("Password : %s\n",p->upw_passwd);
printf("LastUpdate : %u\n",p->upw_lastupdate);
printf("Flags : %u\n\n\n",p->upw_flags);
*/
strcpy(p->upw_passwd,crypt((const *)pass,(const *)pass2));
/* infos DEBUG
printf("Apres modif MEMOIRE : \n");
printf("------------- \n");
printf("Nom : %s\n",p->upw_name);
printf("Password : %s\n",p->upw_passwd);
printf("LastUpdate : %u\n",p->upw_lastupdate);
printf("Flags : %u\n\n\n",p->upw_flags);
*/
setpwdb(S_WRITE);
putuserpw(p);
endpwdb();
}
---------------------------------------------------------
Gruss ...
peschmae
09-05-2005, 21:28
Das Programm expect kann das auch ganz gut. Ist sogar ein example dabei bei mir was das macht:
#!/bin/sh
# \
exec expect -f "$0" ${1+"$@"}
# wrapper to make passwd(1) be non-interactive
# username is passed as 1st arg, passwd as 2nd
set password [lindex $argv 1]
spawn passwd [lindex $argv 0]
expect "assword:"
send "$password\r"
expect "assword:"
send "$password\r"
expect eof
MfG Peschmä
muellners
10-05-2005, 08:27
wie arbeite ich damit eine Liste ab ???
Hab installiert, probiert und resigniert ;-\
#!/bin/sh
# \
exec expect -f "$0" ${1+"$@"}
# wrapper to make passwd(1) be non-interactive
# username is passed as 1st arg, passwd as 2nd
set password [lindex $argv 1]
spawn passwd [lindex $argv 0]
expect "assword:"
send "$password\r"
expect "assword:"
send "$password\r"
expect eof
bräuchte noch Aufklärung über
"$0" ${1+"$@"}
den Rest verstehe ich.
Gruß
muellners
peschmae
10-05-2005, 09:05
Das Script msust du eigentlich nicht ändern - am besten steckst du das separat in eine .sh Datei (setpasswd.sh oder so) die du dann aufrufst.
Dann kannst du einfach in den Bash eine schleife machen à la
while read f; do
setpasswd.sh $f neuesPasswort
done < liste.txt
wenn in der Liste alle benutzernamen stehen dern Passwort du ändern möchtest.
MfG Peschmä
muellners
10-05-2005, 10:48
Verstehe ich das mit der Liste richtig?
username neuerPasswort
nexterusername neuersPasswort
nexterusername neuersPasswort
nexterusername neuersPasswort
usw...
username und passwort(neu) durch Tab oder Space getrennt ??
Dann wäre es ;
cat liste.txt |
while read username password junk
do
setpasswd.sh $username $password
done
Gruss, AA
peschmae
10-05-2005, 17:37
while read f; do
setpasswd.sh $f
done < liste.txt
tuts dann auch - weil ja read die ganze Zeile in die Variable einliest (solange du nur eine Variable angibst immer vorausgesetzt, aber das ist ja hier der Fall)
MfG Peschmä
muellners
10-05-2005, 22:57
Danke, das haut hin !!!
Powered by vBulletin® Version 4.2.5 Copyright ©2024 Adduco Digital e.K. und vBulletin Solutions, Inc. Alle Rechte vorbehalten.