Running jq on a remote machine over ssh and overwrite the file - linux

I am trying to create a file from the output of jq command over ssh command.
ssh <server-Name> "jq '.credsStore = "ecr-login"' ~/.docker/config.json > ~/.docker/output.json "
It gives me following error:
bash: .docker/output.json: No such file or directory
Am I not running the command properly or is there any other problem?

ssh "$server" "bash -s" <<'EOF'
[[ -e ~/.docker/config.json ]] || {
echo "ERROR: $HOME/.docker/config.json does not exist on the remote server" >&2
exit 1
}
jq '.credsStore = "ecr-login"' \
<~/.docker/config.json \
>~/.docker/output.json
EOF

Related

Calling multiple shell scripts within a script on different virtual machines

I am trying to create shell scripts which will setup Zookeeper Server in one VM, and its corresponding Zookeeper Clients in different VM's so i written a shell script as below
#!/bin/bash
ZOOKEEPER_SERVER_IP="1.2.3.4"
while read ipaddress zookeepertype number
do
echo -e "Setting up the Zookeepers \n"
echo $ipaddress
if [ "${zookeepertype}" = 'zookeeperserver' ]; then
echo "Setup Zookeeper Server"
#ZOOKEEPER_SERVER_IP = $ipaddress
#echo $ZOOKEEPER_SERVER_IP
#echo $ipaddress
sudo scp -i /home/ubuntu/.ssh/fd -r /home/ubuntu/ZooKeeper_Server_Script.sh ubuntu#$ipaddress:/home/ubuntu/
ssh -i /home/ubuntu/.ssh/fd ubuntu#$ipaddress /home/ubuntu/ZooKeeper_Server_Script.sh
echo "This script is about to run ZooKeeper_Server_Script."
echo "The server script has completed.";
#sleep 30
exit 1
fi
echo -e $ZOOKEEPER_SERVER_IP
if [ $zookeepertype = "zookeeperclient" ] ; then
echo "Setup Zookeeper Client"
echo $ipaddress
sudo scp -i /home/ubuntu/.ssh/fd -r /home/ubuntu/ZooKeeper_Client_Script.sh ubuntu#$ipaddress:/home/ubuntu/
ssh -i /home/ubuntu/.ssh/fd ubuntu#$ipaddress
#mkdir /home/ubuntu/keyfiles
#exit
#sudo scp -i /home/ubuntu/.ssh/fd -r /home/ubuntu/abc/network/test/keyfiles/* ubuntu#$ipaddress:/home/ubuntu/keyfiles
#sudo scp -i /home/ubuntu/.ssh/fd -r /home/ubuntu/abc/test/simple/abc.json ubuntu#$ipaddress:/home/ubuntu/
#ssh -i /home/ubuntu/.ssh/fd ubuntu#$ipaddress
#chmod 777 ZooKeeper_Client_Script.sh
#echo "This script is about to run ZooKeeper_Client_Script."
#sh ./ZooKeeper_Client_Script.sh $ZOOKEEPER_SERVER_IP
echo "The client script has completed."
#exit
fi
#Separating Runhosts File
done < setupZkinput.txt
the input file is
1.2.3.4 zookeeperserver 1
5.6.7.8 zookeeperclient 2
9.10.11.12 zookeeperclient 3
The issue that i am facing is
1) Only the server setup is being done , i.e the script is exiting after the first line
2)Not able to assign the server ip dynamically , in the line ZOOKEEPER_SERVER_IP = $ipaddress
Thanks
What is the default permission you are setting up after copying a file to the server?
remember it required execute permission in order to execute script.

What is the use of "bash -c" command?

What is the use of bash -c command, while surfing on the internet I came across the command
ssh -t "${SSH_USER}#${SERVER_IP}" bash -c
First part here is doing ssh obviously to a server but what exactly bash -c is responsible for?
bash -c option with ssh is one such method to execute multi line commands on the ssh server with variable expansion.
Say you have
VAR1="Variable 1"
ssh app#HOSTNAME '
ls
pwd
if true; then
echo "True"
echo $VAR1 # <-- it won't work
else
echo "False"
fi
'
But with bash -c
VAR1="Variable 1"
ssh -t "${SSH_USER}#${SERVER_IP}" bash -c "'
ls
pwd
if true; then
echo $VAR1 <-- This works
else
echo "False"
fi
'"

How to log non-interactive bash command sent through ssh

I'm sending a command through ssh:
ssh server.org 'bash -s' << EOF
ls -al
whoami
uptime
EOF
How to log it in the system (remote server)? I'd like to log those commands in some file (.bash_history or /tmp/log).
I've tried to add the line below to sshd_config:
ForceCommand if [[ -z $SSH_ORIGINAL_COMMAND ]]; then bash; else echo "$SSH_ORIGINAL_COMMAND" >> .bash_history; bash -c "$SSH_ORIGINAL_COMMAND"; fi
But it logs "bash -s" only.
I'll appreciate any help.
When bash shell exits, bash reads and executes commands from the ~/.bash_logout file. Probably you can run the history command at the end in the .bash_logout(of the server) and save it to some location.
If it suffices to work with the given command, we can put the necessary additions to enable and log command history at the beginning and end, e. g.
ssh server.org bash <<EOF
set -o history
ls -al
whoami
uptime
history|sed 's/ *[0-9]* *//' >>~/.bash_history
EOF
Or we could put them into the awfully long ForceCommand line:
… if [[ "$SSH_ORIGINAL_COMMAND" == bash* ]]; then echo "set -o history"; cat; echo "history|sed 's/ *[0-9]* *//' >>~/.bash_history"; else cat; fi | bash -c "$SSH_ORIGINAL_COMMAND"; fi

bash script accessing ec2 instance

This script required an ip and the script or file that we have to run on the remote server i gave a file in which i have wrote commands like
touch /root/test
ls /root/test
this make the file but do not show and it is displaying an error
tcgetattr: Inappropriate ioctl for device
connection closed
How can I resolve this is there any suggestion ??
#!/bin/bash
# The private key used to identify this machine
IDENTITY_KEY=/home/admnew.pem
syntax()
{
echo "Syntax: Ec2.sh server_ip scriptFile]"
echo "For example: ./Ec2.sh server_ip scriptFile"
exit 1
}
if [ $# -ne 2 ]
then
echo not enough arguments
syntax
fi
echo "Running script $2 on $1"
ssh -t -t -i $IDENTITY_KEY ec2-user#$1 sudo -i 'bash -s' < $2
exit
exit
echo "Done"
Try:
ssh -t -t -i $IDENTITY_KEY ec2-user#$1 sudo -i 'bash -s' <<EOF
(
$(cat "$2")
)
EOF
e.g. wrap the script into (), e.g the:
touch /root/test
ls /root/test
should be
(
touch /root/test
ls /root/test
)

shell script ssh command not working

I have a small list of servers, and I am trying to add a user on each of these servers. I can ssh individually to each server and run the command.
sudo /usr/sbin/useradd -c "Arun" -d /home/amurug -e 2014-12-12 -g users -u 1470 amurug
I wrote a script to loop through the list and run this command but I get some errors.
#!/bin/bash
read -p "Enter server list: " file
if [[ $file == *linux* ]]; then
for i in `cat $file`
do
echo "creating amurug on" $i
ssh $i sudo /usr/sbin/useradd -c "Arun" -d /home/amurug -e 2014-12-12 -g users -u 1470 amurug
echo "==============================================="
sleep 5
done
fi
When I run the script it does not execute the command.
creating amurug on svr102
Usage: useradd [options] LOGIN
Options:
What is wrong with my ssh crommand in my script?
Try this script:
#!/bin/bash
read -p "Enter server list: " file
if [[ "$file" == *linux* ]]; then
while read -r server
do
echo "creating amurug on" "$server"
ssh -t -t "$server" "sudo /usr/sbin/useradd -c Arun -d /home/amurug \
-e 2014-12-12 -g users -u 1470 amurug"
echo "==============================================="
sleep 5
done < "$file"
fi
As per man bash:
-t
Force pseudo-tty allocation. This can be used to execute arbitrary screen-based programs on a remote which can be very useful, e.g. when implementing menu services. Multiple -t options force tty allocation, even if ssh has no local tty.

Resources