LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 08-27-2007, 06:58 AM   #1
sefaklc
LQ Newbie
 
Registered: Aug 2007
Location: ankara(turkey)
Distribution: ubuntu,red hat
Posts: 28

Rep: Reputation: 15
shell script for remote ssh login


hello all.
i am very new to write scripts.And i have a question.
How can i execute a script for remote ssh connection?
Code:
#!/bin/ksh

ssh 192.168.182.132
df -k 
pwd
echo $PATH
it works when i write them to terminal line by line ,but not with a script.
can you explain how can i do this? or some source for learning?

thank you
 
Old 08-27-2007, 07:55 AM   #2
al2
Member
 
Registered: Mar 2007
Location: Sweden
Distribution: Slackware
Posts: 34

Rep: Reputation: 16
Are you using korn shell or bourne again shell? In case of the latter the first line in the script should be
Code:
#!/bin/bash
Make sure you have the rights to execute the script. In the terminal type
Code:
sudo chmode u+x filename
where filename is the name of your script file.

Last edited by al2; 08-27-2007 at 08:18 AM.
 
Old 08-27-2007, 08:30 AM   #3
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,453

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
To run commands on the remote machine, you have to give commands in input to ssh. For simple commands you can use the syntax
Code:
ssh user@hostname <command>
where command is a string (better enclose in double quotes) containing the command you want to execute. Or you can use a "here document", e.g.
Code:
ssh user@hostname << LimitString
<command>
<command>
<command>
LimitString
where the commands placed after the ssh line and before the "LimitString" are executed on the remote machine. Actually you can use any string in place of "LimitString". You may also consider "private/public key authentication" to avoid typing password.
 
Old 08-28-2007, 06:58 AM   #4
sefaklc
LQ Newbie
 
Registered: Aug 2007
Location: ankara(turkey)
Distribution: ubuntu,red hat
Posts: 28

Original Poster
Rep: Reputation: 15
when i try
Code:
 
16 ssh  $ipaddress <<LimitString
 17 while (($i < ${#command[*]})) ; do
 18     ${command[$i]}  >>$outputfile || print "${command[$i]} :command not found\n">>$outputfile
 19     i=$i+1
 20     printf "****************************************\n" >>$outputfile
 21     done 
 22 exit
 23 LimitString
it gives me the error
Code:
Pseudo-terminal will not be allocated because stdin is not a terminal.
and it is waiting.
 
Old 08-28-2007, 07:21 AM   #5
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,453

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
The error message is actually a warning. You can use option -T to disable pseudo-terminal allocation on the remote server
Code:
ssh -T $ipaddress << LimitString
Most likely it hangs for other reasons, depending by the commands you're trying to execute on the remote machine. Just a note:
Code:
exit
LimitString
The exit statement is not necessary here: the ssh session will terminate as soon as all commands are executed.
 
Old 08-28-2007, 09:03 PM   #6
choogendyk
Senior Member
 
Registered: Aug 2007
Location: Massachusetts, USA
Distribution: Solaris 9 & 10, Mac OS X, Ubuntu Server
Posts: 1,189

Rep: Reputation: 105Reputation: 105
#!/bin/ksh is fine. I do most of my work in ksh. I realize a lot of people like bash, and there's nothing wrong with that either. They are related to one another.

As far as getting the commands to run on the remote server, it is true that the tty issue with a script can be a problem. I have been successfully doing this sort of thing for quite some time with the following sort of construct in scripts (just an example):

su - backup -c "ssh ${TAPESERVER} \"dd obs=64b of=${TAPDEV}\"";

In that example, the first set of quotes encloses the command which is to be executed by the backup user within the su. The second set of quotes inside which is made literal with the \ encloses the command that is being passed through the ssh to be executed on the remote machine. In this case this whole construct was following a pipe carrying the output of a ufsdump to the remote tape drive through ssh. I had ssh keys set up for the backup user so that this wouldn't get hijacked by the requirement to input a password. The entire script was being run under root, but I didn't want root keys between the servers. The backup user only had access to the tape drive.

In your example, you could do something like:

ssh 192.168.182.132 "df -k; pwd; echo $PATH";

and that should work straight out assuming that the user under whom you issue that line has keys set up for the connection.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
auto-login script for connecting to remote m/n by using telnet/ftp/ssh Uday123 Programming 5 10-12-2011 11:31 AM
ssh remote login sKAApGIF Linux - Networking 4 11-22-2006 07:13 AM
exacuting remote comands over SSH with a shell script? dasy2k1 Linux - Software 1 09-05-2006 07:01 PM
something wrong with my shell on ssh to remote system stardotstar Linux - Software 1 08-03-2006 04:11 AM
shutdown xserver via remote shell (ssh) ? timbaloo Linux - Newbie 1 07-07-2004 04:33 PM


All times are GMT -5. The time now is 02:35 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration