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 |
Use the system call crypt to create an encrypted password from command line.
Code:
#define _XOPEN_SOURCE Code:
root@srv2:~# crypt anewpassword Code:
ssh root@serverIP useradd $username -p $(crypt clear_text_password_here) |
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.
|
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... |
You forget to pass the library name to the linker.
Code:
$ gcc src/crypt.c -o bin/crypt -lcrypt |
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 Code:
#!/usr/bin/expect |
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. |