#StackBounty: #bash #shell-script #shell #networking #scripting how to make script to ask user for network details & then to apply …

Bounty: 50

I want two scripts one where I collect the Network information and then second where I use that information to create and apply the net-plan configuration. (Want to achieve this using ONLY shell script)

So in short,

It should simply ask IP, Subnet or CIDR, Gateway & 2 DNS Server IP’s. At the same time it should validate the information entered by user.

The second script is to create a netplan coding. This is a yaml file that can be used to configure the network1. Example file,

network:
  ethernets:
    enp0s3:
      addresses: [192.168.1.3/24]
      gateway4: 192.168.1.1
      nameservers:
        addresses: [4.2.2.2, 8.8.8.8]
  version: 2

Can this be done? Can someone help? I want to use it into an automated installation of Ubuntu 20.04

Below is what I’m working on, I know it’s not complete and may be an awful wrong way of doing it, but that’s why I need help 😛

—shell

#!/bin/bash
nmcli device status | awk '{print $1}' | sed '1d' | sed '$d' > /tmp/itic-ls-networks
echo "None" >> /tmp/itic-ls-networks
sum3="cat /tmp/itic-ls-networks"

            nmcli device status | awk '{print $1}' | sed '1d' | sed '$d' | wc -l > /tmp/itic-total-networks
            sum1=$(cat /tmp/itic-total-networks)
            sum2=`expr $sum1 + 1`

            var1=1
            for sum2 in $var1
            do 
                nmcli device status | awk '{print $1}' | sed '1d' | sed '$d' | sed 's/^/ '$var1')     /' >> /tmp/itic-networks
            done
Please enter the IP Address
read itic-ip-cidr
num = `expr match $itic-ip-cidr [.]`
if num != 3
then
    echo "please enter the correct IP v4 address & the CIDR Value"

I’m still working on it and I’m stuck real bad and don’t know if I’m doing it right or not. Please please help!

Thanks in advance. stay safe guys! 🙂


Get this bounty!!!

#StackBounty: #bash #systemd #tty Can many script instances write to tty (parallel)?

Bounty: 50

I have recently migrated my upstart scripts to systemd, unlike upstart, I don’t
see any output on the tty for services being started/stopped. To get that visual
feedback, I added something like

      echo "Starting $UNIT_NAME" > $MYTTY

Where the MYTTY is an environment variable I am setting from output of tty command.
I have 20 odd services but somehow not all messages were appearing on my tty. So I changed
the line (just to check) to:

      echo "Starting $UNIT_NAME" | write myuser $MYTTY

And with this, I see all the messages being displayed! (of course, with the additional
Message from <user>@<hostname> on <term> at <time> ... EOF

Just to check if write is doing something special, I checked the code in bsdutils and
I didnt find anything special, its just writing character by character (with some handling
for special chars and CR, LF)

What is wrong with :

      echo "Starting $UNIT_NAME" > $MYTTY

?
I also tried:

      echo -e "Starting $UNIT_NAMErb" > $MYTTY

etc. But still I don’t see messages from all the services on the screen!
Is it because systemd starts up all services in parallel that some
writes to the tty vanish !?



Get this bounty!!!

#StackBounty: #command-line #bash #resource-limiting fork: retry: Resource temporarily unavailable in spite of highly tuned server sett…

Bounty: 100

# cat /etc/sysctl.conf 
fs.aio-max-nr=99999999
fs.file-max=99999999
kernel.pid_max=4194304
kernel.threads-max=99999999
kernel.sem=32768 1073741824 2000 32768
kernel.shmmni=32768
kernel.msgmni=32768
kernel.msgmax=65536
kernel.msgmnb=65536
vm.max_map_count=1048576

# cat /etc/security/limits.conf
 * soft core unlimited
 * hard core unlimited
 * soft data unlimited
 * hard data unlimited
 * soft fsize unlimited
 * hard fsize unlimited
 * soft memlock unlimited
 * hard memlock unlimited
 * soft nofile 1048576
 * hard nofile 1048576
 * soft rss unlimited
 * hard rss unlimited
 * soft stack unlimited
 * hard stack unlimited
 * soft cpu unlimited
 * hard cpu unlimited
 * soft nproc unlimited
 * hard nproc unlimited
 * soft as unlimited
 * hard as unlimited
 * soft maxlogins unlimited
 * hard maxlogins unlimited
 * soft maxsyslogins unlimited
 * hard maxsyslogins unlimited
 * soft locks unlimited
 * hard locks unlimited
 * soft sigpending unlimited
 * hard sigpending unlimited
 * soft msgqueue unlimited
 * hard msgqueue unlimited

# cat /etc/systemd/logind.conf
[Login]
UserTasksMax=infinity

# free -g 
              total        used        free      shared  buff/cache   available
Mem:            117           5          44          62          67          48
Swap:            15           8           7

# df -h /
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1       194G  121G   74G  63% /

# cat /proc/meminfo
MemTotal:       123665416 kB
MemFree:        90979152 kB
MemAvailable:   95376636 kB
Buffers:           72260 kB
Cached:         25964076 kB
SwapCached:            0 kB
Active:          8706568 kB
Inactive:       22983044 kB
Active(anon):    7568968 kB
Inactive(anon): 18871224 kB
Active(file):    1137600 kB
Inactive(file):  4111820 kB
Unevictable:           0 kB
Mlocked:               0 kB
SwapTotal:      16777212 kB
SwapFree:       16777212 kB
Dirty:                20 kB
Writeback:             0 kB
AnonPages:       5653128 kB
Mapped:           185100 kB
Shmem:          20786924 kB
KReclaimable:     281732 kB
Slab:             541000 kB
SReclaimable:     281732 kB
SUnreclaim:       259268 kB
KernelStack:       34384 kB
PageTables:        93216 kB
NFS_Unstable:          0 kB
Bounce:                0 kB
WritebackTmp:          0 kB
CommitLimit:    78609920 kB
Committed_AS:   63750908 kB
VmallocTotal:   34359738367 kB
VmallocUsed:       46584 kB
VmallocChunk:          0 kB
Percpu:            18944 kB
HardwareCorrupted:     0 kB
AnonHugePages:         0 kB
ShmemHugePages:        0 kB
ShmemPmdMapped:        0 kB
CmaTotal:              0 kB
CmaFree:               0 kB
HugePages_Total:       0
HugePages_Free:        0
HugePages_Rsvd:        0
HugePages_Surp:        0
Hugepagesize:       2048 kB
Hugetlb:               0 kB
DirectMap4k:      183484 kB
DirectMap2M:     5058560 kB
DirectMap1G:    122683392 kB
And for the user account used to run the scripts:

$ ulimit -a
core file size          (blocks, -c) unlimited
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) unlimited
max locked memory       (kbytes, -l) unlimited
max memory size         (kbytes, -m) unlimited
open files                      (-n) 1048576
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) unlimited
real-time priority              (-r) 0
stack size              (kbytes, -s) unlimited
cpu time               (seconds, -t) unlimited
max user processes              (-u) unlimited
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited

Yet

./somescript.sh: fork: retry: Resource temporarily unavailable

The server has medium load (~ 20 load average atm), and uses many scripts which do extensive forking (i.e. $(comecode) inside many scripts). The server (Google cloud instance) has 16 cores and 128GB ram with a 100GB tmpfs drive and 16GB swap. Even when the CPU, the memory and the swap are all under 50% use the message shows.

It is hard to believe it would be hitting any of these already high upper limits. I suspect there is some other setting that affect this.

What else can be tuned to avoid this fork: retry: Resource temporarily unavailable issue?


Get this bounty!!!

#StackBounty: #bash #file-descriptors #fd Write multiple lines to file descriptor without here-doc and delete it after reading <&amp…

Bounty: 50

#!/usr/bin/env bash

auth() { 
    exec 3<<AUTH
$1
$2
AUTH
}

auth test pwd
sh -c 'cat <&3'

I want to temporarily store credentials in an auth file/mem of the format user/npass, which has to get deleted once it is read by <&3. The binary, that reads it runs infinitely in a docker alpine container with bash5+ and is executed in a /bin/sh environment, so sub-shells like cat <(echo -e "$1n$2") won’t work and deleting the temp file after the command ends is no option.

The code above works, but i don’t like the look of the here-document without indentations. I know i can use real tabs with <<-EOF but this will end up in a public repository and has to function even when some editors replace tabs with 4 spaces, so i can’t rely on that.

Isn’t there an alternative that works like echo $user>$3 or putting the output from (echo -e "$1n$2") into the fd?

Whatever i tried, i either end up with the input <&3 blocking and waiting for EOF or the fd content not getting erased.

Thanks in advance!


Get this bounty!!!

#StackBounty: #bash #file-descriptors #fd Write multiple lines to file descriptor without here-doc and delete it after reading <&amp…

Bounty: 50

#!/usr/bin/env bash

auth() { 
    exec 3<<AUTH
$1
$2
AUTH
}

auth test pwd
sh -c 'cat <&3'

I want to temporarily store credentials in an auth file/mem of the format user/npass, which has to get deleted once it is read by <&3. The binary, that reads it runs infinitely in a docker alpine container with bash5+ and is executed in a /bin/sh environment, so sub-shells like cat <(echo -e "$1n$2") won’t work and deleting the temp file after the command ends is no option.

The code above works, but i don’t like the look of the here-document without indentations. I know i can use real tabs with <<-EOF but this will end up in a public repository and has to function even when some editors replace tabs with 4 spaces, so i can’t rely on that.

Isn’t there an alternative that works like echo $user>$3 or putting the output from (echo -e "$1n$2") into the fd?

Whatever i tried, i either end up with the input <&3 blocking and waiting for EOF or the fd content not getting erased.

Thanks in advance!


Get this bounty!!!

#StackBounty: #bash #file-descriptors #fd Write multiple lines to file descriptor without here-doc and delete it after reading <&amp…

Bounty: 50

#!/usr/bin/env bash

auth() { 
    exec 3<<AUTH
$1
$2
AUTH
}

auth test pwd
sh -c 'cat <&3'

I want to temporarily store credentials in an auth file/mem of the format user/npass, which has to get deleted once it is read by <&3. The binary, that reads it runs infinitely in a docker alpine container with bash5+ and is executed in a /bin/sh environment, so sub-shells like cat <(echo -e "$1n$2") won’t work and deleting the temp file after the command ends is no option.

The code above works, but i don’t like the look of the here-document without indentations. I know i can use real tabs with <<-EOF but this will end up in a public repository and has to function even when some editors replace tabs with 4 spaces, so i can’t rely on that.

Isn’t there an alternative that works like echo $user>$3 or putting the output from (echo -e "$1n$2") into the fd?

Whatever i tried, i either end up with the input <&3 blocking and waiting for EOF or the fd content not getting erased.

Thanks in advance!


Get this bounty!!!

#StackBounty: #bash #file-descriptors #fd Write multiple lines to file descriptor without here-doc and delete it after reading <&amp…

Bounty: 50

#!/usr/bin/env bash

auth() { 
    exec 3<<AUTH
$1
$2
AUTH
}

auth test pwd
sh -c 'cat <&3'

I want to temporarily store credentials in an auth file/mem of the format user/npass, which has to get deleted once it is read by <&3. The binary, that reads it runs infinitely in a docker alpine container with bash5+ and is executed in a /bin/sh environment, so sub-shells like cat <(echo -e "$1n$2") won’t work and deleting the temp file after the command ends is no option.

The code above works, but i don’t like the look of the here-document without indentations. I know i can use real tabs with <<-EOF but this will end up in a public repository and has to function even when some editors replace tabs with 4 spaces, so i can’t rely on that.

Isn’t there an alternative that works like echo $user>$3 or putting the output from (echo -e "$1n$2") into the fd?

Whatever i tried, i either end up with the input <&3 blocking and waiting for EOF or the fd content not getting erased.

Thanks in advance!


Get this bounty!!!

#StackBounty: #bash #ssh #gnu-screen #rename #title Make ssh LocalCommand set GNU-screen window title only when used interactively?

Bounty: 100

Setup

I run screen on a local machine and use different screen windows to connect to different hosts. It would be nice to change window names to the host names automatically during the ssh process.

Approaches

I have tried different variations of the 2 approaches below:

1) Using LocalComand in ssh config file:

Host *
PermitLocalCommand yes
LocalCommand '[ "${TERM:0:6}" == "screen" ] && printf "ek@%he\"

2) Creating a custom bash function:

# function to set screen name based on host and reset on exit
function ssh() {
  echo -e '33k'@$1'33\'
  ssh -Y "$@"
  echo -e '33k'local'33\'
}

Problem

Both of these approaches work but they also seem to interfere with things like auto-completion:

$ scp user@host:~/.s<tab>
...o^[\\   ...cal^[\\

Question

Is there some condition that could be used (on top of checking if $TERM == screen) that would allow executing the LocalCommand (or the ssh() function) only when connecting to host and not when used for auto-completion?


Get this bounty!!!

#StackBounty: #bash #shell #applescript Applescript won't move onto next instruction without ending shellscript?

Bounty: 100

I’m a new user to Applescript and programming in general.

I’m trying to create a script that will 1) load a Renpy project through the terminal and 2) then move the resulting project window to a different monitor using a keystroke command in the application Display Maid.

I can get two different scripts that run successfully on their own:

do shell script "/Applications/renpy-7.3.5-sdk/renpy.sh /Users/username/Documents/Renpy\ projects/projectname"

and

tell application "Display Maid"
    activate
    tell application "System Events" to keystroke "r" using {control down, command down}

end tell

When I put these together into one script, however, it never gets to the second step. It seems like Applescript wants to wait until the shell script is fully finished before getting to the Display Maid part.

How do I get this to work? I’ve also given the resulting app accessibility permissions through System Preferences, but that doesn’t change anything.


Get this bounty!!!

#StackBounty: #bash #shell #applescript Applescript won't move onto next instruction without ending shellscript?

Bounty: 100

I’m a new user to Applescript and programming in general.

I’m trying to create a script that will 1) load a Renpy project through the terminal and 2) then move the resulting project window to a different monitor using a keystroke command in the application Display Maid.

I can get two different scripts that run successfully on their own:

do shell script "/Applications/renpy-7.3.5-sdk/renpy.sh /Users/username/Documents/Renpy\ projects/projectname"

and

tell application "Display Maid"
    activate
    tell application "System Events" to keystroke "r" using {control down, command down}

end tell

When I put these together into one script, however, it never gets to the second step. It seems like Applescript wants to wait until the shell script is fully finished before getting to the Display Maid part.

How do I get this to work? I’ve also given the resulting app accessibility permissions through System Preferences, but that doesn’t change anything.


Get this bounty!!!