LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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
  Search this Thread
Old 07-26-2008, 04:55 PM   #1
Ricio
Member
 
Registered: Sep 2003
Location: Colombia
Distribution: Debian
Posts: 220

Rep: Reputation: 30
Question 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
 
Old 07-26-2008, 05:24 PM   #2
Ricio
Member
 
Registered: Sep 2003
Location: Colombia
Distribution: Debian
Posts: 220

Original Poster
Rep: Reputation: 30
Question 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...
 
Old 08-02-2008, 09:03 AM   #3
Ricio
Member
 
Registered: Sep 2003
Location: Colombia
Distribution: Debian
Posts: 220

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


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
C program for automatic ssh login trollkotze Linux - General 5 01-09-2008 08:53 PM
How to set login and password and root password in suse 10.2 sirius57 SUSE / openSUSE 8 07-24-2007 09:51 AM
ssh keeps asking for password, have set up passwordless gorbgorb Linux - Networking 4 03-21-2007 04:24 PM
No password user/automatic login to desktop lamiczka Linux - Software 2 11-17-2004 09:50 AM
ssh password set to NULL? SiLiCoN Linux - Security 2 09-20-2004 06:53 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration