LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How to direct screen output to a variable? (https://www.linuxquestions.org/questions/programming-9/how-to-direct-screen-output-to-a-variable-465912/)

vimal 07-20-2006 10:10 AM

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

marozsas 07-20-2006 11:37 AM

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)

Matir 07-20-2006 11:44 AM

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.

vimal 08-28-2006 09:32 AM

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...

marozsas 08-28-2006 09:46 AM

You forget to pass the library name to the linker.

Code:

$ gcc src/crypt.c -o bin/crypt -lcrypt

cramer 08-28-2006 12:15 PM

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.

vimal 08-29-2006 12:06 AM

Hello....

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

Thanks...


All times are GMT -5. The time now is 07:26 PM.