-1

I am new to Shell scripting. I want to login to a server using a script. The normal command I use is --> sudo ssh . when i tried putting this into a txt format file and tried running, it throw an error "can't execute". I am an Admin and i have root access. Any help would be highly appreciated.

1
  • Ok, My job is to login to source and destination server. I want to write a script where it should ask me to enter source server after i enter that it should login into that server. Same to destination server.
    – tom1989
    Jul 24, 2014 at 4:43

2 Answers 2

1

Add this to the top of your file so the shell knows which interpreter to use.

#! /bin/sh

Also make sure your file is executable:

chmod +x yourScript

There is also no need for sudo with ssh. Only use ssh

ssh user@machine

If you need to pass the Password I recommend sshpass

apt-get install sshpass
sshpass -p<password> ssh user@machine
1
  • when i tried with ssh user@machine it is asking for the password, then i sudo also, but no luck. I think i have to run this command as root. Please advise.
    – tom1989
    Jul 23, 2014 at 7:01
0

Create a key pair with ssh-keygen.To set up trust between the two users on the two machines, you need add the contents of ~/.ssh/id_rsa.pub on your machine to ~/.ssh/authorized_keys on the remotemachine and chmod it user only. You should be able to login without password now.

Then to use sudo remotely you need to configure something else in the sudoers file on that machine with visudo:

Defaults: user !requiretty

Not the answer you're looking for? Browse other questions tagged .