LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Automatic set no password SSH login (https://www.linuxquestions.org/questions/programming-9/automatic-set-no-password-ssh-login-658457/)

Ricio 07-26-2008 04:55 PM

Automatic set no password SSH login
 
Hello all,

Ive been bouncing ideas lately on how to make a script that automatizes the process of copyind id_rsa.pub into the authorized_keys file, and this are the needs the script will do...

1. Generates SSH key automatically

This one is easy,

ssh-keygen -b 2048 -t rsa -f $(HOME)/.ssh/id_rsa -N ""

2. Makes sure authorized_keys file exists in remote server if it does not it creates it and chmods it to 600, I use expect here...

spawn ssh root@serveraddress 0> echo "mkdir ~/.ssh; touch ~/.ssh/authorized_keys; chmod u+rw .ssh/authorized_keys"
expect "*?assword:*"
send -- "rootpass\r"

Until here I got it working problem comes here, shell prompts error that says it cannot run spawn again and im not quite sure im doing it right!

3. Copy the contents of id_rsa.pub to authorized_key file in the remote server.

I tried the following command,

cat ~/.ssh/id_rsa.pub | ssh root@serveraddress "tee -a .ssh/authorized_keys"

but it does not work, permission denied, but im supossedly login in as root.

4. When all has worked and its all done I will do a restoration of a ntfs image with ntfsclone and modify the current grub so that the partition is recognized and booted into the editing of the grub menu.lst I figure it would be something like
echo "(whatever here)" | tee -a /boot/grub/menu.lst

and the ntfs thing like this:
ssh host ’cat backup.img.gz’ | gunzip -c |ntfsclone --restore-image --overwrite /dev/hda1

Now, this is to be done in one script, and what I have done is run the command separately to see how they work, but once I start to join them in order to get only one script to do this I get confused...

Im starting to learn bash scripting and expect scripting, and I really will use lots of your help with this script...

Thank you

Ricio 07-26-2008 05:24 PM

This is the script so far
 
Code:

#!/usr/bin/expect -f

exec ssh-keygen -b 2048 -t rsa -f /root/.ssh/id_rsa -N ""

spawn ssh root@serveraddress 0> echo "mkdir ~/.ssh;
touch ~/.ssh/authorized_keys; chmod u+rw .ssh/authorized_keys"       
        expect "*?assword:*"
        send -- "password\r"
        expect eof

spawn cat ~/.ssh/id_rsa.pub | ssh root@serveraddress "tee -a
.ssh/authorized_keys"
        expect "*?assword:*"
        send -- "password\r"
        expect eof
exit

This is the error I get:
Code:

spawn ssh root@192.168.128.1 0> echo mkdir ~/.ssh;
touch ~/.ssh/authorized_keys; chmod u+rw .ssh/authorized_keys
Warning: Permanently added 'serveraddress' (RSA) to the list of known hosts.

root@serveraddress's password:
mkdir: cannot create directory `/root/.ssh': File exists
spawn cat ~/.ssh/id_rsa.pub | ssh root@serveraddress tee -a
.ssh/authorized_keys
cat: ~/.ssh/id_rsa.pub: No such file or directory
cat: |: No such file or directory
cat: ssh: No such file or directory
cat: root@serveraddress: No such file or directory
cat: tee -a
.ssh/authorized_keys: No such file or directory

The mkdir error is fine, thats because the .ssh dir already exists, but the rest of the errors I dont get...

Ricio 08-02-2008 09:03 AM

hello... could anyone help me with this ... Im stuck with this error... its taking the pipe | as it if was a file or directory


All times are GMT -5. The time now is 04:13 AM.