I have a program that creates a dynamic shell script using heredoc "stop_remoteServer_script.sh"
template of heredoc below
cat<<_STOP > stop_remoteServer_script.sh
#!/bin/bash
serverName=\$1
function getServerpid {
pid=\$( ps -ef|grep \${serverName}|grep -v grep |awk '{print \$2}' )
echo \$pid
}
function kill_soaServer {
echo "Killing \${serverName} ........"
ps -ef|grep \${serverName} |grep -v grep|awk '{print \$2}'|xargs kill -9
echo "status of killing server is $?"
}
check_soaServer=\$( getServerpid );
while [[ "X\${check_soaServer}" != "X" ]]
do
cmd_status=\$( kill_soaServer )
sleep 30s
check_soaServer=\$( getServerpid )
done
kill_soaServer \${serverName}
_STOP
actual script that goes to remote box for execution
template output
#!/bin/bash
serverName=$1
function getServerpid {
pid=$( ps -ef|grep ${serverName}|grep -v grep |awk '{print $2}' )
echo $pid
}
function kill_soaServer {
echo "Killing ${serverName} ........"
ps -ef|grep ${serverName} |grep -v grep|awk '{print $2}'|xargs kill -9
echo "status of killing server is 0"
}
check_soaServer=$( getServerpid );
while [[ "X${check_soaServer}" != "X" ]]
do
cmd_status=$( kill_soaServer )
sleep 30s
check_soaServer=$( getServerpid )
done
kill_soaServer ${serverName}
Now I have a below expect block of code which will invoke the above generated code in remote box with a parameter of server name
sName=$1
CMD="ssh -o StrictHostKeyChecking=no ${remoteUserName}\@${remoteHostName} "
# script with parameter
script="./stop_remoteServer_script.sh ${sName}"
/usr/bin/expect<<END
set force_conservative 0
spawn bash -c "${CMD} /bin/bash < ${script} "
expect "* password: "
send -- "${MYPWD}\r"
expect "*$*"
expect eof
END
The error I am getting is below
spawn bash -c ssh -o StrictHostKeyChecking=no [email protected] /bin/bash < ./stop_remoteServer_script.sh soa_server2
[email protected]'s password:
/bin/bash: soa_server2: No such file or directory
can any one help me how to send the parameter , manually it works
===================
additional try based on the suggetions :
I have created a simple network using virtual box and tried below
main program
#!/bin/bash
# script with parameter
echo "running exp example"
sName="abc123ibc"
script="a.sh"
echo "thes script is $script \n"
/usr/bin/expect -d <<END
set force_conservative 0
set timeout -1
set prompt "*$*"
puts "before swapn"
spawn scp ./a.sh -o StrictHostKeyChecking=no ansibleadm\@192.168.1.102:/home/ansibleadm/
expect "id_rsa*"
send -- "ansibleadm\r"
expect "*$*"
expect eof
puts "second spawn"
spawn ssh -o StrictHostKeyChecking=no ansibleadm\@192.168.1.102 /bin/bash -c ./a.sh $sName > log.txt
expect "id_rsa*"
send -- "ansibleadm\r"
expect ""
puts "after spawn"
expect eof
END
there is another a.sh
#!/bin/bash
sleep 12s
echo "inside swapned script"
touch ~/sample.txt
cat > ~/sample.txt<<EOF
hi suman $1
EOF
in the sample.txt what i am always getting is below the variable is missing
[ansibleadm@AppServer ~]$ cat sample.txt
hi suman
I have enabled the debugger of the expect and below are debug log
[ansibleadm@Centos-Control ~]$ ./exp.sh
running exp example
thes script is a.sh \n
expect version 5.45.4
argv[0] = /usr/bin/expect argv[1] = -d
set argc 0
set argv0 "/usr/bin/expect"
set argv ""
executing commands from command file
before swapn
spawn scp ./a.sh -o StrictHostKeyChecking=no [email protected]:/home/ansibleadm/
parent: waiting for sync byte
parent: telling child to go ahead
parent: now unsynchronized from child
spawn: returns {33533}
expect: does "" (spawn_id exp6) match glob pattern "id_rsa*"? no
Enter passphrase for key '/home/ansibleadm/.ssh/id_rsa':
expect: does "\rEnter passphrase for key '/home/ansibleadm/.ssh/id_rsa': " (spawn_id exp6) match glob pattern "id_rsa*"? yes
expect: set expect_out(0,string) "id_rsa': "
expect: set expect_out(spawn_id) "exp6"
expect: set expect_out(buffer) "\rEnter passphrase for key '/home/ansibleadm/.ssh/id_rsa': "
send: sending "ansibleadm\r" to { exp6 }
expect: does "" (spawn_id exp6) match glob pattern "*"? yes
expect: set expect_out(0,string) ""
expect: set expect_out(spawn_id) "exp6"
expect: set expect_out(buffer) ""
a.sh 100% 114 165.9KB/s 00:00
-o: No such file or directory
StrictHostKeyChecking=no: No such file or directory
expect: read eof
expect: set expect_out(spawn_id) "exp6"
expect: set expect_out(buffer) "\r\n\ra.sh
||| 0% 0 0.0KB/s --:-- ETA\ra.sh
||| 100% 114 165.9KB/s 00:00 \r\n-o: No such file or directory\r\nStrictHostKeyChecking=no: No such file or directory\r\n"
second spawn
spawn ssh -o StrictHostKeyChecking=no [email protected] /bin/bash -c ./a.sh abc123ibc > log.txt
parent: waiting for sync byte
parent: telling child to go ahead
parent: now unsynchronized from child
spawn: returns {33541}
expect: does "" (spawn_id exp10) match glob pattern "id_rsa*"? no
Enter passphrase for key '/home/ansibleadm/.ssh/id_rsa':
expect: does "\rEnter passphrase for key '/home/ansibleadm/.ssh/id_rsa': " (spawn_id exp10) match glob pattern "id_rsa*"? yes
expect: set expect_out(0,string) "id_rsa': "
expect: set expect_out(spawn_id) "exp10"
expect: set expect_out(buffer) "\rEnter passphrase for key '/home/ansibleadm/.ssh/id_rsa': "
send: sending "ansibleadm\r" to { exp10 }
expect: does "" (spawn_id exp10) match glob pattern ""? yes
expect: set expect_out(0,string) ""
expect: set expect_out(spawn_id) "exp10"
expect: set expect_out(buffer) ""
after spawn
expect: read eof
expect: set expect_out(spawn_id) "exp10"
expect: set expect_out(buffer) "\r\n"
[ansibleadm@Centos-Control ~]$
kill_soaServerfunction, you forgot to escape$?bash -c command a b cthe argsa b cwill set$0 $1 $2, so to pass arg XXX as$1you need to saybash -c command dummy XXX. This is not the case for-swhen you just saybash -s <somefile XXX.