要想通过newlisp远程执行命令,首先要熟悉ssh远程执行命令
这里有一个例子,想要在远程服务器上安装emacs,可以这么做
# ssh -t 10.149.11.157 ‘export http_proxy="http://10.180.86.30:3128" && yum install emacs‘说明:
1. 由于远程服务器无法访问外网,先设置proxy, 之后再运行yum install emacs
2. 由于当前服务器已经和远程服务器建立了ssh认证,无需输入用户名和密码
3. -t 参数可以在本机获得远程服务器的虚拟tty终端,如果有什么需要交互的,在本机即可完成操作,比如下面问y/N
Total download size: 22 M Installed size: 73 M Is this ok [y/N]: y y
现在用newlisp对其包装,主要是需要一个文件提供所有需要操作的的IP地址, 然后利用newlisp生成一个.sh文件,里面包含各种ssh命令。
hosts.lsp里面包含了各个远程服务器的IP地址:
(set ‘remotes ‘("10.149.11.156" "10.149.11.157" "10.149.11.158" "10.149.11.159" "10.149.11.160" "10.149.11.161"))
gen_remote_cmds.lsp文件代码如下:
#!/usr/bin/newlisp
(println "this util will help to generate commands on remote server using newlisp and ssh")
(println "example: " "./gen_remote_cmds.lsp your-cmd your-hosts.lsp")
(set ‘cmd (main-args 2))
(set ‘hosts (main-args 3))
(load hosts)
(if (file? "output.sh")
(delete-file "output.sh"))
(append-file "output.sh" "#!/bin/bash")
(append-file "output.sh" "\n")
(dolist (host remotes)
(set ‘cmd2 (string "ssh -t " host " ‘" cmd "‘"))
(append-file "output.sh" cmd2)
(append-file "output.sh" "\n")
)
(exit)
# ./gen_remote_cmds.lsp ‘export http_proxy="http://10.180.86.30:3128" && yum install emacs‘ ./hosts.lsp this util will help to generate commands on remote server using newlisp and ssh example: ./gen_remote_cmds.lsp your-cmd your-hosts.lsp
然后运行,期间会提示输入一些y, 很快乐的远程安装了一批emacs.
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文:http://blog.csdn.net/csfreebird/article/details/48729731