How to execute commands right after login using SSH connection? But after few seconds of login, it disconnect me from remote server - bash

#!/bin/bash
expect <<END
spawn ssh -o user#remote_ip
expect "password"
send "my_pass\r"
expect eof
END
cd /var/www/html/node_project/
##npm init -y
npm install
##node index.js
I want run some commands right after login into remote server. I can login successfully, but 2 or 3 seconds later, it automatically logout me from the remote server. How can I make it wait until all of my commands run and execute successfully?

Use this:
sshpass -p "PASSWORD" ssh -o StrictHostKeyChecking=accept-new USERNAME#IP_ADDRESS "sleep 10 ; ls -l"
or this (for connection timeout):
sshpass -p "PASSWORD" ssh -o StrictHostKeyChecking=accept-new USERNAME#IP_ADDRESS 'nohup bash -c "sleep 3; ls -l"'
Note: replace ls -l with your command.

Here is my solution: I have run multiple commands in line and it executes all the commands one after another.
spawn ssh user#remote_ip "cd /var/www/html/node_project/" && "npm install" && "node index.js"

Related

Running multiple remote commands via a single ssh command

sshpass -p Password ssh -o StrictHostKeyChecking=no a.user#IP "Command1 ; Command2"
When i try to run multiple commands after ssh it only runs the 1st command and exits after that without executing others.
I am running the above command,
Could I improve on it to run all the commands sequentially?

Remote SSH run local script containing sudo command, -T is not working

I try to run a local script using ssh
ssh remotehost 'bash -s' < test.sh
test.sh is
#!/bin/bash
echo "Start Script"
sudo echo "test"
echo "End Script"
Also, in remote host, sudo logging is enabled in /etc/sudoers
Defaults logfile="/var/log/sudo.log"
Defaults log_input,log_output
After this, the output of ssh remotehost 'bash -s' < test.sh is
Start Script
test
Script got terminated after executing the first sudo command. But I need to get
Start Script
test
End Script
I also tried ssh -T remotehost 'bash -s' < test.sh, unfortunately it's not working.
What's the prope way to do this? Thanks

Run bash script on remote server

I'm trying to run a bash script on the remote server that is already on the remote server. I'm using ssh pass to do it but I'm seeing errors
test.sh (resides on the remote server)
#!/usr/bin/env bash
echo "This is test"
adb start-server
sshpass command (I'm running this sshpass command from docker ubuntu image
sshpass -p password ssh -oStrictHostKeyChecking=no -oCheckHostIP=no user#host "bash -s" < /Users/user/Documents/workspace/test.sh
I also tried
sshpass -p password ssh -oStrictHostKeyChecking=no -oCheckHostIP=no user#host 'cd /Users/user/Documents/workspace/; sh test.sh'
I get this error message
bash: /Users/user/Documents/workspace/test.sh: No such file or directory
The examples you're showing are for a local script, and you said it's a remote script.
sshpass -p password ssh -oStrictHostKeyChecking=no -oCheckHostIP=no user#host "bash /path/to/test.sh"
that ought to do it.
you can try to find your test.sh on the remote computer:
sshpass -p password ssh -oStrictHostKeyChecking=no -oCheckHostIP=no user#host "find ~/ -name \"test.sh\""
Try with here-document:
sshpass -p password ssh -oStrictHostKeyChecking=no -oCheckHostIP=no -T user#host <<EOF
bash /Users/user/Documents/workspace/test.sh
EOF
Include -T option for ssh command, as mentioned above, to disable pseudo-tty.
[AT REMOTE MATCHINE] Ensure that path of adb executable is included in PATH environment variable. Else, specify it with absolute path in the Shell script.

The second command doesn't run as root

I have created a Jenkins job today, what it does is the Jenkins user should log into another server and run two commands separated by &&:
ssh -i /creds/jenkins jenkins#servername.com "sh -c 'sudo su && lxc exec containername bash'"
The logging part works fine, then it runs the sudo su command and becomes root but it never runs the second command.
I even did this manually and from the Jenkins machine logged into the other server (servername). Then ran sh -c "sudo su && lxc exec containername bash" with no luck.
Try to execute the second command as a parameter for the sudo su command, like this:
ssh -i /creds/jenkins jenkins#servername.com "sh -c 'sudo su -c "lxc exec containername bash"'"

How to run remote script with sudo access

I am trying to run a remote script which need to run as a sudo but its unable to run the script as sudo. Is my approach is correct to do sudo? Please help..Thanks in advance.
spawn ssh -t {*}$ssh_opts $user#$ip bash $script {*}$argv
Try this :-
#!/usr/bin/expect
set timeout -1
spawn -noecho bash -c "ssh user#host \"sudo su -c 'your_command' - sudo_user\""
expect {
-re ".*assword:"{
send "password\r"
exp_continue
} eof {
wait
}
}
Hope it helps.!!!

Resources