Welcome to Linux Forums! With a comprehensive Linux Forum, information on various types of Linux software and many Linux Reviews articles, we have all the knowledge you need a click away, or accessible via our knowledgeable members.
Find the answer to your Linux question:
New to Linux Forums? Register here for free!
    Linux Forums > GNU Linux Zone > Linux Networking > How to execute remote shell commands via SSH?

Forgot Password?
 Linux Networking   Hardware/Software related, Modems, Internet connection sharing, IPTables etc.

Site Navigation
Linux Articles
Linux Forums
Linux Downloads
Linux Hosting
Free Magazines
Job Board
IRC Chat
RSS Feeds


Linux Forum Topics
Linux Forums
Your Distro
Linux Resources
GNU Linux Zone
The Community
Reply
 
Thread Tools Display Modes
Old 09-07-2007   #1 (permalink)
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?
Ymer is offline  


Reply With Quote
Old 09-13-2007   #2 (permalink)
Just Joined!
 
Join Date: Jun 2006
Posts: 3
BUMP
Ymer is offline   Reply With Quote
Old 09-13-2007   #3 (permalink)
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.
vijay_kernel is offline   Reply With Quote
Old 09-13-2007   #4 (permalink)
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.
Ymer is offline   Reply With Quote
Old 08-27-2008   #5 (permalink)
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
MeeToo is offline   Reply With Quote
Old 09-09-2008   #6 (permalink)
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...
fuujuhi is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Free Magazines
Run Your Own Web Server Using Linux & Apache - Free 191 Page Preview
Learn about everything you'll need to build and maintain your Linux servers, and to deploy Web applications to them.
subscribe
Open Source Security Myths Dispelled
Dispel the five major myths surrounding Open Source Security and gain the tools necessary to make a truly informed decision for your IT organization
subscribe
InformationWeek
InformationWeek is the only newsweekly you'll need to stay on top of the latest developments in information technology.
subscribe



All times are GMT. The time now is 03:19 AM.






© 2000 - 2009 - All Rights Reserved - Property of  MAS Media

Content Relevant URLs by vBSEO 3.3.0 RC2