Need to ssh and run a single command as part of a larger bash script - linux

My script is basically a convenient way to do remote shutdown and wakeonlan. The script runs from my Raspberry Pi
Shutting down my desktop is easy, but when I shut down my home server, I first would like to turn off my seedbox virtual machine (which is running on my home server) by connecting to it via SSH and issuing "shutdown -h now"
TIMESTAMP=`date "+%Y-%m-%d %H:%M:%S"`
echo "$TIMESTAMP: shutting down seedbox virtual machine" >> ${LOG_FILE}
sshpass -p " " ssh -o StrictHostKeyChecking=no lukesau#192.168.1.27
sleep 5
sudo shutdown -h now
sleep 60
TIMESTAMP=`date "+%Y-%m-%d %H:%M:%S"`
echo "$TIMESTAMP: shutting down $TARGET" >> ${LOG_FILE}
net rpc shutdown -f -t 5 -C 'Shutting down in 5 seconds' -U lukesau%pass -I 192.168.1.99
The seedbox is a Mac OS X virtual machine running in VMware. Perhaps there is a better way to shut down a VM before shutting down the host OS

That's not how it works, ssh isn't going to read the following commands from your input file. You can specify the command to be executed right after ssh
sshpass -p " " ssh -o StrictHostKeyChecking=no lukesau#192.168.1.27 \
'sh -c "sleep 5 ; sudo shutdown -h now ; sleep 60"'
This is assuming that you want these 3 commands executed via the ssh and the rest "locally" again.
I don't take guarantee for the quoting, nor for you sshpass construction as I have no manual for that.

Related

Multi Hop, Self Closing, Background SSH Tunnel

I'm trying to create a multi hop ssh tunnel, that is in the background, and self closing. What I have is...
ssh -L 9999:localhost:9999 user#host1 -t ssh -L 9999:localhost:27017 -N
-i ./mypem.pem user#host2
This successfully creates a multi hop ssh tunnel that closes the port on host1 after I close the tunnel. However, if I try to fork it to the background, like this...
ssh -f -L 9999:localhost:9999 user#host1 -t ssh -L 9999:localhost:27017
-N -i ./mypem.pem user#host2
It runs in the background, but killing the process locally does open the port back on host 1.
Any ideas for a self closing, multi hop, background ssh tunnel? Thanks!
For self-closing, one can use a simple sleep X command.
$ ssh -f -L 9999:localhost:9999 user#host1 \
-t 'ssh -L 9999:localhost:27017 -i ./mypem.pem user#host2 "sleep 3600"'
Note, this doesn't use the -N option, which is for not running a command, instead, we run a command sleep 3600. This keeps the tunnel up for 1 hour and closes itself after that.

getting error tput: No value for $TERM and no -T specified when trying to connect from jenkins to jmeter servers

i am using distributed framework for performance testing where i have 1 controller jmeter machine and 2 jmeter machines are working as load generators.I am trying to connect all the machines from jenkins server and executing shell script on controller machine to execute the test.Setup was working fine but from last couple of days i start getting following error
tput: No value for $TERM and no -T specified
tput: No value for $TERM and no -T specified
Below are the shell scripts, i am executing from jenkins server
#!/bin/bash
USERNAME=jmeter
HOSTS="jmeter01.com jmeter02.com"
#SCRIPT="source .bash_profile; alias ; pwd"
for HOSTNAME in ${HOSTS} ; do
echo "Connecting with host name : ${HOSTNAME}"
ssh -T -o StrictHostKeyChecking=no ${USERNAME}#${HOSTNAME} "${SCRIPT}"
echo "ssh connection successful with ${HOSTNAME} "
done
try 't' instead of 'T'
ssh -t -o StrictHostKeyChecking=no ${USERNAME}#${HOSTNAME} "${SCRIPT}"

SSH: Execute a command and keep the shell open [duplicate]

I find myself needing to log into various servers, set environment variables, and then work interactively.
e.g.
$ ssh anvil
jla#anvil$ export V=hello
jla#anvil$ export W=world
jla#anvil$ echo $V $W
hello world
How can I combine the first few commands, and then leave myself at a prompt?
Something like:
$ ssh anvil --on-login 'export V=hello; export W=world;'
jla#anvil$ echo $V $W
hello world
Obviously this is a model problem. What I am really asking is 'how do I ssh to a different machine, run some commands, and then continue as if I'd run them by hand?'
Probably the simplest thing is:
$ ssh -t host 'cmd1; cmd2; sh -i'
If you want to set variables, do:
$ ssh -t host 'cmd1; cmd2; FOO=hello sh -i'
Note that this is a terrible hack, and you would be much better off putting your desired initial commands in a script and doing:
$ scp setup host:~
$ ssh host
host$ . setup
You could also use the following expect script:
#!/usr/bin/expect -f
spawn ssh $argv
send "export V=hello\n"
send "export W=world\n"
send "echo \$V \$W\n"
interact
Turns out this is answered by this question:
How can I ssh directly to a particular directory?
to ssh:
ssh -t anvil "export V=hello; export W=world; bash"
followed by:
jla#anvil$ echo $V $W
hello world
It is worth to note that ssh -t can actually be used to connect to one host via another host.
So for example if you want to execute a command on anvil, but anvil is only accessible from host gateway (by firewall etc.), you can do like this:
ssh gateway -t 'ssh anvil -t "export V=hello; export W=world;bash -l";'
Exiting the anvil, will also log you out of gateway (if you want to stay on gatway after leaving anvil than just add another bash -l before closing the command.
Another approach is to execute this beast (also gives me a colored shell):
ssh host -t "echo 'rm /tmp/initfile; source ~/.bashrc; cd foo/; git status' > /tmp/initfile; bash --init-file /tmp/initfile"

How to avoid extra shell when ssh -f?

If I do
ssh -f 10.10.47.47 "/opt/omni/bin/mbuffer -4 -v 0 -q -I 8024 | /usr/sbin/zfs receive tank/test"
then I have to press CTRL-D to exit an extra shell that have been spawned on the Linux host, where I ran the ssh command.
If I do
ssh -f 10.10.47.47 "sleep 10s"
and then try to CTRL-D than the Linux host hangs until the sleep command exits on the remote host. Very weird behaviour, which I wouldn't expect since -f was used.
Question
Is it possible to avoid this extra shell on the host where the ssh command is executed?

How to know PID of the process ran by remote ssh

If I run a process at the remote site by using ssh as follows:
nohup ssh remote sleep 100 &
Is there a way to know the PID of sleep at remote?
Trying echo $! just returns the PID of ssh at local.
And, greping won't work since there are multiple sleep processes at remote.
You could pass a string to ssh so try
nohup ssh remote 'sleep 100 &; echo $!'
Try the following
nohup ssh remote 'sleep 100 > out 2> err < /dev/null & echo $!'

Resources