ssh遠端執行命令並自動退出
阿新 • • 發佈:2018-12-30
ssh命令格式如下:
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]]
[ [email protected]]hostname [command]
主要引數說明:
-l 指定登入使用者
-p 設定埠號
-f 後臺執行,並推薦加上 -n 引數
-n 將標準輸入重定向到 /dev/null,防止讀取標準輸入
-N 不執行遠端命令,只做埠轉發
-q 安靜模式,忽略一切對話和錯誤提示
-T 禁用偽終端配置
ssh 執行遠端命令格式:
ssh [options][remote host][command]
假設遠端伺服器IP是192.168.110.34
例:檢視遠端伺服器的cpu資訊
ssh -l www-online 192.168.110.34 "cat /proc/cpuinfo"
[email protected]:~$ ssh -l www-online 192.168.110.34 "cat /proc/cpuinfo" [email protected]'s password: processor : 0 vendor_id : GenuineIntel cpu family : 6 model : 26 model name : Intel(R) Xeon(R) CPU E5506 @ 2.13GHz stepping : 5 cpu MHz : 2128.000 cache size : 4096 KB fpu : yes fpu_exception : yes cpuid level : 11 wp : yes flags : 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_lm bogomips : 4256.00 clflush size : 64 cache_alignment : 64 address sizes : 40 bits physical, 48 bits virtual power management: processor : 1 vendor_id : GenuineIntel cpu family : 6 model : 26 model name : Intel(R) Xeon(R) CPU E5506 @ 2.13GHz stepping : 5 cpu MHz : 2128.000 cache size : 4096 KB fpu : yes fpu_exception : yes cpuid level : 11 wp : yes flags : 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_lm bogomips : 4260.80 clflush size : 64 cache_alignment : 64 address sizes : 40 bits physical, 48 bits virtual power management:
例:執行遠端伺服器的sh檔案
首先在遠端伺服器的/home/www-online/下建立一個uptimelog.sh指令碼
#!/bin/bash
uptime >> 'uptime.log'
exit 0
使用chmod增加可執行許可權chmod u+x uptimelog.sh
在本地呼叫遠端的uptimelog.shssh -l www-online 192.168.110.34 "/home/www-online/uptimelog.sh"
執行完成後,在遠端伺服器的/home/www-online/中會看到uptime.log檔案,顯示uptime內容[email protected]:~$ tail -f uptime.log
21:07:34 up 288 days, 8:07, 1 user, load average: 0.05, 0.19, 0.31
例:執行遠端後臺執行sh
首先把uptimelog.sh修改一下,修改成迴圈執行的命令。作用是每一秒把uptime寫入uptime.log
#!/bin/bash
while :
do
uptime >> 'uptime.log'
sleep 1
done
exit 0
我們需要這個sh在遠端伺服器以後臺方式執行,命令如下:ssh -l www-online 192.168.110.34 "/home/www-online/uptimelog.sh &"
[email protected]:~$ ssh -l www-online 192.168.110.34 "/home/www-online/uptimelog.sh &"
[email protected]'s password:
輸入密碼後,發現一直停住了,而在遠端伺服器可以看到,程式已經以後臺方式運行了。
[email protected]:~$ ps aux|grep uptimelog.sh
1007 20791 0.0 0.0 10720 1432 ? S 21:25 0:00 /bin/bash /home/www-online/uptimelog.sh
原因是因為uptimelog.sh一直在執行,並沒有任何返回,因此呼叫方一直處於等待狀態。
我們先kill掉遠端伺服器的uptimelog.sh程序,然後對應此問題進行解決。
ssh 呼叫遠端命令後不能自動退出解決方法
可以將標準輸出與標準錯誤輸出重定向到/dev/null,這樣就不會一直處於等待狀態。
ssh -l www-online 192.168.110.34 "/home/www-online/uptimelog.sh > /dev/null 2>&1 &"
[email protected]:~$ ssh -l www-online 192.168.110.34 "/home/www-online/uptimelog.sh > /dev/null 2>&1 &"
[email protected]'s password:
[email protected]:~$
但這個ssh程序會一直執行在後臺,浪費資源,因此我們需要自動清理這些程序。
實際上,想ssh退出,我們可以在ssh執行完成後kill掉ssh這個程序來實現。
首先,建立一個sh執行ssh的命令,這裡需要用到ssh的
-f 與 -n 引數,因為我們需要ssh也以後臺方式執行,這樣才可以獲取到程序號進行kill操作。
建立ssh_uptimelog.sh,指令碼如下
#!/bin/bash
ssh -f -n -l www-online 192.168.110.34 "/home/www-online/uptimelog.sh &" # 後臺執行ssh
pid=$(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) # 獲取程序號
echo "ssh command is running, pid:${pid}"
sleep 3 && kill ${pid} && echo "ssh command is complete" # 延遲3秒後執行kill命令,關閉ssh程序,延遲時間可以根據呼叫的命令不同調整
exit 0
可以看到,3秒後會自動退出
[email protected]:~$ ./ssh_uptimelog.sh
[email protected]'s password:
ssh command is running, pid:10141
ssh command is complete
[email protected]:~$
然後檢視遠端伺服器,可以見到uptimelog.sh 在後臺正常執行。
[email protected]:~$ ps aux|grep uptime
1007 28061 0.1 0.0 10720 1432 ? S 22:05 0:00 /bin/bash /home/www-online/uptimelog.sh
檢視uptime.log,每秒都有uptime資料寫入。[email protected]:~$ tail -f uptime.log
22:05:44 up 288 days, 9:05, 1 user, load average: 0.01, 0.03, 0.08
22:05:45 up 288 days, 9:05, 1 user, load average: 0.01, 0.03, 0.08
22:05:46 up 288 days, 9:05, 1 user, load average: 0.01, 0.03, 0.08
22:05:47 up 288 days, 9:05, 1 user, load average: 0.01, 0.03, 0.08
22:05:48 up 288 days, 9:05, 1 user, load average: 0.01, 0.03, 0.08