Insert text to file via ssh


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Insert text to file via ssh
# 1  
Old 11-07-2010
Insert text to file via ssh

I want to create a script that will set up key pairs from "thishost" to "remotehosts". I have a validate script in ~/<dir> that I need to prefix the generated key in the authorized_keys file, and I'm having to manually do this everytime.

Here are my attemps, perhaps you could perfect them for me.

Code:
thishost.pub | ssh user@remotehost "cat >> ~/.ssh/authorized_keys"

- not sure if I can ssh to multiple users@hostname by piping?

I then realised I could save a lot of work using text-processing (especially if we were setting up a lot of keypairs), but I know you will criticise me for being over-complex, and possibly UUOC. :-)

Code:
thishost.pub | ssh rsync@<hostnames> "cat >> ~/.ssh/authorized_keys" awk 'BEGIN {print "command="/home/rsync/cron/validate-rsync" ssh-dss "}{print $0}' validate > <key>.pub

However, this puts the text on a new line, which I don't want. I just want it to prefix the generated text. Would awk or sed be easier for this, and how would I do it over ssh, thus cutting out manual text entry on the remote boxes?
# 2  
Old 11-07-2010
Well, you can do anything you send with ssh. You can send a whole script to ssh host ksh. You can have a list of hosts and ids in a flat file to drive it while read hi do done <file_list. You still need to type in the id unless you wrap the whole thing in expect or the like, to get the password prompt for the controlling terminal back to your script's control. For instance, if you get ssh localhost working without a password, you can
Code:
echo $PASSWD | ssh localhost ssh there command

PS: remove any old as you add new so it is repeatab le without creating duplicates:
Code:
(grep -v 'id'' <file ; echo "$new")>/tmp/new_file;cat /tmp/new_file >file


Last edited by DGPickett; 11-07-2010 at 01:02 PM..
# 3  
Old 11-07-2010
I understand ScannerDarkly's question, but don't understand DGPickett's reply.

I have same question in fact, but i resolve by manual way.

Code:
scp thishost.pub user@remotehost:~/.ssh/thishost.pub
ssh user@remotehost "cat ~/.ssh/thishost.pub >> ~/.ssh/authorized_keys"
ssh user@remotehost rm ~/.ssh/thishost.pub

If anyone have simply solution by one ssh command, please reply.
# 4  
Old 11-07-2010
As DGPickett says:

Quote:
You can send a whole script to ssh host ksh.
Code:
# one way
$ ssh username@host <<EOF
command1
command2
command3
EOF
# another way
$ ssh username@host command1 ';' command2 ';' command3
# yet another way
$ ssh username@host exec /bin/ksh < script.ksh

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to insert text within a file?

Hi, I am trying to check for missing dates in a file and would want to insert the missing date into the file. Currently the script is as below #!/bin/ksh dates="dates" cat ${dates} | grep -v "^#" curr_month=`date '+%m` curr_day=`date '+%d` curr_year=`date '+%Y` #curr_month=02... (7 Replies)
Discussion started by: newbie_01
7 Replies

2. Shell Programming and Scripting

How to insert a word into a text file?

Hi, I have a text file with one line having few words separated by space and I need to insert another word on "n"th column/field so that previous word should shift right (to n+1st column). how can I do that? It seems we can do using awk but unable to figure out. Please advise, thanks! ... (11 Replies)
Discussion started by: magnus29
11 Replies

3. Shell Programming and Scripting

Insert value to db from text file

Hi, I have a single value in insertval file. I want to load that value to database with the current date. I tried the below code but it is inserting <NULL> to database and echo $c is also null. cat insertval | awk -F ' ' '{print $1}' > c echo c=$c data=`sqlplus -s user/pwd@hostname <<EOF ... (5 Replies)
Discussion started by: Neethu
5 Replies

4. Shell Programming and Scripting

How to insert text in the middle of a file?

Hi, So far i've made a script that takes two argument, 1st is the contents and the 2nd is the named file. At the moment i've managed to insert new contents as a new line at the top, but i want to ask how can you insert contents in the middle of the file? Source Code #!/bin/bash #Write... (3 Replies)
Discussion started by: zen10
3 Replies

5. Shell Programming and Scripting

Insert Text On file

Hi All, Can someone pls help me to insert some text on a file. my file contains something like below.. AKBULBU, BALUMIL, BATCH,BATCH BOARROB, BOTAKAT, C57896, CAKIOZE, CHECMER, CICOFRA, CISZPAW,2194485 I want output as USER_ID, LOGIN_ID (6 Replies)
Discussion started by: harshakusam
6 Replies

6. Shell Programming and Scripting

Need to insert new text and change existing text in a file using SED

Hi all, I need to insert new text and change existing text in a file. For that I used the below line in the command line and got the expected output. sed '$a\ hi... ' shell > shell1 But I face problem when using the same in script. It is throwing the error as, sed: command garbled:... (4 Replies)
Discussion started by: iamgeethuj
4 Replies

7. Shell Programming and Scripting

How to insert some constant text at beginig of each line within a text file.

Dear Folks :), I am new to UNIX scripting and I do not know how can I insert some text in the first column of a UNIX text file at command promtp. I can do this in vi editor by using this command :g/^/s//BBB_ e,g I have a file named as Test.dat and it containins below text: michal... (4 Replies)
Discussion started by: Muhammad Afzal
4 Replies

8. UNIX for Dummies Questions & Answers

How to insert text in the middle of a file

Hey guys, how do we take a line of text as an argument from a user and then insert it in the middle of a file irrespective of the number of lines in the file. I am trying to do this without SED or AWK. Inserting it in the beginning and at the end is easy, but i am trying to accomplish inserting... (6 Replies)
Discussion started by: kartikkumar84@g
6 Replies

9. Shell Programming and Scripting

insert text into top of file

how would you insert text into a existing file using aguments first arguments being the line of text and the second argument being file name (1 Reply)
Discussion started by: jimbob
1 Replies

10. Shell Programming and Scripting

Insert text file at a certain line.

I need to insert a file called temp_impact (which has about 15 lines in it) to a file called 11.23cfg starting at line 33. I searched the forums and found the sed '34i\ test' 11.23cfg > newfile That will enter word test at the appropriate line, but i need the entire file dumped there. Any... (4 Replies)
Discussion started by: insania
4 Replies
Login or Register to Ask a Question