for loop in remotely executed bash script does not work - bash

I have a remote bash script, which goes through files using a for loop:
#!/bin/bash
for f in *.pdf;
do
echo $f
done
When executed locally:
server$./test.sh
1508.01585.pdf
1605.07683.pdf
When executed through ssh from a remote client:
client$ ssh user#server 'bash -c ~/papers/test.sh'
*.pdf
I have tried using ssh -T, bash -s, and nothing seems to work.
Please help!

Related

Connecting to SSH using a bash script

I am trying to write a bash script to make things faster. Is it not possible to connect to the server with the code below in a bash script? I can't make it work, even though it works in the terminal.
#!/bin/bash -x
echo "Starting connection script"
sh -i /home/EC2_KEY_HEHE.pem ubuntu#ec2-IP.blabla.amazonaws.com
What I get when I run is a not found output for each line in the pem file,
$ /home/EC2_KEY_HEHE.pem: 1: /home/EC2_KEY_HEHE.pem: -----BEGIN: not found
$ /home/EC2_KEY_HEHE.pem: 1: /home/EC2_KEY_HEHE.pem: adsnaleAFemasdsdsdnds: not foundMadfdasfdasfnda;vonraada
...
Some debug is needed.
Please change:
ssh -i /home/EC2_KEY_HEHE.pem ubuntu#ec2-IP.blabla.amazonaws.com
to:
#!/bin/bash -x
echo "Starting connection script"
ssh -vi /home/EC2_KEY_HEHE.pem ubuntu#ec2-IP.blabla.amazonaws.com
does it produce an idea about the reason ?
you are calling sh which is kind of shell change it to ssh

bash scripting; copy and chmod and untar files in multiple remote servers

I am a newbie to bash scripting. I am trying to copy a gz file, then change permissions and untar it on remote servers (all centos machines).
#!/bin/bash
pwd=/home/sujatha/downloads
cd $pwd
logfile=$pwd/log/`echo $0|cut -f1 -d'.'`.log
rm $logfile
touch $logfile
server="10.1.0.22"
for a in $server
do
scp /home/user/downloads/prometheus-2.0.0.linux-amd64.tar.gz
ssh -f sujatha#10.1.0.22 "tar -xvzf/home/sujatha/downloads/titantest/prometheus-2.0.0.linux-amd64.tar.gz"
sleep 2
echo
done
exit
The scp part is successfull. But not able to do the remaining actions. after untarring I also want to add more actions like appending a variable to the config files. all through the script. Any advise would be helpful
Run a bash session in your ssh connection:
ssh 192.168.2.9 bash -c "ls; sleep 2; echo \"bye\""

how to ssh a loop over several commands

I am new to ssh so forgive me if my questions are trivial..i need to make a a remote computer execute a set of commands several times so i was thinking about making a loop using ssh ..the problem is i don't know do i save those commands in a file and loop on that file or can i like save them in ssh and just call them ..i am really troubled..also if i make a loop like this
i= 10
while i!= 0
execute command.text file ???
i--
How to i tell it to execute the file ?
Just try first on the shell in the remote machine to run the command you want.
You will find plenty of info over the internet about loops in shell/bash/csh/whatevershell:
For instance assuming bash run in the remote host (from: http://www.bashoneliners.com/ )
$ for ((i=1; i<=10; ++i)); do echo $i; done
Once you learn that, simply then take that statement to the ssh command from the machine you want to trigger the action:
$ ssh user#remotehost 'for ((i=1; i<=10; ++i)); do echo $i; done'
You can write a simple script that will execute needed commands, and path it to ssh.
For example:
script.sh, it will iterate over your bunch of commands 10 times:
for i in $(seq 10)
do
command1
command2
command3
done
and path it to remote server for execution:
$ ssh $SERVERNAME < script.sh
If you have this command.text file in which you have written all the commands in column (you can modify them with vi or vim and put them in column), you don't even need to do a loop, you can simply do:
cat command.text | awk '{print "ssh user#remotehost "$0" "}' | sh -x
For example if command.text contains:
ls -lart
cd /tmp
uname -a
This will let you do all commands written in the command.text by doing ssh user#remotehost.

ssh script doesn't return control to the parent script

I am trying to execute a local script on the remote server, by writing it to standard input of an ssh command. The script runs fine, but then ssh doesn't exit: it just hangs, and control doesn't return to the parent script.
Parent Shell :
for HOSTNAME in ${HOSTS} ; do
ssh -t -t $HOSTNAME "bash -s" < ~backup_conf.sh
done
Called Script:
#!/bin/sh
AGENT_BASE_PATH=/home/lokesh
if [ -d "$AGENT_BASE_PATH/CI/DE_deployment/conf" ]; then
if [ -d "$AGENT_BASE_PATH/CI/temp/conf_bkup" ]; then
rm -rf $AGENT_BASE_PATH/CI/temp/conf_bkup
fi
cp -R $AGENT_BASE_PATH/CI/DE_deployment/conf $AGENT_BASE_PATH/CI/temp/conf_bkup
fi
exit
I have written 'exit' but the control is not returning back to the parent script.
It hangs at the remote server.... :(
Culprit is bash -s line in your calling code, which is still expecting input to be ended using ctrl-C:
Try this instead:
for HOSTNAME in ${HOSTS} ; do
ssh -t -t $HOSTNAME "bash ~backup_conf.sh"
done
write your exit - status into a file on the remote host and pick it later from the remote host with ssh/scp/sftp.
Direct via ssh you will not get a exit - status from the other host.

A script to ssh into a remote folder and check all files?

I have a public/private key pair set up so I can ssh to a remote server without having to log in. I'm trying to write a shell script that will list all the folders in a particular directory on the remote server. My question is: how do I specify the remote location? Here's what I've got:
#!/bin/bash
for file in myname#example.com:dir/*
do
if [ -d "$file" ]
then
echo $file;
fi
done
Try this:
for file in `ssh myname#example.com 'ls -d dir/*/'`
do
echo $file;
done
Or simply:
ssh myname#example.com 'ls -d dir/*/'
Explanation:
The ssh command accepts an optional command after the hostname and, if a command is provided, it executes that command on login instead of the login shell; ssh then simply passes on the stdout from the command as its own stdout. Here we are simply passing the ls command.
ls -d dir/*/ is a trick to make ls skip regular files and list out only the directories.

Resources