Shell script to login to ubuntu server and then process commands - shell

I have a shell script(.sh file) which logs in to ubuntu server using ssh command. Once logged in i want to do the following commands:
sudo -i
su web
I want to incorporate the above commands in the same .sh file. How do i do it? Thanks

Related

why Jenkins shell script hangs when i run sudo pm2 ls

I confess I am total newbie to Jenkins.
I have
Jenkins-tls
installed on my Mac for experimentation.
I have a remote server that I testing with.
My Jenkins script is ultra simple.
ssh to the remote machine
sudo pm2 ls
the last command just hangs
I run the same 2 commands from the command line and it all works perfectly.
FYI, I need sudo for pm2 since I need to be root to run pm2, without sudo, I get access denied.
Any thoughts?
I believe you make the invalid assumption that jenkins somehow "types" commands after starting ssh to the remote session's command shell. This is not what happens. Instead, it will wait for the ssh command to finish, and only then execute the next command sudo pm2 ls. This never happens, because the ssh session never terminates. You observe this as a "hang".
How to solve this?
If there's only a small number of commands, you can use ssh to run them with
ssh user#remote sudo mp2 ls
ssh user#remote command arg1 arg2
If this gets longer, why not place all commands in a remote script and just run it with
ssh user#remote /path/to/script

/etc/rc.local is missing from my headless ubuntu 18.04

I am currently hosting a minecraft server on ubuntu server 18.04 LTS. I have a .sh script to start the server's java file, and I would like to run it at startup so that the minecraft server starts when the physical server boots. I wished to do this via /etc/rc.local. However, I do not see rc.local in that location.
Is it in a different location for this version of ubuntu, or is there an entirely different method I should use to run this .sh at startup?
There is no "/etc/rc.local" file on Ubuntu 18.04, but you can create it.
Create the file with a text editor:
sudo nano /etc/rc.local
Paste the following lines and replace "COMMANDS" with the commands to be executed at system startup:
#!/bin/sh -e
COMMANDS
exit 0
Add the execute permission on the file:
chmod +x /etc/rc.local
Set a crontab for this
Make sure the file is executable:
chmod +x /path_to_you_file/your_file/file.sh
To edit crontab file:
crontab -e
Then add this:
#reboot /path_to_you_file/your_file/file.sh
rc.local is disabled by default.
Enable by using this command
sudo systemctl enable rc-local.service

sudo: command not found while using plink

Hi i have created a batch file (run.bat) that after execution connects me to UNIX server with help of plink. But issue starts from this point i have to execute a script after connection to my server the script contains a command sudo -l. After the execution i get the error as mentioned in subject can anyone help me on this issue ??
Batch File-:
"C:\Program Files\PuTTY" plink -ssh -pw Tos#12Ts w44dvftyw#caa1607UX009.wvd.abcd.net /opt/sieb/w44dvftyw/run.sh
Script file(run.sh) -:
#!/bin/bash
sudo -l
It says
sudo: command not found
But when i run my script normally on UNIX server it runs with no issues. What am i missing here to make it work this way please help.
Scripts such as ~/.profile or ~/.bash_profile responsible for setting up the current user's PATH are run only on login shells.
Running sh -c 'somescript' (as performed by ssh host 'somescript') is neither a login shell, nor an interactive shell; thus, it does not gain the benefit of such scripts.
This means that additions to the PATH (in your case, /usr/local/bin) may not be present with commands run in this way.
Among your options:
Pass the PATH you want as part of the command to remotely run. This might look like:
plink -ssh user#host "PATH=/bin:/usr/bin:/usr/local/bin /opt/sieb/w44dvftyw/run.sh"
Embed a working value in the script you're running:
#!/bin/bash
PATH=/bin:/usr/bin:/usr/local/bin
# ...put the rest of your script here.

Automating tasks after vagrant ssh

Wondering if it is possible to automatically run a script or execute a command ONLY after vagrant ssh into the box? I understand that Ansible can provide beforehand installation and set up. But it failed to allow doing things automatically after entering the machine.
I am currently create a file script.sh. The file will be provided to the vagrant via Ansible. After I vagrant ssh into the box, I do bash script.sh to run the script. Is there better way?
Any suggestion would be more appreciated.
Two ways to achieve this,
Say assume your script is in vagrant home directory like,
:~$/home/vagrant/test-me.sh
1) Run command along with ssh
1a) vagrant ssh -- -t '/home/vagrant/test-me.sh; /bin/bash'
**-OR-**
1b) vagrant ssh -c '/home/vagrant/test-me.sh; /bin/bash'
2) Append complete script path in ~/.bashrc file (this should be in vagrant home directory if you are login as user vagrant)
:~$echo '. /home/vagrant/test-me.sh' >> ~/.bashrc

Run a bash script on startup, before login as a user

I am trying to have my Ubuntu 12.04 LTS server run a bash script I have to start a Minecraft server on start up, prior to log in but as user minecraft. I can have it run as root by placing the following in /etc/rc.local
bash /path/to/script/script.sh
which runs the script as root, I have tried the following in /etc/rc.local
su -c `bash /path/to/script/script.sh` minecraft
but to no avail. Can anyone tell me what I am doing wrong or should be doing instead? The first line of my script is
#!/bin/bash
in case it is important. Thanks much!
Try
su minecraft -c '/bin/bash /path/to/script/script.sh &'
The user should be the first argument to su.
You should use quotes and not ticks for the command argument (-c)
You may want to consider using su -l minecraft to have the script run in an environment which would be similar to that if the user minecraft logged in directly.
Give this a shot and let me know if it works.

Resources