bash script read server and not read password - bash

I use curl to open with username and password router
curl http://192.168.1.1 --user admin:admin |grep -i "stats"
But when I made this code to use curl from bash script I have problem to read server and password from file `LINKS_FILE="server"
PASS="passwd"
for link in `cat "$LINKS_FILE"`
do
for pass in `cat "$PASS"`
do
res=$(curl -m 1 "http://${link}:8080" --user admin:${pass} )
if echo $res | grep -i "stats"; then `

Related

Subscribe and Publish in BASH like a receive Buffer

I want to buffer all messages of a subscription during processing publish messages.
The reason is a firmware update over MQTT. I want to send data with crc and the microcontroller have to verify the content and need to answer with new address pointer.
For now, i got it working without any interaction from the microcontroller. If size doesn't fit, or mc is not online, my bash script sends all the data of the binary file into the nirvana.
If I publish a message and subscribe directly after the publish, it is possible to lose an answer very easily.
But I'm not so experienced with bash and I need some suggestions how to do this...
This is what I have:
mosquitto_pub -h $MQTT_SERVER -t "spiffs/${client_name}/file_info" -m "$binary_file" -p 8883 -i "fw_server" --cafile server-cert.pem -d --insecure -u $MQTT_USER -P $MQTT_PW
mosquitto_sub -h $MQTT_SERVER -p 8883 -t "+/${client_name}/#" -i "fw_server" --cafile server-cert.pem -d --insecure -u $MQTT_USER -P $MQTT_PW | while read -r payload
do
fp_jq_rspCode=".upload.fp" # file pointer
address=$(echo $payload | jq -r $fp_jq_rspCode)
echo "${payload}" | jq -c '.[]'
echo "address pointer:$address"
c=${address}
addr=$(printf '%08x' "$c")
content=$(xxd -p -c $payload_size -l $payload_size -seek $c $binary_file)
length=`expr length "$content"`
len=$(printf '%04x' "$length")
crc32=$(echo -n "$content" | gzip -c | tail -c8 | hexdump -n4 -e '"%u"')
crc32=$(printf '%08x' "$crc32")
echo "$addr $len $content $crc32"
message=$(printf '%08x%04x%s%s' "$c" "$length" "$content" "$crc32")
mosq=$(mosquitto_pub -h $MQTT_SERVER -t "spiffs/${client_name}/upload" -m "$message" -p 8883 -i "fw_server" --cafile server-cert.pem -d --insecure -u $MQTT_USER -P $MQTT_PW)
done

BASH SCRIPT output doesn't export in file

i just stuck with my self coded application calling ws command (web socket) and i'm trying to export the output. Also i want to exit wscat when it's finished after sometime of input from API from the JSON backend devlopment
#!/bin/bash
while getopts a:c: flag
do
case "${flag}" in
a) accesskey=${OPTARG};;
c) clientnodeid=${OPTARG};;
esac
done
master="wscat -c ws://localhost:8091/ws/callback -H accessKey:$accesskey -H clientNodeId:$clientnodeid"
sleep 15
eval $master
final=$(eval echo "$master")
echo $final >>logfile.log
ps -ef | grep wscat | grep -v grep | awk '{print $2}' | xargs kill
#curl -X POST --data "$final" -k "https://localhost:7460/activate" -H "accept: application/json" -H "accessKey:$accesskey" -H "clientNodeId:$clientnodeid" -H "Content-Type: application/json" -H "callbackRequested:true"
exit
I want to call then output from wscat to sent over curl
When i run the script manually it got success but when i call it from another application (java) it's it running but not generating log.
With all words, i want to export $final to text file and that text file i should import it to --data of curl calling
Fixed based on #Barmar's comment:
You're overcomplicating this with all those variables. Just do
eval "$master" >> logfile.log

How do I insert a bash variable into the JSON body of a cURL request?

I am trying to run the following cURL command (taken from the ArgoCD documentation).
$ curl $ARGOCD_SERVER/api/v1/session -d $'{"username":"admin","password":"password"}'
With the proper credentials entered manually, it runs fine, but I have my password stored in an environment variable ($PASSWORD) and with the both the ' and " quotes it does not insert the password correctly.
$ curl $ARGOCD_SERVER/api/v1/session -d $'{"username":"admin","password":"$PASSWORD"}'
I suspect it uses the string literal $PASSWORD as the actual password, rather than the variable's content.
How would I insert this variable correctly?
You can use jq to create the JSON:
json=$(jq -c -n --arg username admin --arg password "$password" '$ARGS.named')
curl $ARGOCD_SERVER/api/v1/session -d "$json"
Using jq ensures that the JSON is properly formulated, no matter the contents of the variable:
$ password=$'with\'both"quotes'
$ declare -p password
declare -- password="with'both\"quotes"
$ jq -cn --arg username admin --arg password "$password" '$ARGS.named'
{"username":"admin","password":"with'both\"quotes"}
Like this:
curl $ARGOCD_SERVER/api/v1/session -d '{"username":"admin","password":"'$PASSWORD'"}'
or this:
curl $ARGOCD_SERVER/api/v1/session -d "{\"username\":\"admin\",\"password\":\"$PASSWORD\"}"
this'll probalby works too:
curl $ARGOCD_SERVER/api/v1/session -d "{'username':'admin','password':'$PASSWORD'}"
or:
printf -v data '{"username":"admin","password":"%s"}' "$PASSWORD"
curl $ARGOCD_SERVER/api/v1/session -d "$data"

I want to delete file from server by bash script using ftp

I want to delete file by bash script using ftp
I use below code
$file = xyz/ab/file.txt
curl -v -u $user:$pass ftp://server.com/$file -Q "DELE $file"
but it's give these error
*Entry path is '/'
DELE xyz/ab/file.txt
* ftp_perform ends with SECONDARY: 0
< 550 Could not delete xyz/ab/file.txt: No such file or directory
* QUOT command failed with 550
How can I delete file with single line bash script command
How to delete file from ftp server with curl:
user="foo"
pass="bar"
dir="xyz/ab/" # with trailing slash
file="file.txt"
curl -v -u "$user:$pass" "ftp://server.com/$dir" -Q "-DELE $file"
or
curl -v -u "$user:$pass" 'ftp://server.com' -Q "-DELE /$dir$file"
or without leading /
curl -v -u "$user:$pass" 'ftp://server.com' -Q "-DELE $dir$file"
You could use the ftp command if you want to add additional commands.
user=foo
user=bar
ftp -n 127.0.0.1 <<EOF
quote USER $user
quote PASS $pass
delete xyz/ab/file.txt
exit
EOF
Deleting must be available on the ftp server, however. If I remember correctly, for vsftpd you must set anon_other_write_enable=YES in /etc/vsftpd.conf

How can I pass the result of a local `curl` in the command line to a remote server in one line?

I'm looking to make something like the following work in a one-line shell.
curl -s icanhazip.com | ssh user#host 'php /path/to/script.php "[PASS_IP_HERE]"'
I need to pass my local IP to a remote server, preferably in one line.
Today I do it like this:
curl -s icanhazip.com // manually copy result
ssh user#host 'php /path/to/script.php "[PASTE_RESULT_HERE]"'
Assuming local IP is 1.2.3.4, and remote IP is 5.6.7.8, then the desired result is closer to:
> curl -s icanhazip.com
1.2.3.4
> ssh user#5.6.7.8 'php /path/to/script.php "1.2.3.4"'
// How can I pass this dynamically? ---^
Probably something like:
curl -s icanhazip.com | xargs -I{} ssh user#host 'php /path/to/script.php {}'
You can use command substitution:
ssh -t user#host "php /path/to/script.php $(curl -s icanhazip.com)"

Resources