GVKun编程网logo

PySerial App在shell中运行,不是py脚本(python shell运行py文件)

25

如果您想了解PySerialApp在shell中运行,不是py脚本的相关知识,那么本文是一篇不可错过的文章,我们将对pythonshell运行py文件进行全面详尽的解释,并且为您提供关于bash–Fi

如果您想了解PySerial App在shell中运行,不是py脚本的相关知识,那么本文是一篇不可错过的文章,我们将对python shell运行py文件进行全面详尽的解释,并且为您提供关于bash – Fish shell中运行脚本的目录、bash – shell_exec无法在php web appl中运行、bash – 在shell脚本中运行wget和其他命令、bash – 如何在shell脚本中运行’cd’,并在脚本完成后留在那里?的有价值的信息。

本文目录一览:

PySerial App在shell中运行,不是py脚本(python shell运行py文件)

PySerial App在shell中运行,不是py脚本(python shell运行py文件)

我有一个非常简单的python脚本,它使用pySerial通过串行端口将数据发送到Arduino。当我在python
shell中逐行执行此命令时,它工作正常,但是当我将其放在“
.py”文件中并尝试运行它时,什么也没发生。虽然我的UART上的串行指示灯确实会闪烁。所以事情正在解决,但这是垃圾(我检查了)。

这是简单的代码。

import serial

ser = serial.Serial('COM3',9600,timeout=.2)
ser.write('A')
ser.close()

我已经尝试添加睡眠,但是似乎没有任何方法可以解决此问题。有任何想法吗?

bash – Fish shell中运行脚本的目录

bash – Fish shell中运行脚本的目录

我正在尝试使用Fish shell运行SBT.下面是我正在尝试实现的等效 Bash脚本:

java -Xmx512M -jar `dirname $0`/sbt-launch.jar "$@"

我在Fish文档中看到,Bash中的$@可以用Fish中的$argv替换.
但我无法看到用什么替换dirname $0.

有谁知道Fish中的等效脚本?

解决方法

$_似乎只能通过阅读器/命令行直接工作,或者当脚本来源时,对我来说.

也许这对你有用:

java -Xmx512M -jar (dirname (status -f))/sbt-launch.jar "$argv"      # fish

bash – shell_exec无法在php web appl中运行

bash – shell_exec无法在php web appl中运行

我创建了一个 shell脚本来检测网络交互,然后为每个接口检测IP地址,掩码,广播地址,然后对该网络接口的所有IP地址进行ping操作.

该脚本具有执行权限.通常,脚本会将网络接口列表(eth0 eth1 wlan0)保存在名为“resultat”的文件中,但是当我使用PHP的shell_exec命令从网页运行此脚本时(echo-password for www-data user’| / usr) / lib / cgi-bin / sudo -S global.sh bin / bash /“)没有生成输出.

如果我在终端中运行与用户www-data相同的脚本,则会正确填充结果文件.

剧本:

#!/bin/bash
  #####   paramères relatives au connexion à la base de données
  HOST_BDD="localhost"
  LOGIN="root"
  PASSWD="password"
  NOM_BDD="dbnessus"
  ##### ces requettes pour vider les tables avant de faire la detection
  vider2="TruncATE machine_connecte"
  echo $vider2 | /usr/bin/MysqL -h $HOST_BDD 
  -u $LOGIN -p$PASSWD -s $NOM_BDD

  vider1="TruncATE interfaces"
  echo $vider1 | /usr/bin/MysqL -h $HOST_BDD 
  -u $LOGIN -p$PASSWD -s $NOM_BDD

  initialise="ALTER TABLE machine_connecte AUTO_INCREMENT=0"
  echo $initialise | /usr/bin/MysqL -h $HOST_BDD 
  -u $LOGIN -p$PASSWD -s $NOM_BDD
  #######################################################

  /usr/lib/cgi-bin/get_interface.sh > /usr/lib/cgi-bin/liste_interfaces
  while read line; 
  do 

  ip=$(/usr/lib/cgi-bin/get_ip.sh $line)
  mask=$(/usr/lib/cgi-bin/get_netmask.sh $line)
  bcast=$(/usr/lib/cgi-bin/get_bcast.sh $line)


  ###fonction is_alive_ping
  is_alive_ping()
  {
    ping -i 100 -c 1 $1 > /dev/null 2> /dev/null
   [ $? -eq 0 ] && echo  $i >>/usr/lib/cgi-bin/resultat
  }
  cat /dev/null >/usr/lib/cgi-bin/resultat;
  #########


  ###division des octet d'adresse de broadcst
  if [ "$ip" != "" ]
  then
  i1="$(echo $bcast |cut -d"." -f1)"
  i2="$(echo $bcast |cut -d"." -f2)"
  i3="$(echo $bcast |cut -d"." -f3)"
  i4="$(echo $bcast |cut -d"." -f4)"
  fi
  ###  { HostID / NetworkID } / classe du réseau

  ##################### A.255.255.255 Classe A
  if [ "$i2" == "255" ]
  then
for i in "$i1".{1..254}.{1..254}.{1..254}
do
is_alive_ping $i & disown
done
  fi
  ##################### A.B.255.255 Classe B
  if [ "$i2" != "255" ] && [ "$i3" == "255" ]
  then
for i in "$i1.$i2".{1..254}.{1..254} 
do
is_alive_ping $i & disown
done
  fi
  ##################### A.B.C.255 Classe C
  if [ "$i2" != "255" ] && [ "$i3" != "255" ]&& [ "$i4" == "255" ]
  then
for i in "$i1.$i2.$i3".{1..254} 
do
is_alive_ping $i & disown
    done
  fi
  ################
while read ip_up; 
do 
hostname=$(/usr/bin/resolveip -s $ip_up 2>/dev/null)
if [ "$hostname" == "" ]
then
hostname="*"
fi
mac=$(/usr/sbin/arp -a $ip_up |cut -d" " -f4)
if [ "$ip_up" == "$ip" ] 
then
mac=$(/sbin/ifconfig $line |grep 'HWaddr'|grep -v '127.0.0.1'|awk '{ print $5}')
fi
OS=$( /usr/bin/nmap -A $ip_up |grep "Service Info:" |awk '{print $4,$5}' )
if [ "$OS" == "Unix,Linux" ] || [ "$OS" == "Linux" ]
then
OS="Linux"
elif [ "$OS" == "Windows " ]
then 
OS="Windows"
else
OS="*"
fi
 #sql1="INSERT INTO  dbnessus.interfaces (nom_interface)VALUES ('$line');"
 sql1="INSERT IGnorE INTO  dbnessus.interfaces (nom_interface)VALUES ('$line');"
 sql2="INSERT INTO  dbnessus.machine_connecte (idmachine,ip_mach,mask_mach,nom_mach,mac_mach,os_mach,interfaces_nom_interface)VALUES ( NULL,'$ip_up','$mask','$hostname','$mac','$OS','$line');"


 echo $sql1 | /usr/bin/MysqL -h $HOST_BDD -u $LOGIN -p$PASSWD -s $NOM_BDD
 echo $sql2 | /usr/bin/MysqL -h $HOST_BDD -u $LOGIN -p$PASSWD -s $NOM_BDD




    done < /usr/lib/cgi-bin/resultat
  ip=""
  done < /usr/lib/cgi-bin/liste_interfaces
  echo "cbon"

解决方法

几个想法:

>检查shell_exec是否在PHP.ini的disabled_functions中
(PHP.ini:disabled_functions)
>确保PHP未以安全模式运行
(PHP.ini:safe_mode)
>确保PHP-fpm进程(如果使用PHP-fpm)或https(如果使用apxs)在足够的权限下工作(运行shell脚本并在脚本中执行这些命令)
(在这种情况下,你可以su到该用户,看看你是否可以从bash运行它)

对不起,到目前为止我无法想到更多……

bash – 在shell脚本中运行wget和其他命令

bash – 在shell脚本中运行wget和其他命令

我正在尝试创建一个 shell脚本,我将最新的Atomic gotroot规则下载到我的服务器,解压缩,将它们复制到正确的文件夹等,

我一直在大部分时间都在阅读shell教程和论坛帖子,语法让我逃脱了其中的一些.我已经运行了所有这些命令,如果我手动运行它们,我知道它们有效.

我知道我需要开发一些错误检查,但我只是想让命令正确运行.目前的主要问题是wget命令的语法,我有关于缺少分号的错误,除以零,不支持的方案 – 我尝试了各种引用(单和双)和转义 – /“字符各种组合.

谢谢你的帮助.
原始wget命令是

wget –user =“jim”–password =“xxx-yyy-zzz”“http://updates.atomicorp.com/channels/rules/subscription/VERSION”

#!/bin/sh
update_modsec_rules(){

wget=/usr/bin/wget
tar=/bin/tar
apachectl=/usr/bin/apache2ctl


TXT="Script Run Finished"
WORKING_DIR="/var/asl/updates"
TARGET_DIR="/usr/local/apache/conf/modsec_rules/"
EXISTING_FILES="/var/asl/updates/modsec/*"
EXISTING_ARCH="/var/asl/updates/modsec-*"
WGET_OPTS='--user=jim --password=xxx-yyy-zzz'
URL_BASE="http://updates.atomicorp.com/channels/rules/subscription"


# change to working directory and cleanup any downloaded files and extracted rules in modsec/ directory
cd $WORKING_DIR
rm -f $EXISTING_ARCH
rm -f $EXISTING_FILES
rm -f VERSION*

# wget to download VERSION file
$wget ${WGET_OPTS} "${URL_BASE}/VERSION"

# get current MODSEC_VERSION from VERSION file and save as variable
source VERSION
TARGET_DATE=$MODSEC_VERSION
echo $TARGET_DATE

# wget to download current archive
$wget ${WGET_OPTS} "${URL_BASE}/modsec-${TARGET_DATE}.tar.gz"

# extract archive
echo "extracting files . . . "
tar zxvf $WORKING_DIR/modsec-${TARGET_DATE}.tar.gz

echo "copying files . . . "
cp -uv $EXISTING_FILES $TARGET_DIR

echo $TXT
}

update_modsec_rules $@ 2>&1 | tee -a /var/asl/modsec_update.log

RESTART_APACHE="/usr/local/cpanel/scripts/restartsrv httpd"
$RESTART_APACHE
以下是编写shell脚本时要使用的一些准则.

>使用它们时始终引用变量.这有助于避免误解的可能性. (如果文件名包含空格怎么办?)
>不要相信像rm这样的命令上的文件加载.请改用循环. (如果文件名以连字符开头怎么办?)
>尽可能避免使用子壳.带反引号的线条让我发痒.
>如果你能提供帮助,请不要执行.特别是在你的exec实际运行之后,不要指望脚本的任何部分.

我应该指出,虽然你的shell可能是bash,但你已经指定了/ bin / sh来执行这个脚本,所以它不是一个bash脚本.

这是一个重写错误检查.加盐调味.

#!/bin/sh

# Linux
wget=/usr/bin/wget
tar=/bin/tar
apachectl=/usr/sbin/apache2ctl

# FreeBSD
#wget=/usr/local/bin/wget
#tar=/usr/bin/tar
#apachectl=/usr/local/sbin/apachectl

TXT="GOT TO THE END,YEAH"
WORKING_DIR="/var/asl/updates"
TARGET_DIR="/usr/local/apache/conf/modsec_rules/"
EXISTING_FILES_DIR="/var/asl/updates/modsec/"
EXISTING_ARCH="/var/asl/updates/"

URL_BASE="http://updates.atomicorp.com/channels/rules/subscription"
WGET_OPTS='--user="jim" --password="xxx-yyy-zzz"'

if [ ! -x "$wget" ]; then
  echo "ERROR: No wget." >&2
  exit 1
elif [ ! -x "$apachectl" ]; then
  echo "ERROR: No apachectl." >&2
  exit 1
elif [ ! -x "$tar" ]; then
  echo "ERROR: Not in Kansas anymore,Toto." >&2
  exit 1
fi

# change to working directory and cleanup any downloaded files
# and extracted rules in modsec/ directory
if ! cd "$WORKING_DIR"; then
  echo "ERROR: can't access working directory ($WORKING_DIR)" >&2
  exit 1
fi

# Delete each file in a loop.
for file in "$EXISTING_FILES_DIR"/* "$EXISTING_ARCH_DIR"/modsec-*; do
  rm -f "$file"
done

# Move old VERSION out of the way.
mv VERSION VERSION-$$

# wget1 to download VERSION file (replaces WGET1)
if ! $wget $WGET_OPTS $URL_BASE}/VERSION; then
  echo "ERROR: can't get VERSION" >&2
  mv VERSION-$$VERSION
  exit 1
fi

# get current MODSEC_VERSION from VERSION file and save as variable,# but DON'T blindly trust and run scripts from an external source.
if grep -q '^MODSEC_VERSION=' VERSION; then
  TARGET_DATE="`sed -ne '/^MODSEC_VERSION=/{s/^[^=]*=//p;q;}' VERSION`"
  echo "Target date: $TARGET_DATE"
fi

# Download current archive (replaces WGET2)
if ! $wget ${WGET_OPTS} "${URL_BASE}/modsec-$TARGET_DATE.tar.gz"; then
  echo "ERROR: can't get archive" >&2
  mv VERSION-$$VERSION         # Do this,don't do this,I don't kNow your needs.
  exit 1
fi

# extract archive
if [ ! -f "$WORKING_DIR/modsec-${TARGET_DATE}.tar.gz" ]; then
  echo "ERROR: I'm confused,where's my archive?" >&2
  mv VERSION-$$VERSION         # Do this,I don't kNow your needs.
  exit 1
fi
tar zxvf "$WORKING_DIR/modsec-${TARGET_DATE}.tar.gz"

for file in "$EXISTING_FILES_DIR"/*; do
  cp "$file" "$TARGET_DIR/"
done

# So far so good,so let's restart apache.
if $apachectl configtest; then
  if $apachectl restart; then
    # Success!
    rm -f VERSION-$$
    echo "$TXT"
  else
    echo "ERROR: PANIC! Apache didn't restart.  Notify the authorities!" >&2
    exit 3
  fi
else
  echo "ERROR: Apache configs are broken.  We're still running,but you'd better fix this ASAP." >&2
  exit 2
fi

请注意,虽然我已经将其重写为更加明智,但仍然有很大的改进空间.

bash – 如何在shell脚本中运行’cd’,并在脚本完成后留在那里?

bash – 如何在shell脚本中运行’cd’,并在脚本完成后留在那里?

我在我的shell脚本(bash)中使用’change directory’
#!/bin/bash
alias mycd='cd some_place'
mycd
pwd

pwd打印some_place正确,但在脚本完成后,我当前的工作目录不会更改。

是否可以通过脚本更改我的路径?

您需要将该文件作为:
. myfile.sh

要么

source myfile.sh

如果没有source,那么更改将发生在子shell中,而不是在调用脚本的父shell中。但是当您源文件时,文件中的行将像在命令行中输入一样执行。

我们今天的关于PySerial App在shell中运行,不是py脚本python shell运行py文件的分享就到这里,谢谢您的阅读,如果想了解更多关于bash – Fish shell中运行脚本的目录、bash – shell_exec无法在php web appl中运行、bash – 在shell脚本中运行wget和其他命令、bash – 如何在shell脚本中运行’cd’,并在脚本完成后留在那里?的相关信息,可以在本站进行搜索。

本文标签: