CISCO IOS-XR, Python3.7, Not able to run commands like 'ls' and 'df' on Cisco router - subprocess

Cisco ios-xr router using CLI:
RP/0/RP0#show version
Thu Nov 25 07:53:59.103 UTC
Cisco IOS XR Software, Version 6.5.32.11I
Copyright (c) 2013-2020 by Cisco Systems, Inc.
RP/0/RP0#run
Thu Nov 25 07:54:05.231 UTC
[xr-vm_node0_RP0_CPU0:~]$df
Filesystem 1K-blocks Used Available Use% Mounted on
rootfs 3966080 1332040 2412860 36% /
76892 11848320 43% /mnt/ecu/vdd
[xr-vm_node0_RP0_CPU0:~]$
Using python:
I am able to run show commands using Connecthandler .send.command:
from netmiko import ConnectHandler
import subprocess
Network_Device = {"host": "10.111.22.333", "username": "USER123", "password": "Pass123", "device_type": "cisco_xr",}
Connect = ConnectHandler(**Network_Device)
Connect.enable()
version1 = "show version"
print(Connect.send_command(version1))
But not able to run 'df' or 'ls' commands, as not able to reach bash prompt i reach by running 'run' command on router.
I tried:
disk1files = subprocess.run("df", stdout=subprocess.PIPE)
print(disk1files.stdout.decode())
But seems its wrong. Please suggest the right library or code I can use here.
This is my first question here, so bear some silly questions or mistakes done in code

if on DF you are referring to "Don't fragment" then it is posible to send it like
Connect.send_command("ping 192.168.10.10 df-bit size 1600")
where 1600 represents MTU, and for ls commands is link command,
Connect.send_command("ls-active")
Connect.send_command("ls-active-enabled")
but if you are referring to df and ls in linux (disk free and list files..) then you can use os module for sending commands:
import os
os.system("ls -l")
or use call from subprocess module:
from subprocess import call
call(["ls", "-l"])
If you need to acccess cisco bash:
switch# configure terminal
switch(config)# feature bash-shell
switch# run?
run Execute/run program
run-script Run shell scripts
switch# run bash?
bash Linux-bash
switch# run bash
bash-4.2$ whoami
admin
bash-4.2$ pwd
/bootflash/home/admin
bash-4.2$

Related

eclipse CDT esp-idf build but don't flash

Windows10 eclipse esp-idf latest version 2021-03
With the command line idf.py I can build and flash the esp-idf\examples\get-started\blink programme which run on a ESP32.
In eclipse, the buils works but the run command display in console
Usage: C:\Users\peter\esp-idf\tools\idf.py [OPTIONS] COMMAND1 [ARGS]...
[COMMAND2 [ARGS]...]...
ESP-IDF CLI build management tool. For commands that are not known to
idf.py an attempt to execute it as a build system target will be made.
.... bla bla ...
Can anybody tells me what is wrong ?
Regards
There is a bug in the eclipse/esp-idf launch bar.
The ESP target for esp32 is defined with a Serial Port COM3.
But that info is not used.
If one redefine a new ESP Target with the same serial port under a different name, then the run command will work!
See hereafter for the people interested in the details
cmd.exe /C "cd /D C:\Users\peter\esp-idf\components\esptool_py && C:\Users\peter.espressif\tools\cmake\3.16.4\bin\cmake.exe -D IDF_PATH="C:/Users/peter/esp-idf" -D ESPTOOLPY="C:\Users\peter.espressif\python_env\idf4.2_py3.8_env\Scripts\python.exe C:/Users/peter/esp-idf/components/esptool_py/esptool/esptool.py --chip esp32" -D ESPTOOL_ARGS="--before=default_reset --after=hard_reset write_flash #flash_args" -D WORKING_DIRECTORY="C:/Users/peter/eclipse-workspace/blink/build" -P C:/Users/peter/esp-idf/components/esptool_py/run_esptool.cmake"
esptool.py --chip esp32 -p COM3 -b 460800 --before=default_reset --after=hard_reset write_flash --flash_mode dio --flash_freq 40m --flash_size 2MB 0x8000 partition_table/partition-table.bin 0x1000 bootloader/bootloader.bin 0x10000 blink.bin
esptool.py v3.0

raspistill returning file not found

I am trying to use the 64 bit version of raspbian (which can be found here: https://www.raspberrypi.org/forums/viewtopic.php?f=117&t=275370
I downloaded it, installed everything, ran my updates and then switched on the camera. But when I try to run it, the PI just gives back
bash: /opt/vc/bin/raspistill: No such file or directory
When I do a ls, I can see the directory fine:
pi#raspberrypi:/opt/vc/bin $ ls
containers_check_frame_int containers_test dtoverlay-pre raspiyuv
containers_datagram_receiver containers_test_bits dtparam tvservice
containers_datagram_sender containers_test_uri edidparser vcdbg
containers_dump_pktfile containers_uri_pipe mmal_vc_diag vcgencmd
containers_rtp_decoder dtmerge raspistill vchiq_test
containers_stream_client dtoverlay raspivid vcmailbox
containers_stream_server dtoverlay-post raspividyuv vcsmem
and when I look at the permissions, there are read/execute permissions for everyone:
-rwxr-xr-x 1 root root 142397 Nov 1 16:25 raspistill
Im at a bit of a loss here - the file is right there, so why is it not being found when I try to call it from the command line?
Unfortunately, it looks like MMAL userland still (at the time of writing this) has some unresolved issues with 64bit raspberry pi OS, so it is disabled.
However, one can use docker or cherry-build 32bit packages as workarounds.

Why gcloud command is slow to start?

Just typing gcloud for help take 5 secs.
$ gcloud
...
gcloud 0.30s user 0.13s system 7% cpu 5.508 total
$ gcloud version
Google Cloud SDK 128.0.0
alpha 2016.01.12
bq 2.0.24
bq-nix 2.0.24
core 2016.09.23
core-nix 2016.09.20
gcloud
gsutil 4.21
gsutil-nix 4.21
kubectl
kubectl-darwin-x86_64 1.3.7
$ uname -a
Darwin hiroshi-MacBook.local 16.0.0 Darwin Kernel Version 16.0.0: Mon Aug 29 17:56:20 PDT 2016; root:xnu-3789.1.32~3/RELEASE_X86_64 x86_64
EDIT 2017-03-31: Zachary said that gcloud 148.0.0 addressed this issue. So try gcloud components update. see https://stackoverflow.com/users/4922212/zachary-newman
tl;dr
It turns out that socket.gethostbyaddr(socket.gethostname()) is slow for .local hostname in macOS.
$ python -i
>>> socket.gethostname()
'hiroshi-MacBook.local'
>>> socket.gethostbyaddr(socket.gethostname()) # it takes about 5 seconds
('localhost', ['1.0.0.127.in-addr.arpa'], ['127.0.0.1'])
So, for a workaround, just added the hostname to the localhost line of /etc/hosts.
127.0.0.1 localhost hiroshi-Macbook.local
After that is return value is different, but it returns in an instant.
>>> socket.gethostbyaddr(socket.gethostname())
('localhost', ['1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa'], ['::1'])
How do I get there
Where gcloud command is:
$ which gcloud
/Users/hiroshi/google-cloud-sdk/bin/gcloud
Edit the endline of the shell script...
...
+ echo "$CLOUDSDK_PYTHON" $CLOUDSDK_PYTHON_ARGS "${CLOUDSDK_ROOT_DIR}/lib/gcloud.py" "$#"
"$CLOUDSDK_PYTHON" $CLOUDSDK_PYTHON_ARGS "${CLOUDSDK_ROOT_DIR}/lib/gcloud.py" "$#"
to echo where the gcloud.py is:
$ gcloud
python2.7 -S /Users/hiroshi/google-cloud-sdk/lib/gcloud.py
OK. Who take the 5 secs?
$ python2.7 -S -m cProfile -s time /Users/hiroshi/google-cloud-sdk/lib/gcloud.py
173315 function calls (168167 primitive calls) in 5.451 seconds
Ordered by: internal time
ncalls tottime percall cumtime percall filename:lineno(function)
1 5.062 5.062 5.062 5.062 {_socket.gethostbyaddr}
...
_socket.gethostbyaddr is.
What is the argument of the function call and backtrace look like?
I added some lines before main() of gcloud.py
import traceback
def mygethostbyaddr(addr):
print addr
traceback.print_stack()
return addr
import socket
socket.gethostbyaddr = mygethostbyaddr
Execute gcloud again.
I got it is my .local name of my machine.
$ gcloud
hiroshi-MacBook.local
File "/Users/hiroshi/google-cloud-sdk/lib/gcloud.py", line 74, in <module>
main()
File "/Users/hiroshi/google-cloud-sdk/lib/gcloud.py", line 70, in main
sys.exit(googlecloudsdk.gcloud_main.main())
File "/Users/hiroshi/google-cloud-sdk/lib/googlecloudsdk/gcloud_main.py", line 121, in main
metrics.Started(START_TIME)
File "/Users/hiroshi/google-cloud-sdk/lib/googlecloudsdk/core/metrics.py", line 411, in Wrapper
return func(*args, **kwds)
File "/Users/hiroshi/google-cloud-sdk/lib/googlecloudsdk/core/metrics.py", line 554, in Started
collector = _MetricsCollector.GetCollector()
File "/Users/hiroshi/google-cloud-sdk/lib/googlecloudsdk/core/metrics.py", line 139, in GetCollector
_MetricsCollector._instance = _MetricsCollector()
File "/Users/hiroshi/google-cloud-sdk/lib/googlecloudsdk/core/metrics.py", line 197, in __init__
hostname = socket.getfqdn()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 141, in getfqdn
hostname, aliases, ipaddrs = gethostbyaddr(name)
File "/Users/hiroshi/google-cloud-sdk/lib/gcloud.py", line 32, in mygethostbyaddr
traceback.print_stack()
#hiroshi's answer solved the issue for me i.e. to run gcloud components update. However, since I had installed gcloud through their Cloud SDK using a package manager, I was stuck with the following error*:
ERROR: (gcloud.components.update) You cannot perform this action
because the Google Cloud CLI component manager is disabled for this
installation.
Hence, I had to explicitly mention the apt-get libraries to perform the update. The following command helped me to get it done in one go:
sudo apt-get update && sudo apt-get --only-upgrade install google-cloud-sdk-app-engine-go google-cloud-sdk-datastore-emulator google-cloud-sdk-datalab google-cloud-sdk-firestore-emulator google-cloud-sdk-kubectl-oidc google-cloud-sdk google-cloud-sdk-app-engine-python-extras google-cloud-sdk-cloud-build-local kubectl google-cloud-sdk-cbt google-cloud-sdk-minikube google-cloud-sdk-skaffold google-cloud-sdk-cloud-run-proxy google-cloud-sdk-pubsub-emulator google-cloud-sdk-config-connector google-cloud-sdk-gke-gcloud-auth-plugin google-cloud-sdk-kpt google-cloud-sdk-local-extract google-cloud-sdk-nomos google-cloud-sdk-app-engine-grpc google-cloud-sdk-bigtable-emulator google-cloud-sdk-app-engine-python google-cloud-sdk-terraform-validator google-cloud-sdk-anthos-auth google-cloud-sdk-spanner-emulator google-cloud-sdk-app-engine-java
*More details as to why the aforementioned error occurs and how to permanently solve it can be found here.

Selenium headless does not run on CentOS 7 with Perl, "no display specified"

I try to run headless Selenium on CentOS7:
# cat /etc/os-release
NAME="Red Hat Enterprise Linux Server"
VERSION="7.2 (Maipo)"
I installed Xvfb and run it as
# /usr/bin/Xvfb :99
I installed firefox:
# firefox -v
Mozilla Firefox 38.5.0
and run it to check if it can be run at all:
# export DISPLAY=:99
# firefox
This is the output:
# firefox
Xlib: extension "RANDR" missing on display ":99".
console.error:
[CustomizableUI]
Custom widget with id loop-button does not return a valid node
console.error:
[CustomizableUI]
Custom widget with id loop-button does not return a valid node
GLib-GIO-Message: Using the 'memory' GSettings backend. Your settings will not be saved or shared with other applications.
Firefox seems to be running after that command:
# ps aux | grep firefox
root 29476 7.3 14.9 852356 152256 pts/3 Sl+ 10:30 0:03 /usr/lib64/firefox/firefox
EDIT
Yes, it's running. Taking screenshot from the Xvfb by
DISPLAY=:99 import -window root -crop 1264x948+0+0 /tmp/screenshot.jpg
I can see
Now the problematic part.
I installed Selenium Remote Driver for perl
# cpanm Selenium::Remote::Driver
Then I ran standalone selenium driver:
# java -jar selenium-server-standalone-2.49.0.jar
Now I run test script:
#!/usr/bin/perl
use strict;
use warnings;
use Selenium::Remote::Driver;
my $driver = Selenium::Remote::Driver->new(browser_name=>'firefox');
$driver->get('http://www.google.com');
print $driver->get_title();
$driver->quit();
After 45 second I get error from the driver:
Could not create new session: Unable to connect to host 127.0.0.1 on port 7055 after 45000 ms. Firefox console output:
Error: no display specified
at (eval 89) line 510.
Seems like firefox launched by the driver does not see DISPLAY environment variable. I try to add it from the script:
#!/usr/bin/perl
use strict;
use warnings;
use Selenium::Remote::Driver;
$ENV{DISPLAY}=":99";
my $driver = Selenium::Remote::Driver->new(browser_name=>'firefox');
$driver->get('http://www.google.com');
print $driver->get_title();
$driver->quit();
It does not help, the previous error remains.
What do I do?
EDIT2
I tried the current setup with Python. All works.
# pip install selenium
And used the following test script:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.get("http://www.python.org")
f = open('ptn-sel.txt', 'w')
f.write(driver.title)
driver.close()
f.close()
I understand it's problem of Perl driver.... Any suggestions?
Is python using the standalone server or running firefox itself?
If perl is using the server and the server is spawning firefox then you need $DISPLAY set in the server processes environment not the script's environment. (By running export DISPLAY=:99; java -jar selenium-server-standalone-2.49.0.jaror similar.)
If you don't want to use the standalone server at all then Selenium::Firefox looks like it might be interesting.

Calling sem_open on Solaris as ordinary user

This call fails on Solaris with EACCES when ran as ordinary user:
sem_open(fileName.c_str(), O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO, 1);
When process is started as root, it runs fine. Is this expected behavior?
Environment:
$ uname -a
SunOS solaris 5.11 11.0 i86pc i386 i86pc
$ g++ --version
g++ (GCC) 4.5.2
At the command line try:
prctl $$
These are the system enforced resource limits your process has. Note there are
process.max-sem-ops
process.max-sem-nsems
project.max-sem-ids
These are limits that have a number, if you do not see them (or the limits are already reached) then you have to add them to your account's profile with projadd or projmod to increase them if your project already exists.
If you cannot do this (no root access) consult with your sysadmin, s/he probably has some reason for not allowing semapahore access.
Note carefully:
sempahores are kernel persistent. If you ran your code a bunch of times the sempahores you created are likely still out there.
To see existing semaphores try ipcs -as
To remove lingering sempahores that your code should have removed use ipcrm