shell script executing network configuration hangs over tmux - shell

I am executing a shell script which is doing some network configuration.
when i execute this script using tmux it hangs.
===
Here how I am executing tmux in my bash
ARGS=$*
tmux new-session -d -s ISA_INSTALL "./installaccess $ARGS"
tmux attach
=====
Command which hangs is
0 16:54:33 cmd /usr/bin/ssh -x -o NumberOfPasswordPrompts=0 -o StrictHostKeyChecking=no -o GSSAPIAuthentication=no -o ControlMaster=auto -o ControlPath=/var/tmp/installaccess-201607251641FNK/_ssh_%h -o ControlPersist=yes 10.209.192.171 "LC_ALL=C; LANG=C; export LC_ALL LANG; /sbin/ip addr flush eth2 >/dev/null 2>&1; /sbin/ifconfig eth2 down >/dev/null 2>&1" 2>/dev/null
0 17:08:18 CPI WARNING V-9-20-1051 Interrupt Received--installaccess terminated

Related

Empty ssh invitation (no "user#host:~$") when run command after connect (sh script, sshpass)

Client OS: MacOS 12.1, Server OS: Linux Debian 9 (any server)
case 1:
#!/bin/bash
sshpass -p mypass ssh user#host.ru -o StrictHostKeyChecking=no
works fine:
case 2:
#!/bin/bash
sshpass -p mypass ssh user#host.ru -o StrictHostKeyChecking=no "cd /var/www ; git status ; /bin/bash"
Output of "git status" works fine, but
no "user#host:~$" message in output (input is active).
I tried:
/bin/bash
bash -l
(in server "echo $SHELL" shows /bin/bash)
How to fix it?
Use ssh -t and && inside commands list
#!/bin/bash
sshpass -p mypass ssh -t user#host.ru -o StrictHostKeyChecking=no "cd /var/www && git status && /bin/bash"

Get output from a shell script that does ssh two level

I have two shell scripts like below:
Script1:
ssh -q -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null my_username#jump_box <<EOF
ls
ssh -q -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null actual_host <<EOF1
sudo docker ps --format='{{json .}}'
EOF1
EOF
Script2:
details=nothing
ssh -q -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null my_username#jump_box <<EOF
ls
details=$(ssh -q -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null actual_host "sudo docker ps --format='{{json .}}'")
EOF
echo "${details}"
I need the docker details in a varilable in my local machine so that I can do some operations on it. The first script runs fine and I can see the output of the docker command on my local machine but the second script doesn't work. It seems to be hung/stuck and doesn't do anything and I have to forcefully quit it.
Like the comment from #Gordon Davisson, use a jumpbox.
But you can define it in the ~/.ssh/config file, too.
HOST my_jump_box
hostname jump_box
user my_username
StrictHostKeyChecking no
UserKnownHostsFile /dev/null
HOST actual
hostname actual_hostname
user actual_user
ProxyJump my_jump_box
StrictHostKeyChecking no
UserKnownHostsFile /dev/null
RemoteCommand sudo docker ps --format='{{json .}}'"
Then you can just use ssh actual
To fetch the output details=$(ssh actual).
Btw. Your specific problem could also be solved by changing script2 to:
#!/bin/bash
details=$(./script1)
echo "$details"

(Error while running a command with SSH) command-line: line 0: Bad configuration option

Error Msg:
command-line: line 0: Bad configuration option:
sh '''ssh -i ${rundeck_rsa_key} -o StrictHostKeyChecking=no -o centos#xxxx.net "sudo su -c "sh ./home/centos/releases/xx.sh" rundeck"'''
Broken Down command (I just made the above command for your convenience)
sh '''ssh -i ${rundeck_rsa_key} -o StrictHostKeyChecking=no
-o centos#xxxx.net "sudo su -c "sh ./home/centos/releases/xx.sh" servc"'''
I'm trying to
ssh into the server
change user to "servc"
execute xx.sh shell
I think there is a syntax error on "sudo su -c "sh ./home/centos/releases/xx.sh" servc"
Do you have any clue?? :D
You can't nest a double quoted string inside another without escaping the inner ones.
Try this:
sh '''ssh -i ${rundeck_rsa_key} -o StrictHostKeyChecking=no -o centos#xxxx.net "sudo su -c \"sh ./home/centos/releases/xx.sh\" rundeck"'''

Weird output observed on executing ssh commands remotely over ProxyCommand

Team, I have two steps to perform:
SCP a shell script file to remote ubuntu linux machine
Execute this uploaded file on remote ubuntu linux machine over SSH session using PROXYCommand because I have bastion server in front.
Code:
scp -i /home/dtlu/.ssh/key.key -o "ProxyCommand ssh -i /home/dtlu/.ssh/key.key lab#api.dev.test.com -W %h:%p" /home/dtlu/backup/test.sh lab#$k8s_node_ip:/tmp/
ssh -o StrictHostKeyChecking=no -i /home/dtlu/.ssh/key.key -o 'ProxyCommand ssh -i /home/dtlu/.ssh/key.key -W %h:%p lab#api.dev.test.com' lab#$k8s_node_ip "uname -a; date;echo "Dummy123!" | sudo -S bash -c 'echo 127.0.1.1 \`hostname\` >> /etc/hosts'; cd /tmp; pwd; systemctl status cachefilesd | grep Active; ls -ltr /tmp/test.sh; echo "Dummy123!" | sudo -Sv && bash -s < test.sh"
Both calls above are working fine. I am able to upload test.sh and also its running but what is bothering me is during the process am observe weird output being thrown out.
output:
/tmp. <<< expected
[sudo] password for lab: Showing one
Sent message type=method_call sender=n/a destination=org.freedesktop.DBus object=/org/freedesktop/DBus interface=org.freedesktop.DBus member=Hello cookie=1 reply_cookie=0 error=n/a
Root directory /run/log/journal added.
Considering /run/log/journal/df22e14b1f83428292fe17f518feaebb.
Directory /run/log/journal/df22e14b1f83428292fe17f518feaebb added.
File /run/log/journal/df22e14b1f83428292fe17f518feaebb/system.journal added.
So, I don't want /run/log/hournal and other lines which don't correspond to my command in sh.
Consider adding -q to the scp and ssh commands to reduce the output they might produce. You can also redirect stderr and stdout to /dev/null as appropriate.
For example:
{
scp -q -i /home/dtlu/.ssh/key.key -o "ProxyCommand ssh -i /home/dtlu/.ssh/key.key lab#api.dev.test.com -W %h:%p" /home/dtlu/backup/test.sh lab#$k8s_node_ip:/tmp/
ssh -q -o StrictHostKeyChecking=no -i /home/dtlu/.ssh/key.key -o 'ProxyCommand ssh -i /home/dtlu/.ssh/key.key -W %h:%p lab#api.dev.test.com' lab#$k8s_node_ip "uname -a; date;echo "Dummy123!" | sudo -S bash -c 'echo 127.0.1.1 \`hostname\` >> /etc/hosts'; cd /tmp; pwd; systemctl status cachefilesd | grep Active; ls -ltr /tmp/test.sh; echo "Dummy123!" | sudo -Sv && bash -s < test.sh"
} >&/dev/null

Bash script ignoring command on for loop

I'm running a basic for loop bash script that logs into multiple linux boxes to execute a script and after the script logs out of every box, to wait 60 seconds. The problem is that the script is not applying the command within the script.
#!/bin/bash for i in `cat test.txt`; do ssh -A -t -o userknownhostsfile=/dev/null -o stricthostkeychecking=no -o batchmode=yes -o connecttimeout=5 $i "sudo puppet agent -t & "; sleep 60; done

Resources