Visit Our UNIX and Linux User Community


How to execute commands on remote server using expect script?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to execute commands on remote server using expect script?
# 1  
Old 03-20-2016
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.

Code:
#!/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?

Last edited by kchinnam; 03-20-2016 at 08:27 PM..
# 2  
Old 03-20-2016
Quote:
Originally Posted by kchinnam
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.

Code:
#!/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.

Previous Thread | Next Thread
Test Your Knowledge in Computers #55
Difficulty: Easy
The term UPS stands for 'Universal Power Source'.
True or False?

9 More Discussions You Might Find Interesting

1. AIX

Nim server "Unable to execute remote client commands"

Hello, What is its mean? Could you please help me? Best regards, root@nimserver:/> nimadm -j nimadmvg -c dev4 -s spot1 -l lpp_source1 -d "hdisk7" -Y Initializing the NIM master. Initializing NIM client dev4. 0042-006 c_rsh: (exec_nimsh_cmd) exec_cmd Error 0 poll: setup failure... (2 Replies)
Discussion started by: getrue
2 Replies

2. Shell Programming and Scripting

Sudo connect to a remote server and execute scripts in remote server

Hello Every one!! I am trying to write a shell script which will connect to a remote server and execute scripts which are at a certain path in the remote server. Before this I am using a sudo command to change the user. The place where I am stuck is, I am able to connect to the... (6 Replies)
Discussion started by: masubram
6 Replies

3. Shell Programming and Scripting

Shell script help to execute ssh remote commands

Hi, The below command is not giving me the count , Can somebody help me in re-writing this pls . Proc_Exist=`ssh -q -o "BatchMode=yes" -o "PasswordAuthentication=no" $OAUSER@${Primary_Node} ps -ef | grep -v grep | grep "${ICM_Proc}" |wc -l ` Also the same problem with below... (13 Replies)
Discussion started by: Y.balakrishna
13 Replies

4. Shell Programming and Scripting

Execute a local script against a remote server

I am unable to run the below script against a remote server due to syntax error (then unexpected), but i am able to run it locally. Am i executing it correctly or is there any other way to execute it. ssh username@servernname ksh -s < scriptname #!/bin/ksh function record { ((end =... (5 Replies)
Discussion started by: NarayanaPrakash
5 Replies

5. Shell Programming and Scripting

i want to execute shell script on remote server with in sftp session

Hi, I want to execute shell script with in sftp session for remote server. like i have a shell script test.sh that is on local server.i want to execute that script on remote server sftp user@192.168.56.10 sftp> test.sh ---execute for remote server not for local server. sftp... (3 Replies)
Discussion started by: SAUD PASHA
3 Replies

6. Shell Programming and Scripting

Expect script to execute a script on a remote host

Hi, I am new to the expect scripting. I have this expect script as below : spawn ssh remote_server -l id set pass "12345" set opt "s" expect "Password:" {send "$pass\r" ; } expect "*ENTER*" {send "Enter\r"; exp_continue } expect "Please select option :" {send... (2 Replies)
Discussion started by: curt137
2 Replies

7. Programming

Expect script to run a Shell script on remote server

Hi All, I am using a expect script to run a shell script on remote server, the code is as follows. But the problem is that it executes only first command, and hangs it doesn't run the next commands. spawn ssh $uid@$host expect "password:" send "$password\r" expect "*\r" send... (2 Replies)
Discussion started by: yashwanthsn
2 Replies

8. Shell Programming and Scripting

Script to connect to a remote server and execute scripts

Hello All I need a script or set of commands which can establish a remote connection with another server and execute some scripts over there. Basically it has to establish the connection with the remote server as an user ,say 'testuser' and then execute the script 'testscript'. and return the... (5 Replies)
Discussion started by: sgbhat
5 Replies

9. Shell Programming and Scripting

Can BASH execute commands on a remote server when the commands are embedded in shell

I want to log into a remote server transfer over a new config and then backup the existing config, replace with the new config. I am not sure if I can do this with BASH scripting. I have set up password less login by adding my public key to authorized_keys file, it works. I am a little... (1 Reply)
Discussion started by: bash_in_my_head
1 Replies

Featured Tech Videos