我遇到有关在远程服务器上执行shell命令的问题。
我正在尝试各种解决方案而且我有一个工作但它没有在维护方面进行优化:我使用批处理文件启动连接到远程服务器的putty并发送命令。
即。在groovy:
def batchFile = "C:\\Validation\\Tests_Auto\\Scripts\\remote_process\\setOldDate.bat"
Runtime.runtime.exec(batchFile)
并在我的批处理文件中:
c:
cd C:\Validation\Tests_Auto\Scripts\remote_process\
putty.exe -ssh root@xx.xx.xx.xx -pw **** -m "C:\Validation\Tests_Auto\Scripts\remote_process\setOldDate.txt"
setOldDate.txt包含命令date -s @1522018800
这很有效。但是我想以更清洁的方式启动它,要么避免使用命令的文本文件,要么更好地避免使用putty。 我尝试了几种方法来做同样的事情,但它不起作用。我想我不是太远但我需要一些帮助。
我尝试通过ssh启动直接命令:
Runtime.getRuntime().exec('"c:\\Program Files\\OpenSSH\\bin\\ssh.exe" root:****@xx.xx.xx.xx date -s @1522018800')
如果有人能提供帮助,我将不胜感激
感谢
答案 0 :(得分:3)
@Grab(group='com.jcraft', module='jsch', version='0.1.54')
def ant = new AntBuilder()
ant.sshexec( host:"somehost", username:"dude", password:"yo", command:"touch somefile" )
其他sshexec
和scp
任务参数请参阅doc:
https://ant.apache.org/manual/Tasks/sshexec.html
https://ant.apache.org/manual/Tasks/scp.html
for soapui
此方法使用apache ant
+ jsch-0.1.54.jar
我知道soapui的唯一方法:
下载以下库并将它们放入soapui\bin\endorsed
目录(创建endorsed
目录)
修改soapui\bin\soapui.bat
并在其他JAVA_OPTS
定义的位置添加以下行:
set JAVA_OPTS=%JAVA_OPTS% -Djava.endorsed.dirs="%SOAPUI_HOME%endorsed"
因为ant
libs必须在groovy之前加载。
然后上面的代码应该在soapui中工作(@Grab
除外)
或者,您只能将jsch-XXX.jar
下载到现有的soapui\bin\ext
目录中,并直接从groovy使用jsch
库
参见示例:http://www.jcraft.com/jsch/examples/
或搜索groovy jsch示例
答案 1 :(得分:0)
最后,编译我的各种研究并努力适应我的环境约束(在soapui中groovy),我最终得到了以下对我有用的解决方案:
下载jsch-0.1.54.jar并将其设置为C:\ Program Files \ SmartBear \ ReadyAPI-2.3.0 \ bin \ ext
使用以下groovy脚本:
import java.util.Properties
import com.jcraft.jsch.ChannelExec
import com.jcraft.jsch.JSch
import com.jcraft.jsch.Session
def ip = context.expand( '${#Project#projectEndpoint}' )
try
{
JSch jsch = new JSch();
Session session = jsch.getSession("root","$ip", 22);
session.setPassword("****");
// Avoid asking for key confirmation
Properties prop = new Properties();
prop.put("StrictHostKeyChecking", "no");
session.setConfig(prop);
session.connect();
// SSH Channel
ChannelExec channelssh = (ChannelExec)session.openChannel("exec");
// Execute command
//channelssh.setCommand("date -s @1520018000"); // change date
channelssh.setCommand("ntpdate -u pool.ntp.org"); // restore date
channelssh.connect();
channelssh.disconnect();
}
catch (Exception e)
{
log.info "exception : " + e
System.out.println(e.getMessage());
}
finally
{
session.disconnect();
}