Change dir after 'sudo -i', in one command [closed] - linux

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 8 years ago.
Improve this question
Is there a way to switch dir after doing sudo -i (in one command)?
We use lots of ssh connections on work, and it is a pain to manually set them up when you reboot your pc. So I'm trying to make aliases like ssh remote-dev -t 'sudo -i; bash -l', which connects me to a machine and makes me root there (it is required to do that way; because of Kerberos we can't directly ssh root#...)
So what I would like to do now is expanding the above command in a way that I can also tell it to switch to a specific dir after the sudo, or open a specific file, or tail a logfile or something... Is that possible?
-- edit: Of course you could tell it to do some command everytime someone logs in via ssh. But this is a bad option, because only I want to have these commands to be executed; When other people connect to this machine, they probably want different commands.
-- edit: Sorry I posted it here, did not realize it is offtopic in stackoverflow

Try
sudo -i bash -c "cd /path/to/dir; exec bash"

Related

Using bash with screen in an alias [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 9 months ago.
Improve this question
I have a screen session that is always running in a separate user (for a minecraft server). Each time I want to attach to this screen, I use su -c 'screen -r mc-1.18.2' minecraft to attach back to this screen.
I wanted to make an alias for this command just so I didn't have to type it out or go up in the command history each time. I added alias mcconsole="su -c 'screen -r mc-1.18.2' minecraft" to my ~/.bash_aliases folder. My other aliases work just fine but when I run mcconsole I am left with a brand new screen session. I am not sure where the hiccup is when attaching to the screen session (which is running as I can attach to it normally).
maybe try to invert simple and double quotes, something like:
alias mcconsole='su -c "screen -r mc-1.18.2" minecraft'
or escape double quotes inside:
alias mcconsole="su -c \"screen -r mc-1.18.2\" minecraft"
Sounds like something screwy is happening with quoting when using the alias. Try using a shell function instead.
mcconsole() {
su -c 'screen -r mc-1.18.2' minecraft
}

How to programmatically trigger actions when last Linux terminal is exited/killed? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 2 years ago.
Improve this question
I've modified my .bashrc to mount directories when I start first terminal after login.
If mount point still exists when I start new terminal nothing is done.
Now I want to add a bit of code when last linux terminal is closed/exited (e.g. umount those directories, etc..)
Also there is this not-so-intelligent way of discovering how many terminal instances are still running:
ps -au | grep "bash" | grep "grep" -v -c
I'm running Ubuntu 20.04. and I'm using bash shell.
Questions:
Is there a file which is "triggered" on terminal exit just like .bashrc is on terminal startup? I've tried messing around with .bash_logout but it doesn't seem to do anything in my case (echo, touch..)
Is there another way to do what I'm trying to achieve which doesn't include file from question #1 (if such file even exists)?
You can get it done with the help of trap which is a shell builtin.
For example if you want to clear a folder on running exit command in bash >
trap "rm /cache/*" EXIT
The syntax should be like trap <command> <SIGNAL>
Just put this in the bottom of ~/.bashrc with your desired command and it should run before the terminal is killed.
Try trap --help to know more.

Codeship ssh commands [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 7 years ago.
Improve this question
According to the documentation (https://codeship.com/documentation/continuous-deployment/deployment-with-ftp-sftp-scp/#run-commands-on-a-remote-server-via-ssh), you need to run all commands as a single command.
While I understand that, I don't understand how I can run the following as a single command, the process would be:
ssh demo#111.1.111.101
ssh user#test-server
cd deploy
./deploy.sh
Not that the second command is run on 111.1.111.101 as demo
This is currently what I've tried:
ssh demo#111.1.111.101 "ssh user#test-server; cd deploy; ./deploy.sh"
But this seems to get stuck after the first ssh command, it never goes to the second ssh.
ssh -t demo#111.1.111.101 'ssh user#test-server "cd deploy; ./deploy.sh"'
Like user156213 said probably you dont need two jumps, but anyway you are adding a ; after the ssh that breaks everything
With one jump
ssh user#test-server "cd deploy ; ./deploy.sh"
also you can try to remove the first line
ssh user#test-server ./deploy/deploy.sh
Take a look at ProxyCommand in ssh to avoid the jumps, edit your .ssh/config to include something like that
Host first
Hostname 111.1.111.101
User demo
Host second
User user
ProxyCommand ssh -q first nc test-server
Now with a simple
ssh second deploy/deploy.sh
will do the work

Automate SSH login and programmatically induce remote, interactive shell [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 8 years ago.
Improve this question
I'm working from Mac OSX and am trying to create a script that will ssh user#host and subsequently change the working directory (cd) from within the virtual server.
Googling indicated that ssh -t user#host "command goes here" would allow for this, but when I enter cd /my/path it says that there is no such directory. It also forces me to exit the server. What is the best, simplest way to accomplish this?
I'm assuming that the desired end result is a remote interactive shell, in the desired target directory, which you can interact with from your keyboard. If not, please clarify the question.
Force a TTY with the -t argument to ssh and spawn an interactive shell with an explicit invocation of bash -i:
ssh -t user#host "cd /my/path && exec bash -i"
Scripting/automating the authentication is road already well-traveled (and, thus, duplicative); see existing questions such as Bash: controlling SSH for assistance on that matter.

run same command on multiple tabs of a terminal gnome-terminal [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 9 years ago.
Improve this question
Every day I run same ssh(succeeded by entering password) on 3 or more terminals. Is there any way to run same command parallely on all the terminal tabs opened on gnome-terminal. It can be ssh or cd any any other command. I know this feature is there in Konsole(from KDE) but not in gnome-terminal.
Thanks
To open tabs in gnome-terminal with a predefined commands (i.e. ssh server1, ssh server2, ...) you can use a command
gnome-terminal --tab -e "ssh server1" --tab -e "ssh server2"
And, it was previously mentioned, you should use key authentication to not to enter your passwords every time, but if you can't, change your ssh command to
sshpass -p 'mysecretpass' ssh server1
In addition to this, you can create a script file with the sshpass command and pass it with the -e option while creating the terminal.. this would further simplify things.
Terminator terminal supports the function you are looking for. You can find more information here. I am using this program every day. It is a very useful piece of software.

Resources