So we have Embedded Linux board running Linux.
We can connect to that board using telnet and that spawns shell and gives access to it.
Now I am writing a bash script where I want to run commands on that shell and get its output.
e.g. My commands are something like below command over telnet and see if that was successful or not.
test -c /dev/null
When I run it like below I always get 1 as exit status
{ test -c /dev/null; sleep 1;} | telnet <board ip addr>
If possible I don't want to use expect,
Any suggestion/pointers ?
With SSH could trivially and robustly have done:
ssh yourhost 'test -c /dev/null'
With a simple shell on a TCP port, you could somewhat robustly but annoyingly have used:
echo 'test -c /dev/null; echo $?' | nc -q 1 yourhost 1234
telnet is instead notoriously timing sensitive and tricky to script, so since you don't want to do it robustly with expect, you can try to kludge it:
{ sleep 1; echo 'test -c /dev/null; echo $?'; sleep 1; } | telnet somehost
Related
I would like my script to run in the backround, SSH into another computer, run tcpdump, produce a pcap file and save it to my local computer. I have all of this working save for the running in the background portion.
I have looked at several solutions on Stack Overflow (example) but they don't seem to work for me. Admittedly I am a novice with bash however so it is entirely possible that I am reading them incorrectly.
ssh root#ipaddress "tcpdump -c 400000 -s 0 -U -n -w - -i eth0 not arp" &>/dev/null &disown \ > /root/Destop/BashPcap/01Bash.pcap
Check your quotation endings maybe that's the problem...
Or you can save the file remotely and download back using scp (SecureCoPy).
Eg:
scp root#ipaddress:/path/to/file ~/Documents/path-where you-want-to-save.pcap
As far as I understood your task this is what you want:
nohup ssh root#ipaddress "tcpdump -c 400000 -s 0 -U -n -w - -i eth0 not arp" &> /root/Destop/BashPcap/01Bash.pcap &
In simple words:
nohup - it will allow you to close your terminal and the script will continue to run
ssh ... - this is the command to execute
&> - redirect both stdout and stderr to file (Bash 4)
& - sends command to the background
Note: &> will send to the file both stdout and stderr, you need this if you want to have in your file the summary lines from tcpdump. They are written to stderr:
N packets captured
X packets received by filter
Y packets dropped by kernel
If you do not want to have these lines, then send stderr to /dev/null
nohup ssh root#ipaddress "tcpdump -c 400000 -s 0 -U -n -w - -i eth0 not arp" 2>/dev/null > /root/Destop/BashPcap/01Bash.pcap &
I would like to write a automated script to open telnet session and run some commands. The thing is, that this will be some kind of "logging", so i have to open pipe, and send some commands, and store outputs. I know, how to do this in a while loop like:
(while true
do
echo ${user}
sleep 1
echo ${pass}
sleep 1
echo ${something}
.
.
done)|telnet ${IP}
The problem here is that the telnet pipe is opened/closed in every loop and i want to achieve to open it at the beginning, and then send commands in a loop until some conditions are true.
NOTE: i am limited with commands as i am working with emb.system (such as spawn, expect, etc...)
Thanks for your help ! :)
BR.
Does this work for you?
(echo ${user}
sleep 1
echo ${pass}
sleep 1
while true; do
echo ${something} | tee -a /tmp/logfile.txt
.
.
done
echo "exit") | telnet ${IP} | tee -a /tmp/logfile.txt
you can use sshpass soft.
http://sourceforge.net/projects/sshpass/
tar -zxvf sshpass-1.05.tar.gz
cd sshpass-1.05
./configure
make && make install
.............
The following code suppose to print "connected" repeatedly if ssh is able to connect to the remote machine, else "not-connected" will be printed.
#!/bin/bash
while [ "1" ]
do
ifconfig usb0 &>/dev/null
if [ "$(echo $?)" == "0" ]
then
sshpass -p passwd ssh -o ConnectTimeout=5 duser#10.1.1.3 sudo echo "connected"
else
echo "not-connected"
fi
sleep 0.2
done
The remote machine is connected via the usb0 interface. I am testing this script by frequently unplugging and plugging the usb interface. The problem is sometimes when i unplug the usb interfaces, the code stuck at ssh and it is not printing repeatedly. I suppose if ssh is interrupted in the middle of executing it is getting stuck. How can i overcome this ? Can someone help?
Most apparently its not hanging at all.
But the way you are printing connected seems like wrong.
This looks pretty much suspicious
sshpass -p passwd ssh -o ConnectTimeout=5 duser#10.1.1.3 sudo grep "connected"
What are you trying to grep ?
Normally if you need to grep the output of some app you need to do it via pipe e.g.
ls | grep "connected"
Since you are using bash, this is a much better way to do your test:
2>/dev/null >/dev/tcp/<ip_addr>/<port> && echo pass || echo fail
For more details, see the Bash documentation on redirections.
I would like to ping a IP-Adress an want to be notified.
System: Linux Fedora.
Has anybody a idea or a Software?
Use this shell script. Found on http://jeromejaglale.com/doc/unix/shell_scripts/ping
#! /bin/sh
# -q quiet
# -c nb of pings to perform
ping -q -c5 google.com > /dev/null
if [ $? -eq 0 ]; then
echo "ok"
fi
Put the ping in a loop or run the script with cron.
Instead of echoing you can send a notification.
I would like to start mbuffer in listening mode on a remote server, so I do
ssh -f root#10.10.10.46 'mbuffer -4 -v 0 -q -I 8023 > /tmp/mtest & echo $!'
and it outputs the PID number, which I would like to save in $pidValue, but if I do
pidValue=$(ssh -f 10....)
then it doesn't exit until the mbuffer process exits.
Question
How do I get the PID value from the mbuffer process?
before running the process which uses the remote PID, scp the remote file to local storage, from where you deal with it as you like.
it seems that this construct:
read -r var < <(ssh remote "echo test; sleep 5") ; echo $var
can give you results earlier, but that may depend on the remote process you're starting. Try that.