shell script ssh command not working - linux

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.

Related

I want to create a script that automatically inputs password to ssh client to do reboot on /etc/rc.local

The code manually is:
ssh -fNv -L 3049:localhost:2049 ubuntu#20.115.5.61
Now I have in /etc/rc.local:
#!/bin/bash
remote_host=ubuntu#20.115.5.61
remote_port=3049
local_port=2049
cmd="ssh -fN -R ${remote_port}:localhost:${local_port} ${remote_host}"
while true; do
pgrep -fx "$cmd" >/dev/null 2>&1 || $cmd
sleep 10
done
¿Help me?

Error when stacking SSH command arguments within a bash script using other scripts as variables

I have a csv file called addresses.csv which looks like this,
node-1,xx.xxx.xx.xx,us-central-a
....
node-9,xxx.xx.xxx.xx,us-east1-a
I have a script below called 0run.sh,
#!/bin/bash
username='user'
persist="bash /home/${username}/Documents/scripts/disk/persistentDisk.sh"
first="bash /home/${username}/Documents/scripts/disk/firstAttach.sh"
while IFS=, read -r int ip <&3; do
if [ "$int" == "node-1" ]; then
--->ssh -i ~/.ssh/key -o StrictHostKeyChecking=no -l ${username} ${ip} "${persist}; ${first}"<---
else
ssh -i ~/.ssh/key -o StrictHostKeyChecking=no -l ${username} ${ip} "${first}"
fi
done 3<addresses.csv
The error occurs in the part of the code where I drew the arrows.
When it runs on node-1, instead of running ..persistentDisk.sh followed by ..firstAttach.sh, it only runs ..persistentDisk.sh and gives me the following error before it runs ..persistentDisk.
bash: /home/user/Documents/scripts/disk/firstAttach.sh: No such file or directory
The rest of the script runs completely fine. The only error occurs at this one part where it misses the 2nd script.
When I run the command like this it runs fine.
ssh -i ~/.ssh/key -o StrictHostKeyChecking=no -l ${username} ${ext} "${first}"
When I run it like this, it runs fine as well.
ssh -i ~/.ssh/key -o StrictHostKeyChecking=no -l user xxx.xx.xxx.xx "bash /home/${username}/Documents/scripts/disk/persistentDisk.sh; bash /home/${username}/Documents/scripts/disk/firstAttach.sh"
When I run the command like with a \ before the ; to escape it like this,
ssh -i ~/.ssh/key -o StrictHostKeyChecking=no -l ${username} ${ext} "${persist}\; ${first}"
I get the following error, and neither scripts run within the node-1 part of the code, but the rest of the code's else loops run fine.
bash: /home/user/Documents/scripts/disk/persistentDisk.sh;: No such file or directory
Why can't I stack the 2 commands within the if statement in the ssh using variables?
If I clearly understand: your real problem consist to leave STDIN free for interaction in target host!
About read and redirection
Try using:
#!/bin/bash
username='user'
persist="bash /home/${username}/Documents/scripts/disk/persistentDisk.sh"
first="bash /home/${username}/Documents/scripts/disk/firstAttach.sh"
while IFS=, read -r -u $list int ip foo; do
if [ "$int" == "node-1" ]; then
echo CMD... $ip, $persist
else
[ "$ip" ] && echo CMD... $ip, $first
fi
done {list}<addresses.csv
Tested, this èroduce:
CMD... xx.xxx.xx.xx, bash /home/user/Documents/scripts/disk/persistentDisk.sh
CMD... xxx.xx.xxx.xx, bash /home/user/Documents/scripts/disk/firstAttach.sh
-u flag to read, tell to use file descriptor ${list} instead of STDIN
foo is some useless variable used to prevent rest of line to be stored in $ip (xx.xxx.xx.xx,us-central-a in this case)
{list}</path/to/filename create a new variable by finding any free file descriptor.
About ssh (and redirection)
You could use:
#!/bin/bash
username='user'
persist="/home/${username}/Documents/scripts/disk/persistentDisk.sh"
first="/home/${username}/Documents/scripts/disk/firstAttach.sh"
while IFS=, read -r -u $list int ip foo; do
[ "$int" = "node-1" ] && cmd=persist || cmd=first
[ "$ip" ] && ssh -i ~/.ssh/key -t -o StrictHostKeyChecking=no \
-l ${username} ${ext} /bin/bash "${!cmd}"
done {list}<addresses.csv
By using this syntax, you will keep STDIN free for script running on target host.

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.

Working around sudo in shell script child process

So the reason I am asking this is because I'm running two programs simultaneously that are persistent, on the child process a programm is running that requires sudo rights.
#!/bin/bash
echo "Name the file:"
read filename
while [[ 1 -lt 2 ]]
do
if [ -f /home/max/dump/$filename.eth ]; then
echo "File already exist."
read filename
else
break
fi
done
#Now calling a new terminal for dumping
gnome-terminal --title="tcpdump" -e "sh /home/max/dump/dump.sh $filename.eth"
ping -c 1 0 > /dev/null **Waiting for tcpdump to create file**
#Packet analysis program is being executed
Script dump.sh
#!/bin/bash
filename=$1
echo password | sudo tcpdump -i 2 -s 60000 -w /home/max/dump/$filename -U
host 192.168.3.2
#Sudo still asks me for my password though password is piped into stdin

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
)

Resources