ssh connect and commands programmatically

2019-08-26 07:34发布

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 ?

标签: bash ssh command
1条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-08-26 08:11

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.

查看更多
登录 后发表回答