Bash script to spawn and execute commands

tiger_groove

I am trying to create a bash script to ssh into a session and run a command then exit out of the session. currently this is what i have so far:

#!/usr/bin/expect -f

spawn ssh root@sc
expect "assword:"
send "password\r"
expect "#"
send "cd /data2/someDirectory\r"------> don't see this command being executed

and the output

[user@San ddb]$ test1
spawn ssh root@sc
root@sc's password:
SC02 RH 7.3 (0000009B 02.11.0.1)
[root@sc /]# [user@san1 ddb]$
[user@san1 ddb]$

So my question is why isn't the directory being set to myDirectory and it just exits out of the session?

Prabhu

Your expect script is doing what you are intending but is quitting once the job is send is done.

set some "expectation" like expect "$" at the end of the script and try.

#!/usr/bin/expect -f

spawn ssh [email protected]
expect "assword:"
send "pass\r"
expect "#"
send  "\r"
send  "pwd\r"
send  "\r"
send  "cd /tmp\r"
send  "touch dummy\r"
expect "$"

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Execute gcloud commands in a bash script

From Dev

Cannot execute shell commands in bash script

From Dev

How to execute git commands from bash script?

From Dev

bash script execute commands after ssh

From Dev

Bash script to read from a file and execute commands

From Dev

Bash script to read from a file and execute commands

From Dev

How to execute the following commands in a bash script?

From Dev

Bash script to execute multiple Unix commands

From Dev

How to execute sudo commands with Expect & send commands in bash script?

From Dev

shell script expect and spawn commands

From Dev

Bash Script execute commands from a file but if cancel on to jump on next one

From Dev

Bash script: How to execute commands consecutively without waiting for the previous one?

From Dev

Shell out into zsh and execute commands from bash script

From Dev

How to execute commands in docker container as part of bash shell script

From Dev

Bash script: How to execute commands consecutively without waiting for the previous one?

From Dev

How to execute shell commands in a loop from within a bash script?

From Dev

Bash script does not execute commands until after delay

From Dev

If I sudo execute a Bash script file, will all commands inside the Bash script be executed as sudo as well?

From Dev

If I sudo execute a Bash script file, will all commands inside the Bash script be executed as sudo as well?

From Dev

Can a bash shell script open a text file with less and then execute less commands from the script?

From Dev

Howto switch / chage user id witin a bash script to execute commands in the same script?

From Dev

Optional commands for bash script

From Dev

Python script to execute external commands

From Dev

Bash tries to execute commands in heredoc

From Dev

Execute Bash commands Python way

From Dev

How does bash execute commands

From Dev

Execute bash commands in a curses window

From Dev

Spawn new Terminator tabs from Bash script

From Dev

Spawn new Terminator tabs from Bash script

Related Related

  1. 1

    Execute gcloud commands in a bash script

  2. 2

    Cannot execute shell commands in bash script

  3. 3

    How to execute git commands from bash script?

  4. 4

    bash script execute commands after ssh

  5. 5

    Bash script to read from a file and execute commands

  6. 6

    Bash script to read from a file and execute commands

  7. 7

    How to execute the following commands in a bash script?

  8. 8

    Bash script to execute multiple Unix commands

  9. 9

    How to execute sudo commands with Expect & send commands in bash script?

  10. 10

    shell script expect and spawn commands

  11. 11

    Bash Script execute commands from a file but if cancel on to jump on next one

  12. 12

    Bash script: How to execute commands consecutively without waiting for the previous one?

  13. 13

    Shell out into zsh and execute commands from bash script

  14. 14

    How to execute commands in docker container as part of bash shell script

  15. 15

    Bash script: How to execute commands consecutively without waiting for the previous one?

  16. 16

    How to execute shell commands in a loop from within a bash script?

  17. 17

    Bash script does not execute commands until after delay

  18. 18

    If I sudo execute a Bash script file, will all commands inside the Bash script be executed as sudo as well?

  19. 19

    If I sudo execute a Bash script file, will all commands inside the Bash script be executed as sudo as well?

  20. 20

    Can a bash shell script open a text file with less and then execute less commands from the script?

  21. 21

    Howto switch / chage user id witin a bash script to execute commands in the same script?

  22. 22

    Optional commands for bash script

  23. 23

    Python script to execute external commands

  24. 24

    Bash tries to execute commands in heredoc

  25. 25

    Execute Bash commands Python way

  26. 26

    How does bash execute commands

  27. 27

    Execute bash commands in a curses window

  28. 28

    Spawn new Terminator tabs from Bash script

  29. 29

    Spawn new Terminator tabs from Bash script

HotTag

Archive