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 remote server but neither am able to change the directory path nor I am able to execute scripts which I need to.
Also the log files which I have given in the script are not being created nor the log is being captured in the files even if I create them manually.
Below is the prototype of the script which I have written:
#!/bin/bash
#***************************************
ssh user@remote_server
if [ $? -eq 0 ];
then
echo "connection established successfully" >> path/to/log/file #(this log file is not being created nor is anything being inserted even if I create it manually)#
exit 0
else
echo "connection unsuccessful" >> path/to/log/file
fi
sh /path/to/remote/scripts/script_name
if [ $? -eq 0 ];
then
echo "script executed successfully" >> path/to/log/file #(this log file is not being created nor is anything being inserted even if I create it manually)#
else
echo "script not executed" >> path/to/log/file
fi
exit
Please suggest as where am I going wrong and what needs to be corrected.
The way you issue the ssh command opens an interactive session on the remote host, waiting for your input on stdin. The rest of the script is executed only after you logged out from the host, and the log files should be created locally (unless their path doesn't exist).
ssh will execute the command (list) on its command line, or stdin if redirected to e.g. a "here document".
Thank you very much for the reply, just got few doubts in the prototype you have attached.
1) In the first line, you have mentioned there is "/bin/uname" what is this exactly for and in place of uname do I need to give the username with which I am connecting to remote server?
2) Also there is "> /dev/null 2>&1;" in the script, is it mandatory to have this line in the script as I don't have permission to that path, including it may fail the script and can I specify any other path instead of it?
Please don't mind if the above questions sound silly, as I am a novice to unix, I find them hard to under stand.
Redirecting to /dev/null is a common method to suppress output. Different from many other entries in /dev , null is read/writeable by "other"s, i.e. anyone on the system.
I have made the required changes to the script and tried running it but am getting an error as below
Pseudo-terminal will not be allocated because stdin is not a terminal.
Can you please specify what this is about, I have googled it but it's not clear as in some portals they suggested to add -T and all, but its not working.