sshpass from iplist and run tcptraceroute - bash

Number of servers in a list = 40
I want to run tcptraceroute on each server for all 40 ips in the list. for this i need to loop sshpass and run tcptracetraceroute.
When i run the below code, it just runs for first ip in list and use same ip to run tcptraceroute and exits.
IFS=$IFS,
USER='*********'
PASSWORD='********'
PORT='22'
while read ip; do
sshpass -p $PASSWORD ssh -i turbot -t -o StrictHostKeyChecking=no
$USER#$ip "sudo -s /usr/bin/tcptraceroute "$ip" $PORT" >>Res.txt
done < .PrivateIP-List.txt
I'm trying to output the traceroute results to Res.txt file

Related

Bash While loop is not looping

Im doing a backup script for Mikrotik devices using bash. I want to use a while loop that will read from a file.
The Contents of the file to read from will be like:
1.1.1.1 router1
2.2.2.2 router2
Here is my sample script:
while read -r ip name
do
sshpass -p "pass" ssh -o StrictHostKeyChecking=no export\#$ip "/export;/quit" 2> errors.log >> $name.export
done < iplistandnamefile
The problem is that the script will work for only the 1st IP address on the iplistandnamefile file but will not loop to the 2nd IP.
Any suggestions ?
ssh is eating the rest of the file as it defaults to reading stdin
add -n to the ssh command.
sshpass -p "pass" ssh -no StrictHostKeyChecking=no user#host command_to_run

writing shell ssh script for uploading compiled sketch on multiple arduino yun in network

I work with a couple of arduino yuns and want to write a script to upload sketches on multiple of them. Let's assume I have a compiled arduino program:sketch.hex.
Now I'd like to upload this file via LAN. For a single device it works like this.
Copying the sketch onto the device. (password required)
scp sketch.hex root#yun1.local:/tmp/sketch.hex
Opening an ssh session with the device. (password required)
ssh root#yun1.local
And then load the program onto the Atmega with the following 2 commands.
merge-sketch-with-bootloader.lua /tmp/sketch.hex
run-avrdude /tmp/sketch.hex
Now my question would be, how to do this for multiple arduinos (yun1,yun2,...,yunN) without entering actually ssh-ing into each single device in order to run the bottom 2 commands.
Hope the question is not too confusing and thanks a lot in advance.
Update: could figure it out myself. Here is the code in case someone needs it.
#!/bin/sh
# globalUpload.sh
#
#
# Created by maggu on 21/02/16.
#
clear
FILENAME="valve_adjusting.hex"
SSHPASS="doghunter"
SSHCOMMAND="ssh -p 22 -T -o StrictHostKeyChecking=no -o BatchMode=no"
PREFIX="root#linino"
PREFIXO="linino"
SUFFIX=".local"
YUNS=8
for i in `seq 1 $YUNS`
do
SSHACCOUNT=$PREFIX$i$SUFFIX
ssh-keygen -R $PREFIXO$i$SUFFIX
sshpass -p "doghunter" scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null sketch.hex $SSHACCOUNT:/tmp/sketch.hex
sshpass -p $SSHPASS $SSHCOMMAND $SSHACCOUNT << EOF_run_commands
merge-sketch-with-bootloader.lua /tmp/sketch.hex
run-avrdude /tmp/sketch.hex
EOF_run_commands
done
#!/bin/sh
# globalUpload.sh
#
#
# Created by maggu on 21/02/16.
#
clear
FILENAME="valve_adjusting.hex"
SSHPASS="doghunter"
SSHCOMMAND="ssh -p 22 -T -o StrictHostKeyChecking=no -o BatchMode=no"
PREFIX="root#linino"
PREFIXO="linino"
SUFFIX=".local"
YUNS=8
for i in `seq 1 $YUNS`
do
SSHACCOUNT=$PREFIX$i$SUFFIX
ssh-keygen -R $PREFIXO$i$SUFFIX
sshpass -p "doghunter" scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null sketch.hex $SSHACCOUNT:/tmp/sketch.hex
sshpass -p $SSHPASS $SSHCOMMAND $SSHACCOUNT << EOF_run_commands
merge-sketch-with-bootloader.lua /tmp/sketch.hex
run-avrdude /tmp/sketch.hex
EOF_run_commands
done

Waiting for input from script that is running remotely via ssh

There is a script I'm running that I can not install on the remote machine.
clear && printf '\e[3J'
read -p "Please enter device: " pattern
read -p "Enter date: (YYYY-MM-DD): " date
pfix=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 5 | head -n 1)
mkdir /home/user/logCollectRes/"${pfix}"
ssh xxx.xxx.xxx.xxx 'bash -s' < /usr/local/bin/SearchAdvanced.sh ${pattern} ${date} ${pfix}
In that script, I would like to be able to use read.
ls -g *"${pattern}"*
read -p "Select one of these? [y/n] " "found";
I've tried adding the -n on the read as well as the -t -t option on ssh. As you can see the script presents information that is only seen once the script starts, so I can't use the read on local machine.
EDIT: So lets say server B stores syslogs for 5K computers. The file names are given by using the internal IP of the device and the date at the end.
/var/log/remotes/192.168.1.500201505050736.gz
/var/log/remotes/192.168.1.500201505050936.gz
/var/log/remotes/192.168.1.500201505051136.gz
/var/log/remotes/192.168.1.600201505050836.gz
/var/log/remotes/192.168.1.600201505051036.gz
/var/log/remotes/192.168.1.600201505051236.gz
I'd like to be able to select the IP address from the main script, list all the files matching that IP address, and then select which I want to scp to my local machine.
After speaking with some coworkers I found the answer to be running two scripts: The first pulls the ls -g result and directs the answer to a variable on the local machine. I then print that output with the read option of selecting on of the files. The 2nd script will take that answer and scp the file from the remote machine
In the main script
ssh xxx.xxx.xxx.xxx 'bash -s' < /usr/local/bin/SearchAdvanced.sh ${pattern} ${date} > ${result}
then as a follow up
printf "${result}"
read -p "Select file: "

Connect to multiple ssh connections through scripts

I have been trying to automatically enter a ssh connection using a script. This previous SOF post has helped me so far. Using one connection works (the first ssh statement). However, I want to create another ssh connection once connected, which I thought could look like this:
#! /bin/bash
# My ssh script
sshpass -p "MY_PASSWORD1" ssh -o StrictHostKeyChecking=no *my_hostname_1*
sshpass -p "MY_PASSWORD2" ssh -o StrictHostKeyChecking=no *my_hostname_2*
When running the script, I get only connected to the my_hostname_1 and the second ssh command is not run until I exit the first ssh connection.
I've tried using an if statement like this:
if [ "$HOSTNAME" = my_host_name_1 ]; then
sshpass -p "MY_PASSWORD2" ssh -o StrictHostKeyChecking=no *my_hostname_2*
fi
but I can't get any commands to be read until I exit the first connection.
Here is a ProxyCommand example as suggested by #lihao:
#!/bin/bash
sshpass -p "MY_PASSWORD2" ssh -o StrictHostKeyChecking=no \
-o ProxyCommand='sshpass -p "MY_PASSWORD1" ssh m_hostname_1 netcat -w 1 %h %p' \
my_hostname_2
You are proxying through the first host to get to the second. This assumes you have netcat installed on my_hostname_2. If not, you'll need to install it.
You can also set this up in your ~/.ssh/config file so you don't need the proxy stuff on the command line:
Host my_hostname_1
HostName my_hostname_1
Host my_hostname_2
HostName my_hostname_2
ProxyCommand ssh my_hostname_1 netcat -w 1 %h %p
However, this is a little trickier with the password handling. While you could put the sshpass here, it's not a great idea to have passwords in plain text. Using key based authentication might be better.
A Bash script is a sequence of commands.
echo moo
echo bar
will run echo moo and wait for it to complete, then run the next command.
You can run a remote command like this:
ssh remote echo moo
which will connect to remote, run the command, and exit. If there are additional commands in the script file after this, the shell which is executing these commands will continue with the next one, obviously on the host where you started the script.
To connect to one host from another, you could in principle do
ssh host1 ssh host2
but the proxy command suggested by #zerodiff improves on several aspects of the experience.

how can know ssh is disconected and retry with bash script

I'm using reverse ssh for connecting to remote client , Operator run reverse one time and leave client system
how can i write bash script , when reverse ssh disconnected from server retry to connect to server (ssh)
Use autossh. Autossh "automatically restart[s] SSH sessions and tunnels"
sudo apt-get install autossh
I use autossh to to keep open reverse tunnel that I depend on. It works very well, even with long periods of lost connection.
Here is the script I use to create the tunnel:
#!/bin/bash
AUTOSSH_GATETIME=0
export AUTOSSH_GATETIME
autossh -f -N -R 8022:localhost:22 username#host -o "ServerAliveInterval 45" -o "ServerAliveCountMax 2"
I execute this script at boot with this cronjob:
#reboot /home/scripts/./persistent-tunnel.sh
If you simply want to retry a command until it succeeds, you can use this pattern:
while ! ssh [...]
do
echo "Command failed, retrying..." >&2
done
I have a slightly different method.
My method always tries to reconnect you if you have a dirty disconnection: '~.' or 'Connection closed by remote host.'
But if you disconnect with 'CRTL+D' or with 'exit' it just disconnects and show you some info of the connections.
I have a slightly different method.
My method always tries to reconnect you if you have a dirty disconnection: '~.' or 'Connection closed by remote host.'
But if you disconnect with 'CRTL+D' or with 'exit' it just disconnects and show you some info of the connections.
#/bin/bash
if [ -z "$1" ]
then
echo '''
Please also provide ssh connection details.
'''
exit 1
fi
retries=0
repeat=true
today=$(date)
while "$repeat"
do
((retries+=1)) &&
echo "Try number $retries..." &&
today=$(date) &&
ssh "$#" &&
repeat=false
sleep 5
done
echo """
Disconnected sshx after a successful login.
Total number of tries = $retries
Connected at:
$today
"""
You might want to take a look into ssh options ServerAliveInterval, ServerAliveCountMax and TCPKeepAlive because sometimes your line dies without making this obvious, let me demonstrate:
#!/bin/sh
while true; do
ssh -T user#host \
-o IdentityFile=~/.ssh/tunnel \
-o UserKnownHostsFile=~/.ssh/known_hosts.tunnel \
pkill -f "^sshd:\ user\ \ \ \ $" # needs to be edited for nearly every case
sleep 2
ssh -T -N user#host \
-o IdentityFile=~/.ssh/tunnel \
-o UserKnownHostsFile=~/.ssh/known_hosts.tunnel \
-o Batchmode=yes \
-o ExitOnForwardFailure=yes \
-o ServerAliveCountMax=1 \
-o ServerAliveInterval=60 \
-o LocalForward=127.0.0.1:2501=127.0.0.1:25 \
-o RemoteForward=127.0.0.1:2501=127.0.0.1:25
sleep 60
done
You can use netstat -ntp | grep ":22" or ss -ntp | grep ":22" to see established connections to ssh port, then use grep to filter the ip address you're looking for. If you don't find a connection then reconnect the tunnel.
Use autossh if it works on your version of Linux. It did not on mine as it was an outdated Linux distribution for a custom NAS box.
The alternative is a simple bash script in crontab like this:
maintain_reverse_ssh_tunnel.sh
if ! netstat -planet |grep myserver_ip_or_name |grep ESTABLISHED > /dev/null; then
echo "REVERSE SSH DOWN - Restarting the tunnels"
ssh -fN -R 32999:localhost:22 -R 28080:localhost:80 myusername#myserver_ip_or_name
fi
Replace myusername and myserver_ip_or_name with those of your user and server.
Then add an entry to crontab by typing crontab -e and adding the following line:
1 * * * * /path_to_my_script/maintain_reverse_ssh_tunnel.sh
Make sure to have the execute permissions on the script:
chmod 755 maintain_reverse_ssh_tunnel.sh

Resources