Hello I have the next sh script in other machine like next in machine .35:
#!/bin/bash
mount //X.X.X.20/home/sybase /home/sybase/
Today=$(date --date="today" +%Y%m%d)
Yesterday=$(date --date="yesterday" +%Y%m%d)
LOG=/home/sybase/Log/${Today}_query.log
echo " Arranco el cron: " $(date) >> $LOG
echo " Export for day: " $Today >>$LOG
RemovalDay=$(date --date "-7 days" +"%Y%m%d")
echo " Removal LogFile date: " $RemovalDay >> $LOG
RemovalDay2=$(date --date "-7 days" +"%Y"-"%m"-"%d")
echo " Removal LogFile date: " $RemovalDay2 >> $LOG
# Execute two queries in 2 different Sybase servers
expect << EOF
set timeout 5000000
spawn su sybase
expect "$ "
send " dbisql -nogui -c \"uid=dba;pwd=sql;eng=store1;dbn=store1\" -host X.X.X.32 -port 2640 /home/sybase/query1.sql \r "
expect "$ "
send " dbisql -nogui -c \"uid=dba;pwd=sql;eng=store2;dbn=store2\" /home/sybase/query2.sql \r "
expect "$ "
send "exit \r "
EOF
### Result moved to folder mounted in .20
chmod 777 /home/sybase/query.txt
mv -f /home/sybase/query.txt /home/sybase/query_${Yesterday}.txt
echo " Move file query.txt to mounted folder to be stored" $(date) >> $LOG
### Unmounted.27 paths and delete intermediate files
echo " Deleted server2 file query2 " >> $LOG
## Remove exports from previous days -7
echo " Deleted Export File query.log " >> $LOG
## Remove logs from previous days -7
echo " Deleted Old LogFile "${RemovalDay}"_query.log " >> $LOG
echo " Deleted Old Crontab LogFile "${RemovalDay2}"_query.log " >> $LOG
umount /home/sybase/
echo " Unmounted all folders " $(date) >> $LOG
If I execute locally (in the .35 machine), the sh script query.sh is relatively fast it takes about 10 seconds to complete and it finishes.
When I try to execute that script remotely from the another machine with ip .20 with the next python program:
import paramiko
import os
import time
## Connect to the store1 via SSH (paramiko)
hostname = "X.X.X.35"
port = 22
username = "user"
password = "pass"
# initialize the SSH client
client = paramiko.SSHClient()
# add to known hosts
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
client.connect(hostname=hostname, username=username, password=password, port=port)
except:
print("[!] Cannot connect to the SSH Server")
exit()
stdin, stdout, stderr = client.exec_command('bash /home/sybase/query.sh')
time.sleep(60)
# read the standard output and print it
stdout.channel.recv_exit_status()
lines = stdout.readlines()
for line in lines:
print(line)
# close the connection
client.close()
It seems to never finish it just still executing but nothing happens, the results are never exported.
However when executing the python program, if I do a ps |aux grep query.sh in the X.X.X.35 machine (where the sh script query.sh is located) I get that there is 2 lines with it:
ps aux | grep query.sh
35001 24672 0.0 0.0 106116 1420 ? Ss 15:26 0:00 bash -c bash /home/sybase/query.sh
35001 24700 0.0 0.0 106116 1244 ? S 15:26 0:00 bash /home/sybase/query.sh
It also keep executing forever.