Hello,
I had to make an ssh connection to remote server from a bash shell script. I've summarized my research and I want to share it with you. I hope it will be useful to somebody or it will get improved.
Bash has an option, which is called "input redirection", I think. It is something similar to this:
This script will connect to MySQL, will run the two queries and will quit. In similar way we can make an ssh connection in bash script. Here is some example code:
This way you can execute anything you want on the remote server. I have the following errors, while running the script:
As far as I know this is known issue and it has been discussed in the forum. I haven't tried to fix it - it works for me.
Here is an example, using shell variables in the script:
In this case the value of variable TEST_VAR is acquired from the local environment. Ny local I mean the environment, where the script is executed. So on the remote machine we have executed the command "touch sample_file".
Here is another example showing how to use environment variables, declared on the remote server:
Let's assume that we have TEST_VAR set on the remote server. We have escaped the $ character in our script, so $TEST_VAR is treated as string, not as a variable. On the remote server we have executed "touch $TEST_VAR". and TEST_VAR's value is acquired from there.
And finally let's assume that we have to run some commands on two or more machines. It's very inconvenient to have them two or more times (for each session). In this case the following workaround can be used. A text file is created and the desired commands are saved there. Then we can use "`cat <filename>`" in the script and they will be executed on the server. :
and more convenient solution (thanks to ddreggors):
Don't forget that the file must contain only commands, written in the way we want them executed on the server. The file can't be a bash script - any variables and so on won't be parsed.
That's it. I'll be glad to hear any suggestions, corrections, critics or just opinions. Corrections about my English are also welcome - it's not my mother tongue.
No reason you have to use a "here script" with <<EOF, you can pipe commands directly to the standard input of ssh, for example using echo:
this shows a couple of local variables D and F1 (expanded locally when the local echo is executed), and a remote variable F2 (expands to nothing locally, but remotely expands to the assigned value). The commands produce this output:
The commands shown use the local variable D and remote variable F2, creating, using, and removing a file (/tmp/x1 on the remote machine).
If all of your variable expansions are constant, or only depend on remote things, you can use single quotes and avoid having to escape all the $ signs:
which yields
Once you start using this mechanism for anything complicated, you will quickly run into many quoting issues. Consider writing a script that generates code on the remote system, or a templated script that can be parameterized locally or remotely as needed, then executed remotely with a single call to ssh.
--
qneill
Hi - If iam logged on to server A, on 4 putty windows using SSH ... and out of these 4 logged-in sessions, in one of the sessions if i did SSH from server A to server B;
i would now have 4 putty windows sessions ... of which 3 are actively logged on to Server A while 1 putty window is actively... (2 Replies)
Hi,
I use OpenSSH to log on to a RH server but when I enter the password 2 session windows appear.
I only need one so can anyone advise where I can rectify this?
R,
D. (2 Replies)
Hello,
I am writing a script that has to log in to X number of servers over a full C class range.
I am only keyed on a certain number of these servers and this can change from time to time.
The part of my script that deals with this is
for i in $(cat $server_list); do
ssh ... (0 Replies)
Hey Guys,
I want to have a bash script on my computer (Mac OS X 10.6.8) that can ssh into my iPod and respring. I know how do this by typing in "ssh root@10.0.1.10" and then typing in the password "alpine". From there i simply type "respring". I want to possibly put this into a shell script so it... (0 Replies)
Hi I am working in Solaris 10 and I want to monitor logs for every telnet/ssh session that tries to connect to the server. I need these logs to be generated in a file that I can fetch using ftp.
I am a new user and a stepwise detail will be great
BR
saGGee (3 Replies)
Hi Forum.
First of all thanks for reading.
I need some help here. I have been checking some other posts about this issue, but I still have problems...
I'm trying to conect througn ssh to several machines to kill some process and I have problems with the " ' ' ".
This is what I'm trying... (2 Replies)
Hi All
Is it possible to log and monitor user activities in a SSH session. Is there any configuration or a patch to apply ?. Im currently using OpenSSH with AIX 5.3L
and my syslog.conf includes
auth.* /var/log/sshd/auth.log
and
in sshd_config it shows
SysLogFacility AUTH... (4 Replies)
hello
anyone have done ssh login script without "expect"
(automatic login from host A / user b to Host B / user b without enter any passwords)?
cheers (2 Replies)
hi.
I need a bash script which can login to an other mashin via SSH and then run some commands and then return the result to my mashine.
I dont know where to begin, I think first I will need a ssh connection, dont know how to make it,
then , do I need a ftp connection between the 2 mashins to... (5 Replies)