0

I am executing the below
#!/bin/bash

###############################################################################
# This script can copy any file from Present machine to machine with IP as Test_ip and execute it :)  #
###############################################################################

############################ Main code starts here ####################################
Test_ip="10.20.12.206"

home=`pwd`
expect -c "
   spawn ssh root@$Test_ip \"mkdir /test_machine\"
   expect yes/no { send yes\r; exp_continue }
   expect password { send gaur\r; exp_continue }
   exit
        "
 expect -c "
   spawn scp $home/run_check_node.sh $home/script_3.sh $home/script_2.sh  root@$Test_ip:/test_machine/
   expect yes/no { send yes\r; exp_continue }
   expect password { send gaur\r; exp_continue }
   exit
        "       

expect -c "
    spawn ssh root@$Test_ip \"chmod +x /test_machine/run_check_node.sh /test_machine/script_2.sh /test_machine/script_3.sh;\"
    expect yes/no { send yes\r; exp_continue }
    expect password { send gaur\r; exp_continue }
    exit
        "

expect -c "
    spawn ssh root@$Test_ip \". /test_machine/script_2.sh\"
    expect yes/no { send yes\r; exp_continue }
    expect password { send gaur\r; exp_continue }
    exit
        "

The script shown is script_1.sh.

In the above code, I am creating a directory on Test_ip and then copying all 3 files ( script_2.sh, script_3.sh and run_check_node.sh) from current machine to other machine (Test_ip) using scp. Then I gave execute permission to them. When I am trying to do spawn ssh root@$Test_ip \". /test_machine/script_2.sh\", it is accepting password and then displaying "/test_machine/script_2.sh: No such file or directory".

I am not getting why it is telling like that. It just made all of them executable and at next command, claiming that the file does not exist!

Aakash Goyal
  • 1,051
  • 4
  • 12
  • 44
  • it looks as though you copy the file to $home/script_2.sh but try and execute it from /test_machine/script_2.sh? – Jason Lewis Jun 10 '14 at 07:16
  • for scp, i am copying files from `pwd` to `test_machine` directory on other machine. So, nothing seems like what you are thinking. – Aakash Goyal Jun 10 '14 at 07:25
  • The working of scp command is: scp files_to_copy where_to_copy – Aakash Goyal Jun 10 '14 at 07:28
  • oh yes, sorry, my mistake – Jason Lewis Jun 10 '14 at 07:31
  • There is more relevant information in this page [http://stackoverflow.com/questions/13298487/bash-script-to-ssh-into-a-machine-without-prompting-password-and-without-using-k] That may be worth a look as well. – David C. Rankin Jun 10 '14 at 07:43
  • Well when you log in to those machines have those files actually been created? Perhaps the copy failed ... I don't know expect but it doesn't look to me like you check the error code returned. So if the previous command failed you just keep going. – dave Jun 10 '14 at 07:48
  • @dave The files are there. That why I told that "I am not getting why it is telling like that. It just made all of them executable and at next command, claiming that the file does not exist!" – Aakash Goyal Jun 10 '14 at 07:54

1 Answers1

0

Instead of using
expect -c " spawn ssh root@$Test_ip \". /test_machine/script_2.sh\" expect yes/no { send yes\r; exp_continue } expect password { send gaur\r; exp_continue } exit "

use
expect -c " spawn ssh root@$Test_ip \"***bash*** /test_machine/script_2.sh\" expect yes/no { send yes\r; exp_continue } expect password { send gaur\r; exp_continue } exit "

Note: The . is replaced with bash above.

What I feel happening is, although the script script_2.sh is made executable, when trying to execute it from local machine to remote machine, because of using #!/bin/bash, it may not be allowing it to run as a shell script (by using . /file_path). See the difference between using bash and sh clearly here

Community
  • 1
  • 1
Aakash Goyal
  • 1,051
  • 4
  • 12
  • 44