How To: Use a SSH connection within a shell script
Original: May 23, 2008
Last Update: April 12, 2011
In an automated script I wanted to connect to a remote server and transfer files from the remote server to the local server. Not a problem with ftp or scp but how do you delete the remote files and folders? There are many solutions I guess, I used the one with an automated SSH connection to the remote server to delete the remote files.
The magic word is EOT. With the following solution you are able to let a script run and connect itself to a remote server and send remote commands. To not run into a problem with a password prompt, I suggest you exchange the keys of local and remote server.
echo "This script connects to a remote server and executes commands there."
ssh username@hostname <<EOT
rm -r *
If you receive this error:
Pseudo-terminal will not be allocated because stdin is not a terminal.
tcsetattr: stdin: Invalid argument
You need to modify the ssh command to the following:
ssh -t -t username@hostname <<EOT
Personal Internet VMware PHP Linux Shell Bluecoat Proxy Windows Hardware Virtualization Nagios MySQL DB Monitoring Mail Android Network Wyse Hacks Tomcat Postgres Apple Mac Backup BSD ZFS Solaris SmartOS Unix Multimedia Perl Database MongoDB CMS OTRS FreeBSD Wordpress LXC Nginx Proxmox DNS Graphics GlusterFS Security Chef HAProxy Icinga Ansible HTML MariaDB Containers Rancher Docker AWS ELK Kibana Logstash Filebeat Varnish PGSQL PostgreSQL ElasticSearch CouchDB Bash Macintosh Container Minio Grafana InfluxDB Databases NFS OSSEC SystemD Java Zoneminder Surveillance Elasticsearch SSL TLS Icingaweb2 Cloud Wireless Kubernetes Ubuntu