ssh connect and commands programmatically

主宰稳场 提交于 2019-12-11 17:22:59

问题


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

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 ?


回答1:


try following:

#!/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

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.



来源:https://stackoverflow.com/questions/45129323/ssh-connect-and-commands-programmatically

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!