Search code examples
bashsshcommand

ssh connect and commands programmatically


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 ?


Solution

  • 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.