LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 07-20-2006, 10:10 AM   #1
vimal
Red Hat India
 
Registered: Nov 2004
Location: Kerala/Pune,india
Distribution: RedHat, Fedora
Posts: 260

Rep: Reputation: 34
How to direct screen output to a variable?


Hi Folks,

I am working on a script that is used to add users in a LAN. I want to run this script on a workstation and add users on a central server by using the credentials which is supplied when the script prompts. I am using SSH with the public key copied onto the server so i can run commands in the server from the local machine. The username and password are stored in variables in the local machine and i am using the useradd command.

ie..ssh root@serverIP useradd $username -p $password

The problem is that we cant use the $password as plain text when using the -p option, but has to supply it as encrytpted. For this I am using the utility 'grub-md5-crypt'.

Now comes the real problem:

When I run the 'grub-md5-crypt', it promts us for the string two times. After that it presents us with the encrypted string. Now I want to get this encrypted string assigned to a variable and use it with the useradd command. Really I don't know how to implement this as I am a newbie to shell scripting. The most troubling thing is that I have finished my script and am waiting for this part. Please help...

Thanks in advance...



Vimal
 
Old 07-20-2006, 11:37 AM   #2
marozsas
Senior Member
 
Registered: Dec 2005
Location: Campinas/SP - Brazil
Distribution: SuSE, RHEL, Fedora, Ubuntu
Posts: 1,393
Blog Entries: 1

Rep: Reputation: 63
Use the system call crypt to create an encrypted password from command line.
Code:
#define _XOPEN_SOURCE
#include <unistd.h>

int main (int argc, char* argv[]) {
        char *result;

        result= crypt (argv[1], "$1$");

        // printf ("the encrypted password for %s is %s\n", argv[1], result);
        printf ("%s\n", result);
        return (0);
}
after you have compiled this small c program, you can get the encripted password which is compatible with useradd's p option:

Code:
root@srv2:~# crypt anewpassword
$1$$F8UiBgeByod6aJH9ApWXG0
root@srv2:~#
and you can use it as in:

Code:
ssh root@serverIP useradd $username -p $(crypt clear_text_password_here)
 
Old 07-20-2006, 11:44 AM   #3
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,506

Rep: Reputation: 116Reputation: 116
marozsas has a good solution. If you don't want to install a new binary for this, however, you can probably use expect to perform the operation and capture output for you.
 
Old 08-28-2006, 09:32 AM   #4
vimal
Red Hat India
 
Registered: Nov 2004
Location: Kerala/Pune,india
Distribution: RedHat, Fedora
Posts: 260

Original Poster
Rep: Reputation: 34
Hello,

Thankyou everyone for the information... But the C code presented some errors related to the 'crypt'. I had gcc version 3.4.2 for the compilation.

This was the output :::

$ gcc -o script script.c
/tmp/cchB3wno.o(.text+0x2d): In function `main':
: undefined reference to `crypt'
collect2: ld returned 1 exit status


Could you please look into this and clear my way...

Thanks...
 
Old 08-28-2006, 09:46 AM   #5
marozsas
Senior Member
 
Registered: Dec 2005
Location: Campinas/SP - Brazil
Distribution: SuSE, RHEL, Fedora, Ubuntu
Posts: 1,393
Blog Entries: 1

Rep: Reputation: 63
You forget to pass the library name to the linker.

Code:
$ gcc src/crypt.c -o bin/crypt -lcrypt
 
Old 08-28-2006, 12:15 PM   #6
cramer
Member
 
Registered: Feb 2006
Distribution: Red Hat 9
Posts: 112

Rep: Reputation: 15
Here is a simple shell script which I placed in my /bin directory along with the expect script to add a user and encrypt their password for them. It depends upon 2 command line paramters 1=username 2=password

The bash script:
Code:
#!/bin/sh
# Add client to server
/usr/sbin/adduser -m $1
passwd.exp $1 $2
The expect script: (save as passwd.exp)
Code:
#!/usr/bin/expect
spawn passwd [lindex $argv 0]
set password [lindex $argv 1]
expect "password:"
send "$password\r"
expect "password:"
send "$password\r"
expect eof
Now if the exp and sh scripts are in the same directory running the bash script with following username and password will work. I know it is very rough but it gets the job done for me.
 
Old 08-29-2006, 12:06 AM   #7
vimal
Red Hat India
 
Registered: Nov 2004
Location: Kerala/Pune,india
Distribution: RedHat, Fedora
Posts: 260

Original Poster
Rep: Reputation: 34
Hello....

Thankyou everyone out there who contributed on this topic... My script is running fine using the C code..

Thanks...
 
  


Reply

Tags
adduser, assign, command, expect, output, script, useradd, variable, variables


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to save output from a command to a variable? tomolesonjr Linux - Newbie 6 04-19-2006 03:04 PM
Perform an operation on files x, y...direct output to x.out, y.out respectively johndoe0028 Linux - General 6 04-02-2006 10:39 PM
How to direct output both log file and to terminal... freebies Linux - General 1 02-25-2005 06:32 AM
insert output of sed into a variable hwouters Linux - General 3 11-06-2004 07:54 PM
How to direct output from make to a file? DDD25 Programming 6 07-25-2004 05:55 AM


All times are GMT -5. The time now is 02:36 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration