Find the answer to your Linux question:
Results 1 to 6 of 6
Hi, I would like to automatically execute a command on the remote shell when I login through SSH to the server. The main problem is that due to some technical ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jun 2006
    Posts
    3

    Question How to execute remote shell commands via SSH?


    Hi,

    I would like to automatically execute a command on the remote shell when I login through SSH to the server. The main problem is that due to some technical issues on the remote server (which it not going to be solved in near future), my home directory does get mounted automatically when I login. However it's possible to mount the home directory throught some manual commands. So I would like to have these commands executed automatically.

    I know that it's possible to invoke SSH with a command option, like ssh user@server COMMAND , however this closes the connection after executing the COMMAND, while I need an interactive shell.

    So is it (at all) possible to use SSH to login to a server and automatically execute some commands without losing the connection while not having access to .bashrc etc?

  2. #2
    Just Joined!
    Join Date
    Jun 2006
    Posts
    3
    BUMP

  3. #3
    Just Joined! vijay_kernel's Avatar
    Join Date
    Apr 2007
    Posts
    77
    Hi,


    Why it is closing the connection when you execute some commands and which commands are you trying to execute.

  4. $spacer_open
    $spacer_close
  5. #4
    Just Joined!
    Join Date
    Jun 2006
    Posts
    3
    Hi,

    I think closing the connection is the default behavior of SSH when used with a command. What I try to do is to execute a bash script where I put all home mounting things together with sourcing my .bashrc , so it looks like

    ssh user@server /somewhere/startup

    and startup is the bash script. However, when you try it, it executes it and closes the connection. I tried with any other command and it happens all the time (you can try it with ls for example). But I want to stay connected.

    Any help would be much appreciated.

  6. #5
    Just Joined!
    Join Date
    Aug 2008
    Posts
    2

    How to execute remote shell commands via SSH

    i noticed someone had a similar question and i did not see a conclusion. I am new to Linux and writing scripts. i need to open an SSH connection to a remote server and execute some commands. It can be any command. I would like the connection to stay open and not close. In my script below i tried the Top command. I can open the connection but any command fails. I got the script from Nixcraft. What am i missing?

    #!/usr/bin/expect -f
    # set Variables
    set password [lrange $argv 0 0]
    set ipaddr [lrange $argv 1 1]
    set username [lrange $argv 2 2]
    set timeout -1
    # now connect to remote UNIX box (ipaddr) with given script to execute
    spawn ssh root@123.456.789.123
    match_max 100000
    # Look for passwod prompt
    expect "*?assword:*"
    # Send password aka $password
    send -- "MyPassWord\r"
    # send blank line (\r) to make sure we get back to gui
    send -- "\r"
    top -b -n 1 | head -n 8
    expect eof

  7. #6
    Just Joined!
    Join Date
    Sep 2008
    Posts
    1

    Use ssh -t

    Hello,

    I had the same request, and a friend of mine who just has a degree in Linux Mastership gave me the answer:

    Simply do something like:

    ssh -t YOURHOST "bash --rcfile PATH_TO_RCFILE"


    bash will be executed on the remote host, and bash will execute the specfied RCFILE at startup, and connection will remain open. -t is to have the current terminal forwarded to the ssh session so that you have a real terminal.

    You can have some variant on the same kind, still use ssh -t. Like if screen is installed, you can do:

    ssh -t YOURHOST screen

    Hope it helps!

    ps: another master in linux told me this other solution: "ssh YOURHOST bash --rcfile YOUR_RC_FILE -i". But then you don't have a real terminal, and some stuff will not work correctly (like tab auto-completion).
    Last edited by fuujuhi; 09-09-2008 at 01:17 PM. Reason: Forgot quote around ssh command...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •