ssh连接并以编程方式命令 [英] ssh connect and commands programmatically

查看:82
本文介绍了ssh连接并以编程方式命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使脚本通过SSH连接连接到服务器并执行一些命令。第一部分有效:

I'm trying to make a script connecting via an SSH connection to a server and executing some commands. The first part works:

#!/usr/bin/expect -f
spawn ssh address
expect "password:"
send "password\r"
interact

但是之后,我想执行更多命令,例如 cd 到目录,启动更多脚本等。有什么方法可以实现这些功能?

but after that I want to execute some more commands, e.g cd to directory, launch some more scripts etc. Is there any way to implement these things ?

推荐答案

尝试以下操作:

#!/usr/bin/expect
set login "any_user"
set addr "some_address"
set pw "any_pwd"

spawn ssh -t $login@$addr
expect "$login@$addr\'s password:"
send "$pw\r"
expect "~" ; # put here string from your server prompt
send "mkdir some_dir\r"
interact

这是命令之一,您可以尝试使用cd等其他命令,也可以尝试其他脚本,并让我们知道是否有任何查询。

This is one of the command, you could try other commands like cd, any other scripts too in it and let us know if any queries.

这篇关于ssh连接并以编程方式命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆