在远程服务器上执行本地的shell脚本
来源:互联网 发布:淘宝客服的案例 编辑:程序博客网 时间:2024/05/13 15:04
1 使用ssh实现
ssh user@hostname -C "/bin/bash" < test.sh
2 使用expect实现
采用的策略就是先在本地通过expect把shell脚本推送到远程服务器上,之后再用expect模拟登录之后,先给远程的shell脚本权限,然后再执行脚本,
最后删除脚本就ok啦!PS:在expect和控制台交互的时候我设置的timeout时间是1s,如果执行的shell脚本时间比较长的话,timeout应该大于shell脚本的时长
下面是expect脚本的代码:
#!/home/map/.jumbo/bin/expect set serverPwd [lindex $argv 0] #服务器密码 set serverIp [lindex $argv 1] #服务器IP set address [lindex $argv 2] #服务器存放路径 set file [lindex $argv 3] #脚本名 set timeout 5 #手动设置与控制台交互的时间 #将shell脚本推送到远程服务器 spawn scp $file $serverIp:$address expect { "*yes/no*" {send "yes\r"; exp_continue} "*password:" {send "$serverPwd\r"; exp_continue} "*Password:" {send "$serverPwd\r"} } #登录远程服务器 spawn ssh $serverIp expect { "*yes/no*" {send "yes\r"; exp_continue} "*password:" {send "$serverPwd\r"; exp_continue} "*Password:" {send "$serverPwd\r"} } expect "#" #给shell脚本添加权限 send "chmod a+x $address$file\r" expect "#" #执行shell脚本 send "$address$file $address\r" expect "#" #删除远程服务器上的shell脚本 send "rm $address$file\r" expect "#" #退出 send "exit\r" expect eof
0 0
- 在远程服务器上执行本地的shell脚本
- linux 通过shell脚本在本地执行远程机器命令
- shell脚本备份本地服务器的文件到远程服务器
- Python远程在windows服务器上执行批处理脚本
- SSH 在本地执行远程机器上的命令
- SSH 在本地执行远程机器上的命令
- SSH在本地执行远程机器上的命令
- SSH 在本地执行远程机器上的命令
- 在服务器上的所有数据库上执行脚本
- java怎么调用远程服务器上的shell脚本 二参数调用远程
- 执行远程服务器脚本
- 远程执行服务器脚本
- Ganymed SSH-2 for Java系列7之删除远程服务器上的非空目录(运用执行shell脚本的方式)
- Ganymed SSH-2 for Java系列7之删除远程服务器上的非空目录(运用执行shell脚本的方式)
- shell远程执行 远程执行脚本
- 执行远程服务器上的脚本失败?(环境变量引起的问题)
- shell远程登录执行脚本
- shell: 普通用户如何以root权限执行远程服务器上的命令_20160706_七侠镇莫尛貝
- 10位以内二进制数字转十进制计算器(Printing the Decimal Equivalent of a Binary Number)
- JetBrains WebStorm 快捷键
- 层次聚类算法之single-linkage和complete-linkage(C语言实现)
- C基础(6-10)
- 【水】2016.06.17
- 在远程服务器上执行本地的shell脚本
- .NET取代理IP Request.ServerVariables["HTTP_X_FORWARDED_FOR"]
- Qt/C++ 动画 stackedWidget切换动画
- 查找目录下大小超过10M的文件
- java基础-内部类
- c++之map与vector的erase
- hadoop大数据学习路线
- C# .NET 遍历Json 形成键值对
- Create Maximum Number