linux execute command remotely
I guess ssh
is the best secured way for this, for example :
ssh -OPTIONS -p SSH_PORT user@remote_server "remote_command1; remote_command2; remote_script.sh"
where the OPTIONS have to be deployed according to your specific needs (for example, binding to ipv4 only) and your remote command could be starting your tomcat daemon.
Note:
If you do not want to be prompt at every ssh run, please also have a look to ssh-agent, and optionally to keychain
if your system allows it. Key is... to understand the ssh keys exchange process. Please take a careful look to ssh_config (i.e. the ssh client config file) and sshd_config (i.e. the ssh server config file). Configuration filenames depend on your system, anyway you'll find them somewhere like /etc/sshd_config
. Ideally, pls do not run ssh as root obviously but as a specific user on both sides, servers and client.
Some extra docs over the source project main pages :
ssh and ssh-agent
man ssh
http://www.snailbook.com/index.html
https://help.ubuntu.com/community/SSH/OpenSSH/Configuring
keychain
http://www.gentoo.org/doc/en/keychain-guide.xml
an older tuto in French (by myself :-) but might be useful too :
http://hornetbzz.developpez.com/tutoriels/debian/ssh/keychain/
How to execute linux commands in a remote machine with shell script
Only ssh
command can do this:
$ for ip in 192.168.138.22{1,2,3}; do ssh ${ip} -o StrictHostKeyChecking=no "free -h"; done
total used free shared buff/cache available
Mem: 7.8G 782M 5.2G 152M 1.8G 6.4G
Swap: 7.9G 0B 7.9G
total used free shared buff/cache available
Mem: 7.8G 1.3G 4.1G 169M 2.5G 5.8G
Swap: 7.9G 0B 7.9G
total used free shared buff/cache available
Mem: 7.8G 563M 5.9G 118M 1.4G 6.6G
Swap: 7.9G 0B 7.9G
However, this need to authenticate ssh-key
with each other. So, we can use sshpass to pass the passqord
:
$ for ip in 192.168.138.22{1,2,3}; do sshpass -p PASSWORD ssh ${ip} -o StrictHostKeyChecking=no "free -h"; done
total used free shared buff/cache available
Mem: 7.8G 782M 5.2G 152M 1.8G 6.4G
Swap: 7.9G 0B 7.9G
total used free shared buff/cache available
Mem: 7.8G 1.3G 4.1G 169M 2.5G 5.8G
Swap: 7.9G 0B 7.9G
total used free shared buff/cache available
Mem: 7.8G 563M 5.9G 118M 1.4G 6.6G
Swap: 7.9G 0B 7.9G
SSH remote machine and execute command
Let's use set -x
to debug what we actually run:
$ set -x
$ ssh localhost echo $PATH
+ ssh localhost echo /usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games
/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games
The line with the +
tells us that the command we actually run is:
ssh localhost echo /usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games
Unsurprisingly, this is also the value we get back, regardless of what the remote PATH
is.
We can single quote the command to ensure that we send echo $PATH
instead of echo /usr/local/bin:...
to the server:
$ ssh localhost 'echo $PATH'
+ ssh localhost 'echo $PATH'
Now set -x
shows that ssh
is being run with the unexpanded command instead of the expanded command, and we get the remote PATH
in return.
Run a command on remote machine and store its output in variable on remote machine
TL;DR: Use <<"EOF"
instead of <<EOF
.
Your Here-Document will expand all variables and evaluate all subshells before the script is even sent to your ssh server.
Consider the following script:
ssh user@servername <<EOF
echo "$(hostname)"
EOF
This will not print servername
(the name of the computer you are connecting to) but the name of your localhost instead (the name of the computer you working on).
Before ssh
is executed, the subshell $(hostname)
is executed. The resulting string "echo localhostname
" is then passed to ssh
and executed on the remote server.
To fix the problem you have to escape the $
inside the Here-Document or use a literal Here-Document:
ssh user@servername <<"EOF"
echo "$(hostname)"
EOF
Execute a remote command over SSH with remote evaluation
You need to escape all the characters that need to be interpreted by the remote shell like so:
ssh -A username@ip "sudo docker exec -it \"\$(docker ps | grep 'some' | awk '{ print \$1 }')\" python manage.py shell"
This way you will send the quotes belonging to the -it argument, as well as the $ sign unchanged and the remote shell will execute them.
Bash script how to run a command remotely and then exit the remote terminal
When you run a command in background on a terminal, regardless of weather it be local or remotely, if you attempt to logout most systems will warn you have running jobs. One further attempt to logout and your jobs get killed as you exit.
In order to avoid this you need to detach your running jobs from terminal.
if job is already running you can
disown -h <jobspec ar reported by jobs>
If you want to run something in background and then exit leaving it running you can use nohup
nohup command &
This is certainly ok on init systems ... not sure if it works exactly like this on systems that use systemd.
Execute bash script on a remote-linux
Use the full path to start ads2. When using remote SSH your environment variables may be different than in a local shell.
#!/bin/bash
echo "khaled"
/home/nvidia/ads2 svcd&
Not sure where ads2 is located.
Try the following to locate it on your Ubuntu local shell.
command -v ads2
You may also need nohup to persist the process beyond the life of the SSH session.
nodejs execute command on remote linux server
I solved the problem myself. There is one npm package (ssh-exec) available for ssh command execution. Below is the code I used.
var exec = require('ssh-exec')
var v_host = 'XX.XX.XX.XXX'
exec('ls -lh', {
user: 'root',
host: 'XX.XX.XX.XXX',
password: 'password'
}).pipe(process.stdout , function (err, data) {
if ( err ) { console.log(v_host); console.log(err); }
console.log(data)
})
Related Topics
Portable Way to Get File Size (In Bytes) in the Shell
Linux Pipe Audio File to Microphone Input
Maximum Number of Files/Directories on Linux
How to Find Files That Only Have Certain Permission for Owner
Provide Password to Ssh Command Inside Bash Script, Without The Usage of Public Keys and Expect
How to Capture Still Image from Webcam on Linux
Difference Between Virtual Page and Page Frame
Linux Os: /Proc/[Pid]/Smaps Vs /Proc/[Pid]/Statm
Why Doesn't Perf Report Cache Misses
How to Remove All White Spaces from a Given Text File
How to Install Maven on Redhat Linux
Command Line Arduino Compiling and Uploading
Permissions Required to Move File to Different Directory in Unix/Linux
Icmp Request Received, But Doesn't Reply
Ocr - Getting Text from Image Using Tesseract 3.0 and Imagemagick 6.6.5