Starting tmux on Google cloud - python-3.x

Goals: I am trying to create a startup script on google which run tmux and also running python3 script on the tmux after start/reset the VM (virtual machine). My python script was a telegram bot script which response to the chat.
My current condition: I just know that startup script was run as a root so I run it using sudo -H -u USERNAME tmux new-session -d -s SESSION-NAME. It runs perfectly on startup script and the session was there when I make SSH connection to the machine. I am using debian 10 as OS and already install both python3 and tmux.
Problem: I want to run python3 code in the tmux session, so I add "python3 XXXX.py" at the back of the startup script. but it doesn't work as it should be. when I make SSH connection the session was not there. when I check the startup script - it was said that it was run perfectly but I cannot find my session and the script also did not run.
My current code:
#! bin/bash
sudo -H -u USERNAME tmux new-session -d -s SESSION-NAME "python3 SCRIPT.py"

Related

ec2 run-instances user-data script - changing user from root to ubuntu

I am using aws cli to launch ec2 instances. I am using the user-data parameter to run a custom script when they are launched:
aws ec2 run-instances \
......
--user-data file://~/Desktop/script.sh
In script.sh i can see that it is running it user root user
for example when i run
whoami > testwhoami.txt
i can see root in the text file
I need to switch the user to ubntu and it doesn't seem to work.
I have tried many things in this script:
sudo su -l ubuntu
su -l ubuntu &> output.txt
but after all these whoami keeps showing that root is the user and output.txt is empty as well
What might cause this? How can i debug it properly?
Your command su -l ubuntu returns an interactive shell, which is useless when you're running a script (EC2 user-data).
You need to submit your command using su -c option like this:
su ubuntu -c 'whoami' > output.txt

execution of remote script containing "sudo su" through ssh [duplicate]

This question already has answers here:
Pass commands as input to another command (su, ssh, sh, etc)
(3 answers)
Closed 1 year ago.
I need to run a script which needs to be run with root privileges remotely. Therefore I add "sudo su" command at the start of the script. However the ssh just login the remote server and stuck at sudo su command, and it does not continue from next line in the script.
server.sh
sudo -s
sudo apt-get update
sudo apt-get upgrade
client.sh
scp -i "$key.pem" server.sh "$dns:/tmp"
ssh -tt -i "$key.pem" $dns "bash /tmp/server.sh"
server.sh and client.sh is at the same local directory. When I run ./client.sh, server.sh which is run remotely stuck at first line and does not continue with "sudo apt-get update" command. What is the reason of this behavious and is there a solution?
When you run the command sudo -s you change the user and the rest of the script is lost because it is in a new shell.
Remove the line sudo -s and try running the script again.
Note: it is important to remember that the user running sudo must be in the /etc/sudoers file with the username ALL=(ALL) NOPASSWD:ALL permissions.
sudo -s with no command starts a new, interactive shell. The following commands won't execute until it exits. See man sudo.
If you are already running apt-get via sudo, and sudo does not require a password, why do you need the sudo -s?
You can use
ssh user#ip '[command]'
to run [command] on the remote host. If you have a user with root privileges (aka. sudo) and if you can use commands without passwords (NOPASSWD:[command,list or ALL]) this is the safest way i can suggest however if you want the script to run on the remote server and triggered by the local computer you can always
ssh user#ip 'sudo /bin/bash /home/[user]/server.sh'
This would work as well. You can also use "scp" command to copy the script and then delete it with ssh again for automated one-script approach.

Not able to login in server via sshpass and attach to a tmux session through bash script

I am trying to login in the remote server and attach to a tmux session which is already running in server via script but it always show me:
open terminal failed: not a terminal
Bash Script:
#!/bin/bash
sshpass -p 'password' ssh username#host "tmux new-session -d -s my_session 'sudo tailf /opt/log/debug.log';tmux attach -t my_session"
Anyone please help me how to open any running file automatically after login because through this method it automatically logs out from remote server.

cd to directory and su to particular user on remote server in script

I have some tasks to do on a remote Ubuntu CLI-only server in our offices every 2 weeks. I usually type the commands one by one, but I am trying to find a way (write a script maybe?) to decrease the time I spend in repeating those first steps.
Here is what I do:
ssh my_username#my_local_server
# asks for my_username password
cd /path/to/particular/folder
su particular_user_on_local_server
# asks for particular_user_on_local_server password
And then I can do my tasks (run some Ruby script on Rails applications, copy/remove files, restart services, etc.)
I am trying to find a way to do this in a one-step script/command:
"ssh connect then cd to directory then su to this user"
I tried to use the following:
ssh username#server 'cd /some/path/to/folder ; su other_user'
# => does not keep my connection open to the server, just execute my `cd`
# and then tells me `su: must be run from terminal`
ssh username#server 'cd /some/path/to/folder ; bash ; su other_user'
# => keeps my connection open to the server but doesn't switch to user
# and I don't see the usual `username:~/current/folder` prefix in the CLI
Is there a way to open a terminal (keep connection) on a remote server via ssh and change directory + switch to particular in a automated way? (to make things harder, I'm using Yakuake)
You can force allocation of a pseudo-terminal with -t, change to the desired directory and then replace the shell with one where you are the desired user:
ssh -t username#server 'cd /some/path/to/folder && exec bash -c "su other_user"'
sudo -H keeps the current working directory, so you could do:
ssh -t login_user#host.com 'cd /path/to/dir/; sudo -H -u other_user bash'
The -t parameter of ssh is needed otherwise the second sudo won't be able to ask you for your password.

"Sudo" fails with "sudo requires a tty" when executed from PuTTY command line

I'm trying to run some commands on a remote CentOS machine using PuTTY. I'm using the following command:
putty.exe -ssh [IP] -l [user] -pw [password] -m [Script]
Where [Script] is a .txt file containing the commands I want to run. The issue is that one of the commands requires sudo, and when PuTTY tries to run it I get an error:
sudo requires a tty
The thing that's confusing me is that if I start the session without giving a script, then run the commands from the script manually, it works fine. I've tried using -load instead of -ssh, and it made no difference.
I can't change the requiretty setting in my sudoers file for security reasons, which is the only solution I've been able to find. Is there another option?
The sudo requires TTY/interactive session.
On the contrary the PuTTY/Plink -m switch uses non-interactive session by default.
Use the -t switch to override that.
putty.exe -ssh [IP] -l [user] -pw [password] -t -m [Script]
Read the error: sudo requires a tty. That is, an interactive shell. You have to find an other way of doing those privileged instructions. For example, you could login as root with a key-based authentication.

Resources