4

I need to run a batch to login to server, get in sudo su - username and run specific commands.

I tried below code:

putty username@servername -pw password -m myshell.sh

myshell.sh:

#!/bin/sh
sudo su - username
cd to particular folder
then tail a file

i am getting in sudo, but after that the script stucks until i logout.

BMW
  • 42,880
  • 12
  • 99
  • 116
Suraj Modi
  • 71
  • 1
  • 5
  • Instead if running every command as sudo (with the password), login as root and run all the commands without sudo. – Nir Alfasi Dec 07 '14 at 06:31
  • 1
    @Alfasin: who says he has ROOT access ? – tvCa Dec 07 '14 at 11:56
  • Unless there is a need to CD, it's better to TAIL the file, supplying the full path in the TAIL command. Suppose the directory does not exist, what error messages will you get on the screen ? He'll state the file is missing, but it may not be obviouse he looks for that in a another (wrong) directory. It's also possible a file exists in the wrong directory. – tvCa Dec 07 '14 at 11:59
  • @tvCa you're hair-splitting: so connect as the *user* that has permissions – Nir Alfasi Dec 07 '14 at 17:42
  • You said ROOT, now you say : any user. – tvCa Dec 07 '14 at 18:09
  • hi all, let me explain the problem again the username which I use to sudo is used by multiple users and I don't have the password for that user. But when I do sudo su - username it directly logs me in without prompting for password. Let me know if made myself clear. – Suraj Modi Dec 07 '14 at 19:03
  • Possible duplicate of [Pass commands as input to another command (su, ssh, sh, etc)](https://stackoverflow.com/questions/37586811/pass-commands-as-input-to-another-command-su-ssh-sh-etc) – tripleee Jul 19 '17 at 03:34

2 Answers2

7

You can use sh -c and then use semicolons between commands, I'd consider the solution suggested in the comments though, just have whole script run as sudo.

sudo sh -c "cd /tmp;pwd;cd /dev;pwd""
Adam
  • 35,919
  • 9
  • 100
  • 137
  • sh -c prompts for password which i dont have. i use "sudo su - username" and does not asks for password, then i can execute my commands. But running the script with sudo prompts for password. – Suraj Modi Dec 07 '14 at 07:07
  • @SurajModi: Which password is it asking for? ------ `sh -c` should not ask for any password and `sudo` asks for **your** password depending on the arguments used and the configuration in `/etc/sudoers`. – pabouk - Ukraine stay strong Aug 06 '15 at 08:47
2

updated my shell file with below command and it worked:

#!/bin/sh
sudo su - username << block
cd /; 
tail filename;
block

all the commands are to be written in block and separated by ";"

Suraj Modi
  • 71
  • 1
  • 5
  • Much cleaner and safer way would be to tell `sudo` to run the command(s) directly as the user `username` instead of running `su` as `root`. Additionally you do not need to use `cd`, just specify the full path of the file. According to your example it would be: `/filename`. ------ Here is the preferred command: `sudo -u username tail /filename`. Unfortunately the possibility to use `sudo` this way could be disabled in `/etc/sudoers`. ------ Note: In the script you do not need to separate commands by `;` when they are separated by newlines already. – pabouk - Ukraine stay strong Aug 06 '15 at 09:05