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.
2 Answers
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
-
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.– tom1989Jul 23, 2014 at 7:01
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