The ssh command format is as follows:
usage: ssh [-1246AaCfgKkMNnqsTtVvXxYy] [-b bind_address] [-c cipher_spec] [-D [bind_address:]port] [-e escape_char] [-F configfile] [-I pkcs11] [-i identity_file] [-L [bind_address:]port:host:hostport] [-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port] [-R [bind_address:]port:host:hostport] [-S ctl_path] [-W host:port] [-w local_tun[:remote_tun]] [user@]hostname [command]
Description of main parameters:
-L specify the Login User
-P: Set the port number.
-F runs in the background, and the-n parameter is recommended.
-N redirects the standard input to/dev/null to prevent reading standard input.
-N: do not execute remote commands and only perform port forwarding.
-Q quiet mode: Ignore all conversations and error prompts
-T disable Pseudo Terminal Configuration
Format of the Remote Command executed by ssh:
ssh [options][remote host][command]
Assume that the remote server IP address is 192.168.110.34.
Example: view the cpu information of the remote server
Ssh-l www-online 192.168.110.34 "cat/proc/cpuinfo"
www-online@onlinedev01:~$ ssh -l www-online 192.168.110.34 "cat /proc/cpuinfo"www-online@192.168.110.34's password:processor : 0vendor_id : GenuineIntelcpu family : 6model : 26model name : Intel(R) Xeon(R) CPU E5506 @ 2.13GHzstepping : 5cpu MHz : 2128.000cache size : 4096 KBfpu : yesfpu_exception : yescpuid level : 11wp : yesflags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss syscall nx rdtscp lm constant_tsc arch_perfmon pebs bts rep_good xtopology tsc_reliable nonstop_tsc aperfmperf pni ssse3 cx16 sse4_1 sse4_2 popcnt hypervisor lahf_lmbogomips : 4256.00clflush size : 64cache_alignment : 64address sizes : 40 bits physical, 48 bits virtualpower management:processor : 1vendor_id : GenuineIntelcpu family : 6model : 26model name : Intel(R) Xeon(R) CPU E5506 @ 2.13GHzstepping : 5cpu MHz : 2128.000cache size : 4096 KBfpu : yesfpu_exception : yescpuid level : 11wp : yesflags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss syscall nx rdtscp lm constant_tsc arch_perfmon pebs bts rep_good xtopology tsc_reliable nonstop_tsc aperfmperf pni ssse3 cx16 sse4_1 sse4_2 popcnt hypervisor lahf_lmbogomips : 4260.80clflush size : 64cache_alignment : 64address sizes : 40 bits physical, 48 bits virtualpower management:
Example: Execute the sh file of the remote server
First, create an uptimelog. sh script under/home/www-online/on the remote server.
#!/bin/bashuptime >> 'uptime.log'exit 0
Use chmod to add executable permissions
chmod u+x uptimelog.sh
Call remote uptimelog. sh locally
ssh -l www-online 192.168.110.34 "/home/www-online/uptimelog.sh"
After the execution is complete, the uptime. log file is displayed in/home/www-online/of the remote server, and the uptime content is displayed.
www-online@nmgwww34:~$ tail -f uptime.log21:07:34 up 288 days, 8:07, 1 user, load average: 0.05, 0.19, 0.31
For example, run sh on the remote background.
First, modify uptimelog. sh to the Command executed cyclically. The function is to write the uptime into uptime. log every second.
#!/bin/bashwhile :do uptime >> 'uptime.log' sleep 1doneexit 0
We need to run the sh command on the remote server. The command is as follows:
Ssh-l www-online 192.168.110.34 "/home/www-online/uptimelog. sh &"
www-online@onlinedev01:~$ ssh -l www-online 192.168.110.34 "/home/www-online/uptimelog.sh &"www-online@192.168.110.34's password:
After you enter the password, you can see that the program has been running on the remote server.
www-online@nmgwww34:~$ ps aux|grep uptimelog.sh1007 20791 0.0 0.0 10720 1432 ? S 21:25 0:00 /bin/bash /home/www-online/uptimelog.sh
The reason is that uptimelog. sh has been running and no response is returned. Therefore, the caller remains in the waiting state.
Kill the uptimelog. sh process of the remote server and solve the problem accordingly.
The solution cannot be automatically exited after the remote command is called by ssh.
You can redirect the standard output and standard error output to/dev/null so that the output will not remain in the waiting state.
Ssh-l www-online 192.168.110.34 "/home/www-online/uptimelog. sh>/dev/null 2> & 1 &"
www-online@onlinedev01:~$ ssh -l www-online 192.168.110.34 "/home/www-online/uptimelog.sh > /dev/null 2>&1 &"www-online@192.168.110.34's password:www-online@onlinedev01:~$
But this ssh process will always run in the background, wasting resources, so we need to automatically clean these processes.
In fact, to exit ssh, we can kill the ssh process after the ssh execution is complete.
First, create a sh to execute the ssh command. here we need to use the-f and-n parameters of ssh, because we need ssh to run it later, in this way, you can get the process number and perform the kill operation.
CreateSsh_uptimelog.shThe script is as follows:
#! /Bin/bashssh-f-n-l www-online 192.168.110.34 "/home/www-online/uptimelog. sh & "# Run sshpid =$ (ps aux | grep" ssh-f-n-l www-online 192.168.110.34/home/www-online/uptimelog. sh "| awk '{print $2}' | sort-n | head-n 1) # obtain the Process Code echo" ssh command is running, pid: $ {pid} "sleep 3 & kill $ {pid} & echo" ssh command is complete "# Run the kill command three seconds later to close the ssh process, the latency can be adjusted to exit 0 based on different called commands.
As you can see, it will automatically exit in 3 seconds.
www-online@onlinedev01:~$ ./ssh_uptimelog.shwww-online@192.168.110.34's password:ssh command is running, pid:10141ssh command is completewww-online@onlinedev01:~$
Then check the remote server. You can see that uptimelog. sh runs normally in the background.
www-online@nmgwww34:~$ ps aux|grep uptime1007 28061 0.1 0.0 10720 1432 ? S 22:05 0:00 /bin/bash /home/www-online/uptimelog.sh
View uptime. log, which writes uptime data every second.
www-online@nmgwww34:~$ tail -f uptime.log22:05:44 up 288 days, 9:05, 1 user, load average: 0.01, 0.03, 0.0822:05:45 up 288 days, 9:05, 1 user, load average: 0.01, 0.03, 0.0822:05:46 up 288 days, 9:05, 1 user, load average: 0.01, 0.03, 0.0822:05:47 up 288 days, 9:05, 1 user, load average: 0.01, 0.03, 0.0822:05:48 up 288 days, 9:05, 1 user, load average: 0.01, 0.03, 0.08