» IT tipps and howto's

How To: Use a SSH connection within a shell script

Original: May 23, 2008
Last Update: April 12, 2011

Introduction
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.

Solution
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.

#!/bin/bash
echo "This script connects to a remote server and executes commands there."
ssh username@hostname <<EOT
cd /home/someuser
ls -l
rm -r *
exit
EOT
exit 0

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

Source: http://www.unix.com/unix-advanced-expert-users/26124-pseudo-terminal-will-not-allocated-because-stdin-not-terminal.html

RSS feed

Blog Tags:

  AWS   Android   Ansible   Apache   Apple   Atlassian   BSD   Backup   Bash   Bluecoat   CMS   Chef   Cloud   Coding   Consul   Containers   CouchDB   DB   DNS   Database   Databases   Docker   ELK   Elasticsearch   Filebeat   FreeBSD   Galera   Git   GlusterFS   Grafana   Graphics   HAProxy   HTML   Hacks   Hardware   Icinga   Influx   Internet   Java   KVM   Kibana   Kodi   Kubernetes   LVM   LXC   Linux   Logstash   Mac   Macintosh   Mail   MariaDB   Minio   MongoDB   Monitoring   Multimedia   MySQL   NFS   Nagios   Network   Nginx   OSSEC   OTRS   Office   PGSQL   PHP   Perl   Personal   PostgreSQL   Postgres   PowerDNS   Proxmox   Proxy   Python   Rancher   Rant   Redis   Roundcube   SSL   Samba   Seafile   Security   Shell   SmartOS   Solaris   Surveillance   Systemd   TLS   Tomcat   Ubuntu   Unix   VMWare   VMware   Varnish   Virtualization   Windows   Wireless   Wordpress   Wyse   ZFS   Zoneminder