很多时候linux服务器管理、发布代码等,通常需要两个工具,一个是远程拷贝,一个是远程执行命令,下面介绍两个脚本,实现这两个功能。
需要安装expect,远程执行命令,centos下直接yum -y install expect
远程执行命令脚本
可以通过脚本实现远程执行命令,对远程服务器进行管理,在代码发布方面比较好用,具体脚本如下:
[root@localhost ~]# cat remote-exe.sh
#!/usr/bin/expect -f
set ipaddress [lindex $argv 0]
set port [lindex $argv 1]
set username [lindex $argv 2]
set passwd [lindex $argv 3]
set cmd [lindex $argv 4]
spawn ssh $ipaddress -p$port -l$username
expect {
"yes/no" { send "yes\r";
exp_continue }
"assword:" { send "$passwd\r" }
}
expect -re "~]($|#)"
send "$cmd \r"
expect -re "~]($|#)"
send "exit\r"
执行命令: ./remote-exe.sh 192.168.19.130 22
root
123456
/root/tt1.sh
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/20610335/viewspace-2124578/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/20610335/viewspace-2124578/