ssh in a script

Thread Tools Search this Thread
# 1  
ssh in a script


I am trying to ssh/rlogin onto another box through a script. What is the syntax so I do not get prompted for a password.

Thanks in advance
# 2  
To use ssh in script and not get prompt for password, you need to generate an rsa or dsa public key by

ssh-keygen -t rsa $HOME/.ssh/id_rsa

Run the above command from the local machine home directory of the user that intend to execute the script

The above will produre a file called
rename this file to authorized_keys and copy it
to the remote machine's user home directory.

in your script you will have

ssh username@remote_machine

this will allow ssh connection without password prompt
# 3  
# 4  

I have a detailed description about what to do (pasted below). Something else is that for using ssh most of all you need a tty alocation, what you do not get per default. Therefor issuing "ssh -ttt <server> <command>" would be most preferred.

Now, below the copy and paste :

Setting up the keys

1. It is most efficient to take the time to set up all hosts at the same time. If you have an account on 6 machines, set up all 6 at the same time. If you later are granted access to another machine, it is your responsibility to make the modifications to all of them. This is the responsibility of each user rather than a job for the systems administrator.
2. Fastest/easiest way to set up the key files.
a. Open a session on one host using CDE (Citrix Metaframe for Unix is best). Open a standard text terminal session inside the CDE desktop.
b. Also open the text editor from the CDE desktop.
c. On the terminal session, type ssh-keygen -t dsa. Press <ENTER> for the next 3 questions. Do NOT use a passphrase unless instructed to do so by Security, or if standard policies change.
i. The above command will create the .ssh directory under the users' home directory--which is sometimes abbreviated with the ~ sign.
ii. It will also create 3 files, you will add a fourth, and a fifth will soon appear.
d. Change into the .ssh directory with cd ~/.ssh.
e. Next, display the public key created with the previous step: cat This will show the lengthy public key.
f. Highlight that public key by holding the left mouse button down, and dragging the cursor down across the displayed characters. There are a LOT of them. The very last will be your user name and the host on which you're working. Make sure that they are all highlighted, but nothing else.
g. At the top of the menu bar, click Edit-->Copy. This loads the character string into the CDE paste buffer
h. Next, move the cursor into the menu bar of the text editor. Click Edit-->Paste. This writes the paste buffer into the text file. At the end of the line, type a single <ENTER> so that the carriage returns to the far left.
i. Return to the text terminal session by clicking on its menu bar. Ensure that your home directory has appropriate permissions. Type cd ../.. which will move up two directories in the hierarchy. List the directory with ll. On the far left, the permissions should read drwxr-xr-x. If they don't, change them with chmod 755 <directory name>. The secure utilities will not work properly if these permissions are not correct.
j. Next, open a session on the next host on which you will use the secure utilities. Type ssh <hostname>. You will be challenged with: The authenticity of host '<hostname> (ip address)' can't be established. RSA key fingerprint is (long hexadecimal number). Are you sure you want to continue connecting (yes/no)? Answer yes.
k. You will next see Warning: Permanently added '(hostname) (IP address) ' (RSA) to the list of known hosts. You will then be challenged for your password, as normal.
l. Close the session on remote host, return to the standard terminal session on the local host. If you havve completed steps c-k (above) for all hosts, skip to step m below. Otherwise open a session on another remote host and begin again with step c, above. You will be creating a text file with the contents of the file from each host, separated by a carriage return.
m. When steps c-k have been done on all hosts, save the file in the text editor. Click on File-->Save As, and enter /(the path to your home directory/.ssh/authorized_keys. It is essential to save this file in this particular directory with this particular filename. Be sure that this path/filename is in the box near the bottom of the popup window.
n. Next, ensure that the authorized_keys file has the proper permissions. Return to the home session on the local system. Then change into the .ssh directory with cd ~/.ssh.
o. List the .ssh directory with ll. The authorized_keys files should show -rw-r--r-- on the far left. If not, change the permissions with chmod 644 authorized_keys.
p. Next copy the authorized_keys file into the .ssh directory of each remote host on which you ran steps c-k, with scp authorized_keys HOST:/(my home directory)/.ssh/ authorized_keys.
q. Lastly, open a session on each host that has the authorized_keys file, with ssh <hostname>. You will be challenged for a password, but if the previous steps have been done exactly, this will be the last time. You can check this by logging off (with exit or <ctrl><d>), then logging on again. You should not be challenged for a password. If so, check permissions as above before opening a help desk ticket.
r. You might want to open a session ON each host, TO each host. Again, you should only be challenged for a password the first time. Afterwards, you may ssh or scp securely without being prompted for a password.
s. If you are later granted access to another host, you must add the contents of its ~/.ssh/ file to each ~/.ssh/authorized_keys file.

Using Secure Copy

1. The syntax for secure copy is identical to remote copy.
a. To copy a file from the local host to a remote host, the syntax is scp (path/)filename $DESTINATION:(/path)/filename.
b. You can use scp indirectly, with scp $SOURCE:(/path/)filename $DESTINATION:(/path)/filename. This will copy a file between two hosts, neither of which must have a session open.

Using Secure Shell

1. The syntax for secure shell is similar to remote shell (rsh)
a. The command ssh $HOSTNAME opens a new session on a remote host.
b. The command ssh $HOSTNAME "command" executes the command on the remote host, and returns control to the local host. This is convenient for executing a single command remotely without opening a session on the remote host.

disabled smilies --oombera

Last edited by oombera; 02-18-2004 at 04:07 PM..

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #984
Difficulty: Medium
FreeBSD was the most popular open-source BSD operating system, accounting for more than three-quarters of all installed BSD systems.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Ssh script to validate ssh connection to multiple serves with status

Hi, I want to validate ssh connection one after one for multiple servers..... password less keys already setup but now i want to validate if ssh is working fine or not... I have .sh script like below and i have servers.txt contains all the list of servers #/bin/bash for host in $(cat... (3 Replies)
Discussion started by: sreeram4
3 Replies

2. Shell Programming and Scripting

SFTP or scp with password in a batch script without using SSH keys and expect script

Dear All, I have a requirement where I have to SFTP or SCP a file in a batch script. Unfortunately, the destination server setup is such that it doesn't allow for shell command line login. So, I am not able to set up SSH keys. My source server is having issues with Expect. So, unable to use... (5 Replies)
Discussion started by: ss112233
5 Replies

3. Shell Programming and Scripting

script for ssh

The below command executing manually from server01. It will ask the password and retrieves the result(total number of characters in a filename.txt file available in server02) (username@server01)$ ssh username@server02 wc -c /log/filename.txt username@server02's password: 25500... (1 Reply)
Discussion started by: subi.ut
1 Replies

4. Shell Programming and Scripting

ssh script

I am connecting to a remote server using this ssh command: ssh -p 2222 username@***.***.***.*** I then get a password prompt where I enter the password. I need to make an ssh connection using a script instead of doing it manually. How can I automate the connection process and make the... (2 Replies)
Discussion started by: locoroco
2 Replies

5. UNIX for Advanced & Expert Users

SSH using shell script terminates the script execution

Hello, I am writing a shell script in which i do ssh to remote server and count the number of files there and then exit. After the exit the shell script terminates which i believe is expected behavior. Can some one suggest me a way where even after the exit the script execution resumes. ... (2 Replies)
Discussion started by: manaankit
2 Replies

6. Shell Programming and Scripting

SSH Script

So, right now I'm trying to make an SSH script for my place of employment. This script, I want to go out to the server hostnames we have specified (in another file) and change a users account password. We use Kerberized telnet, so if telnet root hostname fails, I want it to use ssh usernamehostname... (1 Reply)
Discussion started by: nkitmitto
1 Replies

7. Shell Programming and Scripting

SSH through a script

I want to use ssh through a script without setting key pair in those two machines. I ll take the password as input from user and then run some commands on remote machine. Can someone help me on how to do it (1 Reply)
Discussion started by: vickylife
1 Replies

8. UNIX for Advanced & Expert Users

SSH script

Hello All, I have public keys authentication set up already on my system, to connect to another team's system. What I'm trying to do is to write a script that connects to the customer's box, changes to a certain directory, and then changes the permissions of the files in that directory. ssh... (4 Replies)
Discussion started by: Khoomfire
4 Replies

9. Shell Programming and Scripting

How can we ssh login with script?

Hi all, Normally, we give ip address to login with ssh like ssh And we can give login name to ssh like ssh -l root Then the shell asks the password, we enter the password and login to the system. While using the script file, we are not able to supply the password to... (5 Replies)
Discussion started by: pcsaji
5 Replies

10. UNIX for Dummies Questions & Answers

ssh in script

Hi ssh has now been set up to not prompt for a password. My problem is I have the script below that I wish. Firstly ssh onto another box and then run the rest of the script problem I am not sure of the syntax. #!/bin/ksh . ${ENV}/oracle_env.ksh ****I WANT TO SSH AT THIS POINT AND... (2 Replies)
Discussion started by: mrbnetbar
2 Replies

Featured Tech Videos