Home > Enterprise >  How to execute commands right after login using SSH connection? But after few seconds of login, it d
How to execute commands right after login using SSH connection? But after few seconds of login, it d

Time:09-18

`

#!/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?

CodePudding user response:

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 .

CodePudding user response:

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 cd /var/www/html/node_project/" && "npm install" && "node index.js"

  • Related