How to execute commands on remote server using expect script?

I need to copy python script file to around 100 servers using expect script.

  1. Copy script to my user home first(/home/myhome) on each remote server
  2. change permissions on copied file to 766.
  3. sudo to appuser1 account on remote server. copy script file from my user home to /usr/bin/ folder.
#!/bin/bash

REMOTE_IP='30.20.138.25'
SCP_PASSWORD="Rkfasfafsafas"
SCP_REMOTE="myuserid@$REMOTE_IP:/home/myhome"
SCP_FILE=somefile.py

expect -c "
   set timeout 1
   spawn scp $SCP_FILE $SCP_REMOTE
   expect yes/no { send yes\r ; exp_continue }
   expect *assword: { send $SCP_PASSWORD\r }
   expect 100%
   sleep 1

   send "/bin/chmod 766 $SCP_FILE\n"
   expect "$"

   send "/usr/bin/sudo bin/su appUser1"
   expect *assword: { send $SCP_PASSWORD\r }

   send "/usr/bin/cp /home/myhome/$SCP_FILE /usr/bin\r"
   sleep 1
   exit
"

above script is copying file to remote server. send commands are failing.
couldn't read file "766": no such file or directory

How can I get rest of the code after first sleep 1 working? Do I need to initiate another SSH connection? Can I use ssh connection opened for scp and perform other commands?

Note that in your script the 1st argument passed to expect is -c and I have marked the 2nd argument to expect in red. That makes the 3rd argument passed to expect be the string 766 (which appears to be interpreted as a filename by expect thereby producing the diagnostic message you quoted).

If you want to pass quoted strings to commands inside the 2nd argument to expect , you need to escape the inner quotes. Note also that some of the arguments passed to send are quoted and some are not. I would assume that they should be consistently quoted or unquoted; not a mix.