shell自动登录ssh并运行另外一台机器上的脚本

平时工作,如果客户有买linux服务器,经常要帮客户安装一些东西,这时得登录客户的服务器,然后从我们公司的服务器拷贝脚本,然后再运行,比较麻烦.
使shell利用python的SimpleHTTPServer模块可以很方便的进行操作(剑走偏锋)
PS: perl的net::ssh与ruby的net/ssh都是现成的.
以后直接运行它就可以了!

  1. #!/bin/bash
  2. use () {
  3.         echo -e "try again.\teg: ./$0 1.1.1.1" && exit 1
  4. }
  5. echo $(echo $@)|grep '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}' 2>&1 >/dev/null
  6. if [ $? -ne 0 ]
  7.         then
  8.         use
  9. if [ $# != 1 ]
  10.         then
  11.         use && exit 1
  12.         fi
  13.         fi
  14. cd /root/K
  15. python -c "import SimpleHTTPServer;SimpleHTTPServer.test()"  2>&1 >/dev/null &
  16. ssh -tt $@ <<EOF
  17. curl Your_Server_IP:8000/Some_Script|bash && exit
  18. EOF
  19. ps aux|grep \[S]impleHTTPServer|awk '{print "kill -9",$2}'|bash
  20. echo "all the jobs has been done!"

grep不显示本身进程

无需传统意义上的 grep xx|grep -v grep ……

  1. [root@Centos ~]# ps aux|grep ssh
  2. root      1959  0.0  0.2   7076  1064 ?        Ss   Oct02   0:00 /usr/sbin/sshd
  3. root      5812  0.0  0.1   3920   688 pts/1    R+   15:30   0:00 grep ssh
  4. root     18278  0.0  0.5  10076  2912 ?        Ss   Oct06   0:18 sshd: root@pts/1 
  5. [root@Centos ~]# ps aux|grep \[s]sh
  6. root      1959  0.0  0.2   7076  1064 ?        Ss   Oct02   0:00 /usr/sbin/sshd
  7. root     18278  0.0  0.5  10076  2912 ?        Ss   Oct06   0:18 sshd: root@pts/1

Linux硬件监控工具sensors

linux查看硬件温度,硬件监控工具.
主要用他来监控cpu的温度与风扇的转速,工具名称为sensors
以下为安装步骤

  1. 安装sensors
  2. yum install -y lm_sensors.i386
  3.  
  4. 运行sensors侦测并向导
  5. sensors-detect
  6.  
  7. # sensors-detect revision 5291 (2008-06-23 23:40:46 -0700)
  8.  
  9. This program will help you determine which kernel modules you need
  10. to load to use lm_sensors most effectively. It is generally safe
  11. and recommended to accept the default answers to all questions,
  12. unless you know what you're doing.
  13.  
  14. We can start with probing for (PCI) I2C or SMBus adapters.
  15. Do you want to probe now? (YES/no):
  16.  
  17. #探测PCI SMBus(系统管理总线)和I2C(串行总线) , 选择YES
  18.  
  19. Probing for PCI bus adapters...
  20. Use driver `i2c-i801' for device 0000:00:1f.3: Intel ICH10
  21.  
  22. We will now try to load each adapter module in turn.
  23. Module `i2c-i801' already loaded.
  24. If you have undetectable or unsupported I2C/SMBus adapters, you can have
  25. them scanned by manually loading the modules before running this script.
  26.  
  27. To continue, we need module `i2c-dev' to be loaded.
  28. Do you want to load `i2c-dev' now? (YES/no):
  29.  
  30. #加载i2c-dev,选择YES
  31.  
  32. Module loaded successfully.
  33.  
  34. We are now going to do the I2C/SMBus adapter probings. Some chips may
  35. be double detected; we choose the one with the highest confidence
  36. value in that case.
  37. If you found that the adapter hung after probing a certain address,
  38. you can specify that address to remain unprobed.
  39.  
  40. Next adapter: SMBus I801 adapter at 1180 (i2c-0)
  41. Do you want to scan it? (YES/no/selectively): yes
  42.  
  43. #扫描SMBus,选择yes
  44.  
  45. Client found at address 0x50
  46. Probing for `Analog Devices ADM1033'...                     No
  47. Probing for `Analog Devices ADM1034'...                     No
  48. Probing for `SPD EEPROM'...                                 Yes
  49.     (confidence 8, not a hardware monitoring chip)
  50. Probing for `EDID EEPROM'...                                No
  51. Client found at address 0x52
  52. Probing for `Analog Devices ADM1033'...                     No
  53. Probing for `Analog Devices ADM1034'...                     No
  54. Probing for `SPD EEPROM'...                                 Yes
  55.     (confidence 8, not a hardware monitoring chip)
  56. Probing for `EDID EEPROM'...                                No
  57.  
  58. Some chips are also accessible through the ISA I/O ports. We have to
  59. write to arbitrary I/O ports to probe them. This is usually safe though.
  60. Yes, you do have ISA I/O ports even if you do not have any ISA slots!
  61. Do you want to scan the ISA I/O ports? (YES/no): yes
  62.  
  63. #是否扫描ISA I/O端口,选择yes
  64.  
  65. Probing for `National Semiconductor LM78' at 0x290...       No
  66. Probing for `National Semiconductor LM78-J' at 0x290...     No
  67. Probing for `National Semiconductor LM79' at 0x290...       No
  68. Probing for `Winbond W83781D' at 0x290...                   No
  69. Probing for `Winbond W83782D' at 0x290...                   No
  70.  
  71. Some Super I/O chips may also contain sensors. We have to write to
  72. standard I/O ports to probe them. This is usually safe.
  73. Do you want to scan for Super I/O sensors? (YES/no): yes
  74.  
  75. #是否扫描I/O传感器,选择yes
  76.  
  77. Probing for Super-I/O at 0x2e/0x2f
  78. Trying family `National Semiconductor'...                   No
  79. Trying family `SMSC'...                                     No
  80. Trying family `VIA/Winbond/Fintek'...                       Yes
  81. Found unknown chip with ID 0xb073
  82.     (logical device B has address 0x290, could be sensors)
  83. Probing for Super-I/O at 0x4e/0x4f
  84. Trying family `National Semiconductor'...                   No
  85. Trying family `SMSC'...                                     No
  86. Trying family `VIA/Winbond/Fintek'...                       No
  87. Trying family `ITE'...                                      No
  88.  
  89. Some south bridges, CPUs or memory controllers may also contain
  90. embedded sensors. Do you want to scan for them? (YES/no):
  91.  
  92. #是否扫描南桥传感器,选择yes
  93.  
  94. Silicon Integrated Systems SIS5595...                       No
  95. VIA VT82C686 Integrated Sensors...                          No
  96. VIA VT8231 Integrated Sensors...                            No
  97. AMD K8 thermal sensors...                                   No
  98. AMD K10 thermal sensors...                                  No
  99. Intel Core family thermal sensor...                         Success!
  100.     (driver `coretemp')
  101. Intel AMB FB-DIMM thermal sensor...                         No
  102.  
  103. Now follows a summary of the probes I have just done.
  104. Just press ENTER to continue:
  105.  
  106. #扫描完毕,按回车
  107.  
  108. Driver `coretemp' (should be inserted):
  109.   Detects correctly:
  110.   * Chip `Intel Core family thermal sensor' (confidence: 9)
  111.  
  112. Do you want to overwrite /etc/sysconfig/lm_sensors? (YES/no):
  113.  
  114. #检测结果,决定是否覆盖/etc/sysconfig/lm_sensors,选择yes
  115.  
  116. 运行sensors进行检测
  117.  
  118. w83627dhg-isa-06e0
  119. Adapter: ISA adapter
  120. VCore:     +0.89 V  (min =  +0.00 V, max =  +1.74 V)
  121. in1:      +10.14 V  (min =  +8.98 V, max =  +0.48 V) ALARM
  122. AVCC:      +3.06 V  (min =  +1.02 V, max =  +0.19 V) ALARM
  123. 3VCC:      +3.06 V  (min =  +0.11 V, max =  +0.00 V) ALARM
  124. in4:       +0.97 V  (min =  +1.37 V, max =  +0.02 V) ALARM
  125. in5:       +1.36 V  (min =  +0.59 V, max =  +0.01 V) ALARM
  126. in6:       +1.02 V  (min =  +3.46 V, max =  +4.71 V) ALARM
  127. VSB:       +3.06 V  (min =  +2.61 V, max =  +0.06 V) ALARM
  128. VBAT:      +2.99 V  (min =  +2.06 V, max =  +2.82 V) ALARM
  129. Case Fan:    0 RPM  (min =  527 RPM, div = 32) ALARM
  130. CPU Fan:  1110 RPM  (min = 1259 RPM, div = 16) ALARM
  131. Aux Fan:     0 RPM  (min =  540 RPM, div = 32) ALARM
  132. fan4:        0 RPM  (min = 2343 RPM, div = 16) ALARM
  133. Sys Temp:    +54°C  (high =   +70°C, hyst =    +4°C)  [CPU diode ]
  134. CPU Temp:  +62.0°C  (high = +80.0°C, hyst = +75.0°C)  [CPU diode ]
  135. AUX Temp: -128.0°C  (high = +80.0°C, hyst = +75.0°C)  [CPU diode ]
  136. vid:      +2.050 V
  137. ?>

不超频的话,主要是监控CPU的温度跟风扇的转速,用脚本跑一下就很简单了.

Linux命令行 快速查看本机IP

利用curl命令抓取网页,快速查看本机IP,无需文本过滤,直接就可以查看.

  1. curl ip.appspot.com

很实用的Linux常用命令

工作期间常用的linux命令,这些linux常用命令可以帮助提高效率.

  1. 1) 连接mkdircd命令,无需再cd一次,mkdir后直接进入目录
  2.  
  3. [root@Centos test]# function MC() { mkdir "$@" && eval cd "\"\$$#\""; }
  4.  
  5. 2) 利用alias快速返回上N级目录
  6. [root@Centos test]# alias ..4="cd ../../../../"
  7. [root@Centos test]# ..4
  8.  
  9. 3) 控制标准输出与错误的流向
  10. 禁止错误输出 2> /dev/null
  11. 错误输出至文件 2> out.error
  12. 禁止标准输出 1> /dev/null
  13. 禁止错误与标准输出 >/dev/null 2>& 1
  14.  
  15. 4) 查看用户登录时间
  16. [root@Centos test]# ac -d SomeUser
  17. 总登录时间
  18. [root@Centos test]# ac -p SomeUser
  19.  
  20. 5) 追加key至远程主机
  21. [root@Centos test]# ssh-copy-id -i ~/.ssh/id_rsa.pub 10.0.0.200
  22. [root@Centos test]# ssh-copy-id -i ~/.ssh/id_rsa.pub '-p YourPort_here 10.0.0.200' #非默认22端口
  23.  
  24. 6) 利用pythonsimplehttp共享文件
  25. alias WS='python -c "import SimpleHTTPServer;SimpleHTTPServer.test()"'
  26. WS命令操作后,本地会开启8000端口监听,成为一个简单的http server.
  27.  
  28. 7) 快速创建文件
  29. touch a{1..10}
  30.  
  31. 8) awk快速统计并罗列出所有文件及文件夹大小
  32. du -sk ./*|sort -n|awk 'BEGIN{a[1]="K";a[2]="M";a[3]="G";}{total=total+$1;x=$1;y=1;while(x>1024){x=(x+1023)/1024;y++;}printf("%g%s\t%s\n",int(x*10)/10,a[y],$2);}END{y=1;while(total>1024){total=(total+1023)/1024;y++;}printf("Total: %g%s\n",int(total*10)/10,a[y]);}'
  33.  
  34. 9) curl查看httpd服务类型
  35. curl -I 10.0.0.200 2> /dev/null|grep Server
  36.  
  37. 10) 快速重命名文件
  38. [root@Centos test]# rename .asp .php *.asp
  39. 把所有.asp的文件,重命名为.php
2 12»